Skip to content

Commit 402714e

Browse files
author
Vivek Chowdhury
committed
added login api and services for mock validation [Work in progress]
1 parent 29907f9 commit 402714e

File tree

7 files changed

+149
-8
lines changed

7 files changed

+149
-8
lines changed

package-lock.json

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"prestart:api": "node tools/createMockDatabase.js",
2020
"start:api": "node tools/localServer.js",
2121
"start": "run-p start:api start:dev",
22-
"start:dev": "react-scripts start",
22+
"start:dev": "cross-env REACT_APP_API_URL=http://localhost:3001 react-scripts start",
2323
"build": "react-scripts build",
2424
"test": "react-scripts test",
2525
"eject": "react-scripts eject"
@@ -40,6 +40,7 @@
4040
]
4141
},
4242
"devDependencies": {
43+
"cross-env": "^7.0.2",
4344
"json-server": "^0.16.1",
4445
"npm-run-all": "^4.1.5"
4546
}

src/api/LoginServices.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const baseUrl = process.env.REACT_APP_API_URL + "/Login";
2+
3+
export function validateUser(userid, password) {
4+
const url = baseUrl + `?userid=${userid}&password=${password}`;
5+
return fetch(url).then(handleResponse).catch(handleError);
6+
}
7+
8+
async function handleResponse(response) {
9+
if (response.ok) return response.json();
10+
if (response.status === 400) {
11+
// So, a server-side validation error occurred.
12+
// Server side validation returns a string error message, so parse as text instead of json.
13+
const error = await response.text();
14+
throw new Error(error);
15+
}
16+
throw new Error("Network response was not ok.");
17+
}
18+
19+
function handleError(error) {
20+
console.error("API call failed. " + error);
21+
throw error;
22+
}

src/api/UserService.js

Whitespace-only changes.

src/app/Login/Login.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ import React, { useState } from "react";
22
import "./Login.css";
33
import LoginForm from "./LoginForm/LoginForm";
44
import bubbleimage from "./../../assets/placeholders/bubble-image.png";
5+
import * as LoginServices from "./../../api/LoginServices";
6+
7+
function Login(props) {
8+
const [user, setUser] = useState({ userId: "", password: "" });
9+
10+
const handleChange = ({ target }) => {
11+
setUser({
12+
...user,
13+
[target.name]: target.value,
14+
});
15+
};
16+
17+
const handleSubmit = (event) => {
18+
event.preventDefault();
19+
console.log(user);
20+
LoginServices.validateUser(user.userId, user.password).then((response) => {
21+
console.log(response);
22+
props.history.push("/feeds");
23+
});
24+
};
525

6-
function Login() {
7-
const [user] = useState({ userid: "", password: "" });
8-
const handleChange = ({ target }) => {};
9-
const handleSubmit = () => {};
1026
return (
1127
<div style={{ backgroundImage: `url(${bubbleimage})` }}>
1228
{/* <div>

src/app/TechMarketShell.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import Footer from "./Footer/Footer";
33
import Header from "./Header/Header";
4-
import { Switch, Route } from "react-router-dom";
4+
import { Switch, Route, Redirect } from "react-router-dom";
55
import "./TechMarketShell.css";
66

77
import Feeds from "./Feeds/Feeds";
@@ -11,14 +11,22 @@ import ContactUs from "./ContactUs/ContactUs";
1111
import Login from "./Login/Login";
1212

1313
function TechMarketShell(props) {
14+
function enforceAuthentication(component, path, user) {
15+
if (!user) {
16+
return <Redirect exact from={path} to="/" />;
17+
}
18+
return <Route exact path={path} component={component} />;
19+
}
20+
console.log(process.env.BASE_URL, "BASE_URL===>");
1421
return (
1522
<div className="rootContainer">
1623
<Header />
1724
<div className="container-fluid contentContainer">
1825
<Switch>
1926
<Route path="/" exact component={Login} />
20-
<Route path="/feeds" exact component={Feeds} />
21-
<Route path="/profile" component={Profile} />
27+
<Route path="/feeds" component={Feeds} />
28+
{/* <Route path="/profile" component={Profile} /> */}
29+
{enforceAuthentication(Profile, "/profile", null)}
2230
<Route path="/about" component={About} />
2331
<Route path="/contact" component={ContactUs} />
2432
</Switch>

tools/localServer.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,38 @@ server.post("/users/", function (req, res, next) {
4646
}
4747
});
4848

49+
server.get("/login/", function (req, res, next) {
50+
const query = req.query;
51+
const users = router.db.getState().users;
52+
let match;
53+
if (users) {
54+
match = users.filter((u) => {
55+
return u.id === query.userid && u.password === query.password;
56+
});
57+
if (match && match.length > 0) {
58+
res.status(200).send(match[0]);
59+
}
60+
}
61+
if (!match || match.length === 0) {
62+
const error = { errorCode: 404, error: "No record found" };
63+
res.status(404).send(error);
64+
}
65+
// next();
66+
});
67+
68+
server.get("/login/validateUserId/", function (req, res, next) {
69+
const query = req.query;
70+
const users = router.db.getState().users;
71+
let match = isUserIdUnique(users, query.userid);
72+
if (!match || match.length === 0) {
73+
const message = { message: "No match found" };
74+
res.status(200).send(message);
75+
} else {
76+
const error = { errorCode: 404, error: "User id already exist" };
77+
res.status(404).send(error);
78+
}
79+
});
80+
4981
// Use default router
5082
server.use(router);
5183

@@ -55,6 +87,16 @@ server.listen(port, () => {
5587
console.log(`JSON Server is running on port ${port}`);
5688
});
5789

90+
function isUserIdUnique(users, userid) {
91+
let match;
92+
if (users) {
93+
match = users.filter((u) => {
94+
return u.id === userid;
95+
});
96+
}
97+
return match;
98+
}
99+
58100
function validateUser(user) {
59101
if (!user.userId) return "User id is required.";
60102
if (!user.passwore) return "Password is required.";

0 commit comments

Comments
 (0)