-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathindex.tsx
More file actions
63 lines (54 loc) · 1.9 KB
/
index.tsx
File metadata and controls
63 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { NavigateLink } from 'components';
import { UnauthorizedLayout } from 'layouts/UnauthorizedLayout';
import { useAppDispatch } from 'hooks';
import { ROUTES } from 'routes';
import { useGoogleCallbackMutation } from 'services/auth';
import { AuthErrorMessage } from 'App/AuthErrorMessage';
import { Loading } from 'App/Loading';
import { setAuthData } from 'App/slice';
export const LoginByGoogleCallback: React.FC = () => {
const { t } = useTranslation();
const [searchParams] = useSearchParams();
const navigate = useNavigate();
const code = searchParams.get('code');
const state = searchParams.get('state');
const [isInvalidCode, setIsInvalidCode] = useState(false);
const dispatch = useAppDispatch();
const [googleCallback] = useGoogleCallbackMutation();
const checkCode = () => {
if (code && state) {
googleCallback({ code, state })
.unwrap()
.then(({ creds: { token } }) => {
dispatch(setAuthData({ token }));
navigate('/');
})
.catch(() => {
setIsInvalidCode(true);
});
}
};
useEffect(() => {
if (code && state) {
checkCode();
} else {
setIsInvalidCode(true);
}
}, []);
if (isInvalidCode)
return (
<UnauthorizedLayout>
<AuthErrorMessage title={t('auth.authorization_failed')}>
<NavigateLink href={ROUTES.BASE}>{t('auth.try_again')}</NavigateLink>
</AuthErrorMessage>
</UnauthorizedLayout>
);
return (
<UnauthorizedLayout>
<Loading />;
</UnauthorizedLayout>
);
};