Skip to content

Commit a8f51b9

Browse files
committed
#3895 login redirect fix
1 parent c805630 commit a8f51b9

1 file changed

Lines changed: 35 additions & 24 deletions

File tree

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
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-
6-
import { FormEvent, useState } from 'react';
5+
import { FormEvent, useEffect, useState } from 'react';
76
import { useHistory } from 'react-router-dom';
87
import { useToggleTheme } from '../../hooks/theme.hooks';
98
import { useAuth } from '../../hooks/auth.hooks';
@@ -14,9 +13,6 @@ import { useQuery } from '../../hooks/utils.hooks';
1413
import { useOrganization } from '../../hooks/organizations.hooks';
1514
import { CredentialResponse } from '@react-oauth/google';
1615

17-
/**
18-
* Page for unauthenticated users to do login.
19-
*/
2016
const Login = () => {
2117
const [devUserId, setDevUserId] = useState('');
2218
const history = useHistory();
@@ -25,20 +21,12 @@ const Login = () => {
2521
const auth = useAuth();
2622
const organizationContext = useOrganization();
2723

28-
if (auth.isLoading) return <LoadingIndicator />;
29-
30-
/**
31-
* Produce the path of the page redirected from the login page.
32-
* @param queryArgs the query args sent from the login page, containing page, value1, value2, ..., and other args
33-
* @returns the path, with args, redirected to
34-
*/
3524
const redirectQueryArgsToPath = (queryArgs: URLSearchParams): string => {
3625
const pageName: string = queryArgs.get('page')!;
3726
queryArgs.delete('page');
3827

3928
const intermediatePathValues: string[] = [];
4029
for (let valueIdx = 1; queryArgs.has(`value${valueIdx}`); valueIdx++) {
41-
// collect all the &valueX=... args, in order, from login query args
4230
intermediatePathValues.push(`/${queryArgs.get(`value${valueIdx}`)!}`);
4331
queryArgs.delete(`value${valueIdx}`);
4432
}
@@ -47,46 +35,69 @@ const Login = () => {
4735
return `${pathString}?${queryArgs.toString()}`;
4836
};
4937

50-
const redirectAfterLogin = () => {
51-
if (!query.has('page')) {
52-
history.push(routes.HOME);
53-
} else {
54-
history.push(redirectQueryArgsToPath(query));
38+
useEffect(() => {
39+
if (!auth.triedCurrent) {
40+
auth.signInCurrent();
5541
}
56-
};
42+
}, [auth.triedCurrent, auth]);
43+
44+
useEffect(() => {
45+
if (!auth.isLoading && auth.user) {
46+
if (!query.has('page')) {
47+
history.replace(routes.HOME);
48+
} else {
49+
history.replace(redirectQueryArgsToPath(query));
50+
}
51+
}
52+
}, [auth.isLoading, auth.user, history, query]);
5753

5854
const devFormSubmit = async (e: FormEvent) => {
5955
e.preventDefault();
6056
const authedUser = await auth.devSignin(devUserId);
57+
if (!authedUser) return;
58+
6159
if (authedUser.defaultTheme && authedUser.defaultTheme.toLocaleLowerCase() !== theme.activeTheme) {
6260
theme.toggleTheme();
6361
}
6462
if (authedUser.organizations.length > 0) {
6563
const [defaultOrganization] = authedUser.organizations;
6664
organizationContext.selectOrganization(defaultOrganization);
6765
}
68-
redirectAfterLogin();
66+
67+
if (!query.has('page')) {
68+
history.replace(routes.HOME);
69+
} else {
70+
history.replace(redirectQueryArgsToPath(query));
71+
}
6972
};
7073

7174
const verifyLogin = async (response: CredentialResponse) => {
72-
if (!response.credential) {
73-
throw new Error('Failed to get credentials');
74-
}
75+
if (!response.credential) throw new Error('Failed to get credentials');
76+
7577
const authedUser = await auth.signin(response.credential);
78+
7679
if (authedUser.defaultTheme && authedUser.defaultTheme !== theme.activeTheme.toUpperCase()) {
7780
theme.toggleTheme();
7881
}
7982
if (authedUser.organizations.length > 0) {
8083
const [defaultOrganization] = authedUser.organizations;
8184
organizationContext.selectOrganization(defaultOrganization);
8285
}
83-
redirectAfterLogin();
86+
87+
if (!query.has('page')) {
88+
history.replace(routes.HOME);
89+
} else {
90+
history.replace(redirectQueryArgsToPath(query));
91+
}
8492
};
8593

8694
const handleFailure = () => {
8795
console.log('Failed to login');
8896
};
8997

98+
if (auth.isLoading) return <LoadingIndicator />;
99+
if (auth.user) return null;
100+
90101
return (
91102
<LoginPage
92103
devSetUser={setDevUserId}

0 commit comments

Comments
 (0)