Skip to content

Commit bb52e3a

Browse files
committed
refactor: redirect to the right path when auth login on share page
1 parent f90e46f commit bb52e3a

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

frontend/src/app/pages/AuthorizationPage/index.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { EmptyFiller } from 'app/components/EmptyFiller';
22
import useI18NPrefix from 'app/hooks/useI18NPrefix';
33
import { getUserInfoByToken } from 'app/slice/thunks';
4-
import { useCallback, useEffect } from 'react';
4+
import { StorageKeys } from 'globalConstants';
5+
import { useEffect } from 'react';
56
import { useDispatch } from 'react-redux';
67
import { useHistory, useRouteMatch } from 'react-router';
78
import styled from 'styled-components/macro';
9+
import persistence from 'utils/persistence';
810

911
export const AuthorizationPage = () => {
1012
const dispatch = useDispatch();
@@ -13,15 +15,26 @@ export const AuthorizationPage = () => {
1315
const token = paramsMatch.params.token;
1416
const t = useI18NPrefix('authorization');
1517

16-
const toApp = useCallback(() => {
17-
history.replace('/');
18-
}, [history]);
19-
2018
useEffect(() => {
2119
if (token) {
22-
dispatch(getUserInfoByToken({ token, resolve: toApp }));
20+
dispatch(
21+
getUserInfoByToken({
22+
token,
23+
resolve: () => {
24+
const redirectUrl = persistence.session.get(
25+
StorageKeys.AuthRedirectUrl,
26+
);
27+
if (redirectUrl) {
28+
persistence.session.remove(StorageKeys.AuthRedirectUrl);
29+
window.location.href = redirectUrl;
30+
} else {
31+
history.replace('/');
32+
}
33+
},
34+
}),
35+
);
2336
}
24-
}, [token, dispatch, toApp]);
37+
}, [token, dispatch, history]);
2538
return (
2639
<Wrapper>
2740
<EmptyFiller loading title={t('processing')} />

frontend/src/app/pages/LoginPage/LoginForm.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Button, Form, Input } from 'antd';
2020
import { AuthForm } from 'app/components';
2121
import usePrefixI18N from 'app/hooks/useI18NPrefix';
2222
import { User } from 'app/slice/types';
23+
import { StorageKeys } from 'globalConstants';
2324
import React, { useCallback, useState } from 'react';
2425
import { Link, useHistory } from 'react-router-dom';
2526
import styled from 'styled-components/macro';
@@ -31,14 +32,15 @@ import {
3132
SPACE_XS,
3233
} from 'styles/StyleConstants';
3334
import { getToken } from 'utils/auth';
35+
import persistence from 'utils/persistence';
3436
import { AUTH_CLIENT_ICON_MAPPING } from './constants';
3537

3638
interface LoginFormProps {
3739
loading: boolean;
3840
loggedInUser?: User | null;
3941
oauth2Clients: Array<{ name: string; value: string }>;
4042
registerEnable?: boolean;
41-
modal?: boolean;
43+
inShare?: boolean;
4244
onLogin?: (value) => void;
4345
}
4446

@@ -47,7 +49,7 @@ export function LoginForm({
4749
loggedInUser,
4850
oauth2Clients,
4951
registerEnable = true,
50-
modal = false,
52+
inShare = false,
5153
onLogin,
5254
}: LoginFormProps) {
5355
const [switchUser, setSwitchUser] = useState(false);
@@ -65,9 +67,22 @@ export function LoginForm({
6567
setSwitchUser(true);
6668
}, []);
6769

70+
const toAuthClient = useCallback(
71+
clientUrl => () => {
72+
if (inShare) {
73+
persistence.session.save(
74+
StorageKeys.AuthRedirectUrl,
75+
window.location.href,
76+
);
77+
}
78+
window.location.href = clientUrl;
79+
},
80+
[inShare],
81+
);
82+
6883
return (
6984
<AuthForm>
70-
{logged && !switchUser && !modal ? (
85+
{logged && !switchUser && !inShare ? (
7186
<>
7287
<h2>{t('alreadyLoggedIn')}</h2>
7388
<UserPanel onClick={toApp}>
@@ -121,7 +136,7 @@ export function LoginForm({
121136
</Button>
122137
)}
123138
</Form.Item>
124-
{!modal && (
139+
{!inShare && (
125140
<Links>
126141
<LinkButton to="/forgetPassword">
127142
{t('forgotPassword')}
@@ -139,7 +154,7 @@ export function LoginForm({
139154
key={value}
140155
size="large"
141156
icon={AUTH_CLIENT_ICON_MAPPING[name.toLowerCase()]}
142-
href={value}
157+
onClick={toAuthClient(value)}
143158
block
144159
>
145160
{name}

frontend/src/app/pages/SharePage/ShareLoginModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function ShareLoginModal({ visible, onChange }) {
3838
<LoginForm
3939
loading={loading}
4040
oauth2Clients={oauth2Clients}
41-
modal={true}
41+
inShare={true}
4242
onLogin={onChange}
4343
/>
4444
</LoginWrapper>

frontend/src/globalConstants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export enum StorageKeys {
2727
AuthorizationToken = 'AUTHORIZATION_TOKEN',
2828
LoggedInUser = 'LOGGED_IN_USER',
2929
ShareClientId = 'SHARE_CLIENT_ID',
30+
AuthRedirectUrl = 'AUTH_REDIRECT_URL',
3031
Locale = 'LOCALE',
3132
Theme = 'THEME',
3233
}

0 commit comments

Comments
 (0)