|
| 1 | +import { loadScript, log } from '@guardian/libs'; |
| 2 | +import { useEffect } from 'react'; |
| 3 | +import { useIsSignedIn } from '../lib/useAuthStatus'; |
| 4 | + |
| 5 | +const getGSIConfiguration = (): { clientId: string; loginUri: string } => { |
| 6 | + switch (window.guardian.config.stage) { |
| 7 | + case 'PROD': |
| 8 | + return { |
| 9 | + clientId: 'PROD CLIENT ID', |
| 10 | + loginUri: 'https://profile.thegulocal.com/signin/google', |
| 11 | + }; |
| 12 | + case 'CODE': |
| 13 | + return { |
| 14 | + clientId: 'CODE CLIENT ID', |
| 15 | + loginUri: 'https://profile.thegulocal.com/signin/google', |
| 16 | + }; |
| 17 | + default: |
| 18 | + return { |
| 19 | + clientId: |
| 20 | + '774465807556-pkevncqpfs9486ms0bo5q1f2g9vhpior.apps.googleusercontent.com', |
| 21 | + loginUri: 'https://profile.thegulocal.com/signin/google', |
| 22 | + }; |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +const initializeGoogleOneTap = () => (response: { credential: string }) => { |
| 27 | + const { credential } = response; |
| 28 | + |
| 29 | + const queryParams = new URLSearchParams(); |
| 30 | + queryParams.append('got', credential); |
| 31 | + queryParams.append('returnUrl', window.location.href); |
| 32 | + |
| 33 | + window.location.replace( |
| 34 | + `https://profile.thegulocal.com/signin/google?${queryParams.toString()}`, |
| 35 | + ); |
| 36 | +}; |
| 37 | + |
| 38 | +type PromptMomentNotification = { |
| 39 | + isSkippedMoment: () => boolean; |
| 40 | + isDismissedMoment: () => boolean; |
| 41 | + getDismissedReason: () => |
| 42 | + | 'credential_returned' |
| 43 | + | 'cancel_called' |
| 44 | + | 'flow_restarted'; |
| 45 | + getMomentType: () => 'display' | 'skipped' | 'dismissed'; |
| 46 | +}; |
| 47 | + |
| 48 | +export type GoogleIdentityService = { |
| 49 | + initialize: (config: { |
| 50 | + client_id: string; |
| 51 | + login_uri: string; |
| 52 | + callback: (response: { credential: string }) => void; |
| 53 | + auto_select?: boolean; |
| 54 | + cancel_on_tap_outside?: boolean; |
| 55 | + use_fedcm_for_prompt?: boolean; |
| 56 | + }) => void; |
| 57 | + prompt: ( |
| 58 | + callback: (momentNotification: PromptMomentNotification) => void, |
| 59 | + ) => void; |
| 60 | +}; |
| 61 | + |
| 62 | +const loadGSI = async (): Promise<GoogleIdentityService> => { |
| 63 | + log('identity', 'Loading Google Sign-in Services (GSI)'); |
| 64 | + // TODO: Can we invoke the built-in FedCM API instead of using GSI? |
| 65 | + // This would reduce our dpenedency on a third-party library and all of the privacy |
| 66 | + // implications that come with it. It would also save loading ~80KB of JS. |
| 67 | + await loadScript('https://accounts.google.com/gsi/client').catch((e) => { |
| 68 | + throw new Error( |
| 69 | + `Failed to initialize Google One Tap: failed to load GSI`, |
| 70 | + { cause: e }, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + if (!window.google?.accounts?.id) { |
| 75 | + throw new Error('Failed to initialize Google One Tap: GSI not found'); |
| 76 | + } |
| 77 | + |
| 78 | + log('identity', 'Loaded Google Sign-in Services (GSI)'); |
| 79 | + return window.google.accounts.id; |
| 80 | +}; |
| 81 | + |
| 82 | +export const GoogleOneTap = () => { |
| 83 | + const isSignedIn = useIsSignedIn(); |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + if (isSignedIn === true) { |
| 87 | + log( |
| 88 | + 'identity', |
| 89 | + 'User is already signed in, skipping Google One Tap initialization', |
| 90 | + ); |
| 91 | + return; |
| 92 | + } else if (isSignedIn === 'Pending') { |
| 93 | + // If the auth status is still pending, we don't want to initialize Google One Tap yet. |
| 94 | + log( |
| 95 | + 'identity', |
| 96 | + 'User auth state is still pending, delaying Google One Tap initialization', |
| 97 | + ); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const { clientId, loginUri } = getGSIConfiguration(); |
| 102 | + |
| 103 | + void loadGSI().then((gsi) => { |
| 104 | + log('identity', 'Initializing Google One Tap', { |
| 105 | + clientId, |
| 106 | + loginUri, |
| 107 | + }); |
| 108 | + |
| 109 | + gsi.initialize({ |
| 110 | + client_id: clientId, |
| 111 | + login_uri: loginUri, |
| 112 | + callback: initializeGoogleOneTap, |
| 113 | + auto_select: true, |
| 114 | + cancel_on_tap_outside: false, |
| 115 | + use_fedcm_for_prompt: true, |
| 116 | + }); |
| 117 | + |
| 118 | + log('identity', 'Requesting Google One Tap prompt'); |
| 119 | + gsi.prompt((notifcation) => { |
| 120 | + // TODO: Handle tracking of the prompt moment notification. Ophan? |
| 121 | + log('identity', 'Google One Tap prompt notification received', { |
| 122 | + isSkippedMoment: notifcation.isSkippedMoment(), |
| 123 | + isDismissedMoment: notifcation.isDismissedMoment(), |
| 124 | + dismissedReason: notifcation.getDismissedReason(), |
| 125 | + momentType: notifcation.getMomentType(), |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + }, [isSignedIn]); |
| 130 | + |
| 131 | + return <></>; |
| 132 | +}; |
0 commit comments