11import { log } from '@guardian/libs' ;
22import { useEffect } from 'react' ;
3+ import { useAB } from '../lib/useAB' ;
34import { useIsSignedIn } from '../lib/useAuthStatus' ;
45import type { StageType } from '../types/config' ;
56
@@ -50,8 +51,18 @@ type CredentialsProvider = {
5051
5152export const GoogleOneTap = ( ) => {
5253 const isSignedIn = useIsSignedIn ( ) ;
54+ const abTests = useAB ( ) ;
55+ const isUserInTest = abTests ?. api . isUserInVariant (
56+ 'GoogleOneTap' ,
57+ 'variant' ,
58+ ) ;
5359
5460 useEffect ( ( ) => {
61+ // Only initialize Google One Tap if the user is in the AB test. Currently 0% of users are in the test.
62+ if ( ! isUserInTest ) return ;
63+
64+ // FedCM has no knowledge of the user's auth state, so we need to check
65+ // if the user is already signed in before initializing it.
5566 if ( isSignedIn === true ) {
5667 log (
5768 'identity' ,
@@ -67,6 +78,13 @@ export const GoogleOneTap = () => {
6778 return ;
6879 }
6980
81+ /**
82+ * Typescripts built-in DOM types do not include the full `CredentialsProvider`
83+ * interface, so we need to cast `window.navigator.credentials` to our own
84+ * `CredentialsProvider` type which includes the FedCM API.
85+ *
86+ * Related issue: https://github.com/microsoft/TypeScript/issues/60641
87+ */
7088 const credentialsProvider = window . navigator
7189 . credentials as unknown as CredentialsProvider ;
7290
@@ -94,6 +112,8 @@ export const GoogleOneTap = () => {
94112 'identity' ,
95113 'FedCM prompt failed, potentially due to user declining' ,
96114 ) ;
115+ } else {
116+ throw error ;
97117 }
98118 } )
99119 . then ( ( credentials ) => {
@@ -105,7 +125,7 @@ export const GoogleOneTap = () => {
105125 log ( 'identity' , 'No FedCM credentials received' ) ;
106126 }
107127 } ) ;
108- } , [ isSignedIn ] ) ;
128+ } , [ isSignedIn , isUserInTest ] ) ;
109129
110130 return < > </ > ;
111131} ;
0 commit comments