Skip to content

Commit 9abfba7

Browse files
committed
Small changes made
1 parent 4f9d592 commit 9abfba7

1 file changed

Lines changed: 24 additions & 35 deletions

File tree

src/frontend/src/pages/LoginPage/Login.tsx

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* This file is part of NER's FinishLine and licensed under GNU AGPLv3.
33
* See the LICENSE file in the repository root folder for details.
44
*/
5+
56
import { FormEvent, useState } from 'react';
6-
import { useHistory } from 'react-router-dom';
7+
import { Redirect, useHistory } from 'react-router-dom';
78
import { useToggleTheme } from '../../hooks/theme.hooks';
89
import { useAuth } from '../../hooks/auth.hooks';
910
import { routes } from '../../utils/routes';
@@ -24,6 +25,17 @@ const Login = () => {
2425
const auth = useAuth();
2526
const organizationContext = useOrganization();
2627

28+
if (!auth.user && !auth.triedCurrent) {
29+
auth.signInCurrent();
30+
return <LoadingIndicator />;
31+
}
32+
33+
if (auth.isLoading) return <LoadingIndicator />;
34+
35+
if (auth.user) {
36+
return <Redirect to={routes.HOME} />;
37+
}
38+
2739
/**
2840
* Produce the path of the page redirected from the login page.
2941
* @param queryArgs the query args sent from the login page, containing page, value1, value2, ..., and other args
@@ -44,69 +56,46 @@ const Login = () => {
4456
return `${pathString}?${queryArgs.toString()}`;
4557
};
4658

47-
useEffect(() => {
48-
if (!auth.triedCurrent) {
49-
auth.signInCurrent();
50-
}
51-
}, [auth.triedCurrent, auth]);
52-
53-
useEffect(() => {
54-
if (!auth.isLoading && auth.user) {
55-
if (!query.has('page')) {
56-
history.replace(routes.HOME);
57-
} else {
58-
history.replace(redirectQueryArgsToPath(query));
59-
}
59+
const redirectAfterLogin = () => {
60+
if (!query.has('page')) {
61+
history.push(routes.HOME);
62+
} else {
63+
history.push(redirectQueryArgsToPath(query));
6064
}
61-
}, [auth.isLoading, auth.user, history, query]);
65+
};
6266

6367
const devFormSubmit = async (e: FormEvent) => {
6468
e.preventDefault();
6569
const authedUser = await auth.devSignin(devUserId);
66-
if (!authedUser) return;
67-
6870
if (authedUser.defaultTheme && authedUser.defaultTheme.toLocaleLowerCase() !== theme.activeTheme) {
6971
theme.toggleTheme();
7072
}
7173
if (authedUser.organizations.length > 0) {
7274
const [defaultOrganization] = authedUser.organizations;
7375
organizationContext.selectOrganization(defaultOrganization);
7476
}
75-
76-
if (!query.has('page')) {
77-
history.replace(routes.HOME);
78-
} else {
79-
history.replace(redirectQueryArgsToPath(query));
80-
}
77+
redirectAfterLogin();
8178
};
8279

8380
const verifyLogin = async (response: CredentialResponse) => {
84-
if (!response.credential) throw new Error('Failed to get credentials');
85-
81+
if (!response.credential) {
82+
throw new Error('Failed to get credentials');
83+
}
8684
const authedUser = await auth.signin(response.credential);
87-
8885
if (authedUser.defaultTheme && authedUser.defaultTheme !== theme.activeTheme.toUpperCase()) {
8986
theme.toggleTheme();
9087
}
9188
if (authedUser.organizations.length > 0) {
9289
const [defaultOrganization] = authedUser.organizations;
9390
organizationContext.selectOrganization(defaultOrganization);
9491
}
95-
96-
if (!query.has('page')) {
97-
history.replace(routes.HOME);
98-
} else {
99-
history.replace(redirectQueryArgsToPath(query));
100-
}
92+
redirectAfterLogin();
10193
};
10294

10395
const handleFailure = () => {
10496
console.log('Failed to login');
10597
};
10698

107-
if (auth.isLoading) return <LoadingIndicator />;
108-
if (auth.user) return null;
109-
11099
return (
111100
<LoginPage
112101
devSetUser={setDevUserId}

0 commit comments

Comments
 (0)