Skip to content

Commit 2665d0d

Browse files
authored
feat: add callback to accept auth tokens as part of VSC login (#3117)
* add callback to accept auth tokens as part of vsc login * copy spinner * Fix minor issues
1 parent 7c1892f commit 2665d0d

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Card, Classes, Elevation, NonIdealState, Spinner, SpinnerSize } from '@blueprintjs/core';
2+
import classNames from 'classnames';
3+
import React, { useEffect } from 'react';
4+
import { useTranslation } from 'react-i18next';
5+
import { useDispatch } from 'react-redux';
6+
import { useLocation, useNavigate } from 'react-router';
7+
import SessionActions from 'src/commons/application/actions/SessionActions';
8+
import { parseQuery } from 'src/commons/utils/QueryHelper';
9+
import classes from 'src/styles/Login.module.scss';
10+
11+
const LoginVscodeCallback: React.FC = () => {
12+
const navigate = useNavigate();
13+
const dispatch = useDispatch();
14+
const location = useLocation();
15+
const { t } = useTranslation('login');
16+
17+
const { access_token: accessToken, refresh_token: refreshToken } = parseQuery(location.search);
18+
19+
useEffect(() => {
20+
if (accessToken && refreshToken) {
21+
dispatch(
22+
SessionActions.setTokens({
23+
accessToken: accessToken,
24+
refreshToken: refreshToken
25+
})
26+
);
27+
dispatch(SessionActions.fetchUserAndCourse());
28+
navigate('/welcome');
29+
}
30+
// eslint-disable-next-line react-hooks/exhaustive-deps
31+
}, []);
32+
33+
return (
34+
<div className={classNames(classes['Login'], Classes.DARK)}>
35+
<Card elevation={Elevation.FOUR}>
36+
<div>
37+
<NonIdealState
38+
description={t('Logging In')}
39+
icon={<Spinner size={SpinnerSize.LARGE} />}
40+
/>
41+
</div>
42+
</Card>
43+
</div>
44+
);
45+
};
46+
47+
// react-router lazy loading
48+
// https://reactrouter.com/en/main/route/lazy
49+
export const Component = LoginVscodeCallback;
50+
Component.displayName = 'LoginVscodeCallback';
51+
52+
export default LoginVscodeCallback;

src/routes/routerConfig.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { GuardedRoute } from './routeGuard';
2525
const Login = () => import('../pages/login/Login');
2626
const LoginPage = () => import('../pages/login/LoginPage');
2727
const LoginCallback = () => import('../pages/login/LoginCallback');
28+
const LoginVscodeCallback = () => import('../pages/login/LoginVscodeCallback');
2829
const NusLogin = () => import('../pages/login/NusLogin');
2930
const Contributors = () => import('../pages/contributors/Contributors');
3031
const GitHubCallback = () => import('../pages/githubCallback/GitHubCallback');
@@ -128,7 +129,10 @@ export const getFullAcademyRouterConfig = ({
128129
{
129130
path: 'login',
130131
lazy: Login,
131-
children: [{ path: 'callback', lazy: LoginCallback }]
132+
children: [
133+
{ path: 'callback', lazy: LoginCallback },
134+
{ path: 'vscode_callback', lazy: LoginVscodeCallback }
135+
]
132136
},
133137
{ path: 'welcome', lazy: Welcome, loader: welcomeLoader },
134138
{ path: 'courses', element: <Navigate to="/" /> },

0 commit comments

Comments
 (0)