-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js): Introduce experimental support for Google One Tap (#3176
) (#3196) * feat(clerk-js): Introduce experimental support for Google One Tap (#3176) * chore(clerk-js): format * chore(clerk-js): Replace with useCore* hooks * chore(clerk-js): Update tests snapshots * chore(clerk-js): Make linter happy * chore(clerk-js): Use `useUserContext` instead of `useCoreUser` * fix(clerk-js): Reprompt google one tap only when user id changes
- Loading branch information
1 parent
e97c925
commit 4cf2a21
Showing
24 changed files
with
344 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
'@clerk/nextjs': minor | ||
'@clerk/clerk-react': minor | ||
'@clerk/types': minor | ||
--- | ||
|
||
Introduce experimental support for Google One Tap | ||
- React Component `<__experimental_GoogleOneTap/>` | ||
- JS `clerk.__experimental_mountGoogleOneTap(node,props)` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/clerk-js/src/ui/components/GoogleOneTap/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { OneTapProps } from '@clerk/types'; | ||
import React from 'react'; | ||
|
||
import { withCoreSessionSwitchGuard } from '../../contexts'; | ||
import { Flow } from '../../customizables'; | ||
import { Route, Switch } from '../../router'; | ||
import { OneTapStart } from './one-tap-start'; | ||
|
||
function OneTapRoutes(): JSX.Element { | ||
return ( | ||
<Route path='one-tap'> | ||
<Flow.Root flow='oneTap'> | ||
<Switch> | ||
<Route index> | ||
<OneTapStart /> | ||
</Route> | ||
</Switch> | ||
</Flow.Root> | ||
</Route> | ||
); | ||
} | ||
|
||
OneTapRoutes.displayName = 'OneTap'; | ||
|
||
export const OneTap: React.ComponentType<OneTapProps> = withCoreSessionSwitchGuard(OneTapRoutes); |
84 changes: 84 additions & 0 deletions
84
packages/clerk-js/src/ui/components/GoogleOneTap/one-tap-start.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useUserContext } from '@clerk/shared/react'; | ||
import { useEffect } from 'react'; | ||
|
||
import { clerkInvalidFAPIResponse } from '../../../core/errors'; | ||
import type { GISCredentialResponse } from '../../../utils/one-tap'; | ||
import { loadGIS } from '../../../utils/one-tap'; | ||
import { useCoreClerk, useCoreSignIn, useEnvironment, useGoogleOneTapContext } from '../../contexts'; | ||
import { withCardStateProvider } from '../../elements'; | ||
import { useFetch } from '../../hooks'; | ||
import { useSupportEmail } from '../../hooks/useSupportEmail'; | ||
|
||
function _OneTapStart(): JSX.Element | null { | ||
const clerk = useCoreClerk(); | ||
const signIn = useCoreSignIn(); | ||
const user = useUserContext(); | ||
const environment = useEnvironment(); | ||
|
||
const supportEmail = useSupportEmail(); | ||
const ctx = useGoogleOneTapContext(); | ||
|
||
async function oneTapCallback(response: GISCredentialResponse) { | ||
try { | ||
const res = await signIn.__experimental_authenticateWithGoogleOneTap({ | ||
token: response.credential, | ||
}); | ||
|
||
switch (res.status) { | ||
case 'complete': | ||
await clerk.setActive({ | ||
session: res.createdSessionId, | ||
}); | ||
break; | ||
// TODO-ONETAP: Add a new case in order to handle the `missing_requirements` status and the PSU flow | ||
default: | ||
clerkInvalidFAPIResponse(res.status, supportEmail); | ||
break; | ||
} | ||
} catch (err) { | ||
/** | ||
* Currently it is not possible to display an error in the UI. | ||
* As a fallback we simply open the SignIn modal for the user to sign in. | ||
*/ | ||
clerk.openSignIn(); | ||
} | ||
} | ||
|
||
const environmentClientID = environment.displayConfig.googleOneTapClientId; | ||
const shouldLoadGIS = !user?.id && !!environmentClientID; | ||
|
||
/** | ||
* Prevent GIS from initializing multiple times | ||
*/ | ||
const { data: google } = useFetch(shouldLoadGIS ? loadGIS : undefined, 'google-identity-services-script', { | ||
onSuccess(google) { | ||
google.accounts.id.initialize({ | ||
client_id: environmentClientID!, | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
callback: oneTapCallback, | ||
itp_support: true, | ||
cancel_on_tap_outside: ctx.cancelOnTapOutside, | ||
auto_select: false, | ||
use_fedcm_for_prompt: true, | ||
}); | ||
|
||
google.accounts.id.prompt(); | ||
}, | ||
}); | ||
|
||
// Trigger only on mount/unmount. Above we handle the logic for the initial fetch + initialization | ||
useEffect(() => { | ||
if (google && !user?.id) { | ||
google.accounts.id.prompt(); | ||
} | ||
return () => { | ||
if (google) { | ||
google.accounts.id.cancel(); | ||
} | ||
}; | ||
}, [user?.id]); | ||
|
||
return null; | ||
} | ||
|
||
export const OneTapStart = withCardStateProvider(_OneTapStart); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.