Skip to content

Commit

Permalink
Merge pull request #57 from grant-baer/login
Browse files Browse the repository at this point in the history
Login username+password validation
  • Loading branch information
candywal authored Dec 4, 2023
2 parents 094db94 + ed56fff commit 3532d55
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
33 changes: 21 additions & 12 deletions Backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,37 @@ def login():
)
else:
# Incorrect password
print("bad pwd")
return (
jsonify(
{"message": "Login failed, incorrect username or password"}
),
jsonify({"message": "Login failed, incorrect password"}),
401,
)
except DoesNotExist:
# Username does not exist
print("bad user")
return (
jsonify(
{"message": "Login failed, incorrect username or password"}
),
jsonify({"message": "Login failed, invalid username"}),
401,
)
except KeyError:
# Username or password not provided
return (
jsonify(
{"message": "Login failed, must provide username and password"}
),
400,
)
if not data["username"] and data["password"]:
return (
jsonify(
{"message": "Login failed, missing username and password"}
),
400,
)
elif not data["username"]:
return (
jsonify({"message": "Login failed, missing username"}),
400,
)
else:
return (
jsonify({"message": "Login failed, missing password"}),
400,
)
except Exception as e:
# Catch any other errors
print(f"Error during login: {str(e)}")
Expand Down
29 changes: 28 additions & 1 deletion Frontend/src/pages/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const Login = () => {
password: "",
confirmPassword: "",
});
const [usernameError, setUsernameError] = useState("");
const [pwdError, setPwdError] = useState("");

const handleChange = (e) => {
const { name, value } = e.target;
Expand All @@ -37,12 +39,33 @@ const Login = () => {
);
console.log("response", response);


// Store the token in cookie
Cookies.set("token", response.data.access_token, { expires: 7, path: "/" });

router.push("/portfolio");
return response;
} catch (error) {
console.log(error);
console.log(`error=${error},${error.response.data.message}`);
let msg = error.response.data.message.toLowerCase();
if (msg.includes("missing")) {
if (msg.includes("username")) {
setUsernameError("Please enter a username.");
setPwdError("");
}
if (msg.includes("password")) {
setPwdError("Please enter a password.");
if (!msg.includes("username")) {
setUsernameError("");
}
}
} else if (msg.includes("username")) {
setUsernameError("Username does not exist.");
setPwdError("");
} else if (msg.includes("password")) {
setPwdError("Incorrect password.");
setUsernameError("");
} else throw new Error("Unknown login error");
return false;
}
};
Expand All @@ -63,6 +86,9 @@ const Login = () => {
onChange={handleChange}
className="w-full border rounded p-2 text-gray-600 "
/>
{usernameError && (
<p className="text-red-500 text-sm">{usernameError}</p>
)}
</div>

<div className="mb-4">
Expand All @@ -76,6 +102,7 @@ const Login = () => {
onChange={handleChange}
className="w-full border rounded p-2 text-gray-600 "
/>
{pwdError && <p className="text-red-500 text-sm">{pwdError}</p>}
</div>

<button
Expand Down

0 comments on commit 3532d55

Please sign in to comment.