-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1-sign-in.tsx
More file actions
102 lines (94 loc) · 2.51 KB
/
1-sign-in.tsx
File metadata and controls
102 lines (94 loc) · 2.51 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import Avatar from '@mui/material/Avatar';
import Chip from '@mui/material/Chip';
import Typography from '@mui/material/Typography';
import { MutableRefObject } from 'react';
import GsiButton from '@/components/GsiButton';
import Step from '@/components/Step';
import { driveDiscoveryUrl, driveScope } from '@/lib/drive';
import { type AuthInfo } from '@/lib/gsi';
export interface Step1SignInProps {
authInfo: AuthInfo | undefined;
setAuthInfo: (authInfo: AuthInfo | undefined) => void;
setGoogleReady: (googleReady: Promise<boolean>) => void;
error?: string;
setError: (error: string) => void;
setTokenClient: (tokenClient: google.accounts.oauth2.TokenClient) => void;
tokenCallback: MutableRefObject<
undefined | ((resp: google.accounts.oauth2.TokenResponse) => void)
>;
next: () => Promise<void>;
}
const Step1SignIn = ({
authInfo,
setAuthInfo,
setGoogleReady,
error,
setError,
setTokenClient,
tokenCallback,
next,
}: Step1SignInProps): JSX.Element => {
const onAuthInfo = async (info: AuthInfo): Promise<void> => {
setAuthInfo(info);
setGoogleReady(
new Promise(async (gResolve, gReject) => {
setTokenClient(
google.accounts.oauth2.initTokenClient({
// https://developers.google.com/identity/oauth2/web/reference/js-reference#TokenClientConfig
prompt: '',
hint: info.email,
client_id: process.env.NEXT_PUBLIC_CLIENT_ID as string,
scope: driveScope,
callback: (resp: google.accounts.oauth2.TokenResponse): void => {
if (tokenCallback.current === undefined) {
gReject();
return;
}
tokenCallback.current(resp);
},
}),
);
await new Promise((resolve, reject): void => {
gapi.load('client', { callback: resolve, onerror: reject });
});
await gapi.client.init({});
gapi.client.load(driveDiscoveryUrl);
gResolve(true);
}),
);
next();
};
return (
<Step
action={next}
actionLabel="Next"
actionShow={(): boolean => !!authInfo}
error={error}
setError={setError}>
{authInfo ? (
<Chip
avatar={
<Avatar
alt={`Picture of ${authInfo.name}`}
src={authInfo.picture}
/>
}
label={authInfo.name}
onDelete={(): void => {
setAuthInfo(undefined);
setGoogleReady(Promise.resolve(false));
}}
/>
) : (
<>
<Typography>
First sign in with a Google account which has access to the Google
Doc:
</Typography>
<GsiButton onAuthInfo={onAuthInfo} />
</>
)}
</Step>
);
};
export default Step1SignIn;