Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/@react-oauth/google/src/hooks/useGoogleIdToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useGoogleOAuth } from './GoogleOAuthProvider';
import { useCallback } from 'react';

export const useGoogleIdToken = ({onSuccess}:any) => {
const { clientId, scriptLoadedSuccessfully } = useGoogleOAuth();

const login = useCallback(() => {
if (!scriptLoadedSuccessfully) {
console.error("Google script not loaded yet");
return;
}

/* global google */
(window?.google as any)?.accounts?.id?.initialize({
client_id: clientId,
callback: (response:any) => {
if (response.credential) {
onSuccess(response.credential);
}
},
});

// This opens the native Google selection prompt
(window?.google as any)?.accounts?.id?.prompt();
}, [clientId, scriptLoadedSuccessfully, onSuccess]);

return login;
};