@@ -6,6 +6,9 @@ import { Link } from "react-router-dom";
66import { useAuthModal } from "@/context/AuthModalContext" ;
77import HeaderActions from "./HeaderActions" ;
88import Banner from "./Banner" ;
9+ import { CredentialResponse , useGoogleOneTapLogin } from "@react-oauth/google" ;
10+ import { postData } from "@/util" ;
11+ import { useAuth } from "@/context/AuthContext" ;
912
1013export const NAV_MENU = [
1114 { text : "Home" , link : "/" } ,
@@ -23,6 +26,35 @@ const Header = ({ withBanner = true }: { withBanner?: boolean }) => {
2326 closeForgotPassword,
2427 } = useAuthModal ( ) ;
2528
29+ const { login } = useAuth ( ) ;
30+
31+ useGoogleOneTapLogin ( {
32+ onSuccess : async ( response : CredentialResponse ) => {
33+ if ( response . credential ) handleGoogleLogin ( response . credential ) ;
34+ } ,
35+ onError : async ( ) => {
36+ console . log ( "Login Failed" ) ;
37+ } ,
38+ cancel_on_tap_outside : true ,
39+ use_fedcm_for_prompt : true ,
40+ } ) ;
41+
42+ const handleGoogleLogin = async ( code : string ) => {
43+ if ( ! code ) return ;
44+
45+ try {
46+ const userData = await postData ( "auth/login/google" , { code } , ( ) => { } ) ;
47+ if ( userData ) {
48+ await login ( userData ) ;
49+ }
50+ } catch ( err : unknown ) {
51+ let errorMessage = "" ;
52+ if ( err instanceof Error ) errorMessage = err . message ;
53+ // TODO: a toast to show the error: setErrorMessage(`Error sending data to server: ${errorMessage}`);
54+ console . log ( `Error sending data to server: ${ errorMessage } ` ) ;
55+ }
56+ } ;
57+
2658 return (
2759 < header >
2860 < div className = "bg-background flex items-center py-2" >
0 commit comments