Skip to content

Commit a6e80ed

Browse files
author
Vivek Chowdhury
committed
fixed login bug
1 parent 402714e commit a6e80ed

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

src/api/LoginServices.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function validateUser(userid, password) {
66
}
77

88
async function handleResponse(response) {
9-
if (response.ok) return response.json();
9+
if (response.ok || response.status === 401) return response.json();
1010
if (response.status === 400) {
1111
// So, a server-side validation error occurred.
1212
// Server side validation returns a string error message, so parse as text instead of json.

src/app/Login/Login.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as LoginServices from "./../../api/LoginServices";
66

77
function Login(props) {
88
const [user, setUser] = useState({ userId: "", password: "" });
9+
const [error, setError] = useState("");
910

1011
const handleChange = ({ target }) => {
1112
setUser({
@@ -19,7 +20,12 @@ function Login(props) {
1920
console.log(user);
2021
LoginServices.validateUser(user.userId, user.password).then((response) => {
2122
console.log(response);
22-
props.history.push("/feeds");
23+
if (!response.error) {
24+
props.history.push("/feeds");
25+
} else {
26+
setError("Invalid User naem or Password, please try again !");
27+
// TODO: Need to display error message
28+
}
2329
});
2430
};
2531

@@ -34,6 +40,7 @@ function Login(props) {
3440
onTextChange={handleChange}
3541
{...user}
3642
onSubmit={handleSubmit}
43+
error={error}
3744
/>
3845
</div>
3946
</div>

src/app/Login/LoginForm/LoginForm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Link } from "react-router-dom";
55
function LoginForm(props) {
66
return (
77
<form className="form-horizontal" onSubmit={props.onSubmit}>
8+
{props.error && <div className="alert alert-danger">{props.error}</div>}
89
<FormInput
910
inputId="userId"
1011
inputType="text"

src/app/TechMarketShell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function TechMarketShell(props) {
2626
<Route path="/" exact component={Login} />
2727
<Route path="/feeds" component={Feeds} />
2828
{/* <Route path="/profile" component={Profile} /> */}
29-
{enforceAuthentication(Profile, "/profile", null)}
29+
{enforceAuthentication(Profile, "/profile", {})}
3030
<Route path="/about" component={About} />
3131
<Route path="/contact" component={ContactUs} />
3232
</Switch>

tools/localServer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ server.get("/login/", function (req, res, next) {
5959
}
6060
}
6161
if (!match || match.length === 0) {
62-
const error = { errorCode: 404, error: "No record found" };
63-
res.status(404).send(error);
62+
const error = { errorCode: 401, error: "No record found" };
63+
res.status(401).send(error);
6464
}
6565
// next();
6666
});
@@ -73,8 +73,8 @@ server.get("/login/validateUserId/", function (req, res, next) {
7373
const message = { message: "No match found" };
7474
res.status(200).send(message);
7575
} else {
76-
const error = { errorCode: 404, error: "User id already exist" };
77-
res.status(404).send(error);
76+
const error = { errorCode: 401, error: "User id already exist" };
77+
res.status(401).send(error);
7878
}
7979
});
8080

0 commit comments

Comments
 (0)