|
1 | | -import { createContext } from "react"; |
2 | | - |
3 | | -interface AuthenticateUsingButton { |
4 | | - type: "button"; |
5 | | - clientId: string; |
6 | | - userInfoLabel?: string; |
7 | | - scopes?: string[]; |
8 | | - overwriteDefaultScopes?: boolean; |
9 | | -} |
10 | | - |
11 | | -interface AuthenticateUsingServer { |
12 | | - type: "server"; |
13 | | - access_token: string; |
14 | | - scopes?: string[]; |
15 | | - overwriteDefaultScopes?: boolean; |
16 | | -} |
17 | | - |
18 | | -type AuthenticateMethod = AuthenticateUsingButton | AuthenticateUsingServer; |
19 | | - |
20 | | -export type AuthenticationState = |
21 | | - | "AUTHENTICATING" |
22 | | - | "AUTH_ERROR" |
23 | | - | "AUTH_SUCCESS" |
24 | | - | "NOT_AUTHENTICATED"; |
25 | | - |
26 | | -export class GoogleContext { |
27 | | - protected authButton?: string | HTMLElement = undefined; |
28 | | - |
29 | | - setAuthButton(button: HTMLElement) { |
30 | | - this.authButton = button; |
31 | | - } |
32 | | - |
33 | | - authenticate(method: AuthenticateMethod): Promise<null> { |
34 | | - const authContainer = this.authButton; |
35 | | - return new Promise((res, rej) => { |
36 | | - window.gapi.analytics.auth.once("error", (res) => { |
37 | | - rej(res); |
38 | | - }); |
39 | | - window.gapi.analytics.auth.once("signIn", () => { |
40 | | - res(null); |
41 | | - }); |
42 | | - if (method.type === "server") { |
43 | | - const rest = window.gapi.analytics.auth.authorize({ |
44 | | - scopes: method.scopes, |
45 | | - overwriteDefaultScopes: method.overwriteDefaultScopes, |
46 | | - serverAuth: { |
47 | | - access_token: method.access_token, |
48 | | - }, |
49 | | - }); |
50 | | - console.log(rest); |
51 | | - res(null); |
52 | | - } else { |
53 | | - console.log(method.clientId); |
54 | | - window.gapi.analytics.auth.authorize({ |
55 | | - container: authContainer, |
56 | | - clientid: method.clientId, |
57 | | - scopes: method.scopes, |
58 | | - overwriteDefaultScopes: method.overwriteDefaultScopes, |
59 | | - userInfoLabel: method.userInfoLabel, |
60 | | - }); |
61 | | - } |
62 | | - }); |
63 | | - } |
64 | | -} |
65 | | - |
66 | | -export const googleContext = new GoogleContext(); |
67 | | - |
68 | | -export type GoogleAnalyticsContextContent = [AuthenticationState]; |
69 | | -const GoogleAnalyticsContext = |
70 | | - createContext<GoogleAnalyticsContextContent | null>(null); |
| 1 | +import { createContext } from 'react'; |
| 2 | + |
| 3 | +// INITIALIZED -> LOADING -> PROCESSING -> READY |
| 4 | +// -> AUTHENTICATING -> AUTH_ERROR/AUTH_SUCCESS |
| 5 | +export type GoogleAnalyticsState = |
| 6 | + | 'INITIALIZED' |
| 7 | + | 'LOADING' |
| 8 | + | 'PROCESSING' |
| 9 | + | 'READY' |
| 10 | + | 'AUTHENTICATING' |
| 11 | + | 'SIGNOUT' |
| 12 | + | 'AUTH_ERROR' |
| 13 | + | 'AUTH_SUCCESS'; |
| 14 | + |
| 15 | +export type GoogleAnalyticsContextContent = [GoogleAnalyticsState, (el: HTMLElement | null) => void]; |
| 16 | +const GoogleAnalyticsContext = createContext<GoogleAnalyticsContextContent | null>(null); |
71 | 17 | export default GoogleAnalyticsContext; |
0 commit comments