Skip to content

Commit

Permalink
fix error where login with wrong role and refreshing page still logs in
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey committed Jun 12, 2024
1 parent f515c57 commit 73286ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
1 change: 0 additions & 1 deletion frontend/src/APIClients/AuthAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const login = async (
{ email, password },
{ withCredentials: true },
);
localStorage.setItem(AUTHENTICATED_USER_KEY, JSON.stringify(data));
return data;
} catch (error) {
return null;
Expand Down
25 changes: 13 additions & 12 deletions frontend/src/components/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import authAPIClient from "../../APIClients/AuthAPIClient";
import { HOME_PAGE, SIGNUP_PAGE } from "../../constants/Routes";
import AuthContext from "../../contexts/AuthContext";
import { AuthenticatedUser } from "../../types/AuthTypes";
import AUTHENTICATED_USER_KEY from "../../constants/AuthConstants";

type GoogleResponse = GoogleLoginResponse | GoogleLoginResponseOffline;

Expand All @@ -31,15 +32,24 @@ const Login = (): React.ReactElement => {
const searchParams = new URLSearchParams(location.search);
const role = searchParams.get("role");

if (authenticatedUser) {
return <Redirect to={HOME_PAGE} />;
}

if (!role || !["administrator", "facilitator", "learner"].includes(role)) {
// need this changed when welcome page exists
return <Redirect to="/welcome" />;
}

const onLogInClick = async () => {
const user: AuthenticatedUser = await authAPIClient.login(email, password);

if (user && user.role.toLowerCase() !== role?.toLocaleLowerCase()) {
if (user && user.role.toLowerCase() !== role.toLocaleLowerCase()) {
// change this later to not use an alert
// eslint-disable-next-line no-alert
window.alert(`Bad login. Expected ${user.role}, got ${role}`);
return;
alert(`Bad login. Expected ${user.role}, got ${role}`);
}
localStorage.setItem(AUTHENTICATED_USER_KEY, JSON.stringify(user));
setAuthenticatedUser(user);
};

Expand All @@ -54,15 +64,6 @@ const Login = (): React.ReactElement => {
setAuthenticatedUser(user);
};

if (authenticatedUser) {
return <Redirect to={HOME_PAGE} />;
}

if (!role || !["administrator", "facilitator", "learner"].includes(role)) {
// need this changed when welcome page exists
return <Redirect to="/welcome" />;
}

return (
<div style={{ textAlign: "center" }}>
<h1>{capitalizeFirstLetter(role)} Login</h1>
Expand Down

0 comments on commit 73286ca

Please sign in to comment.