11import React , { useState } from "react" ;
2- import { Google } from "@/ui/icons" ;
32import { postData } from "@/util" ;
43import { useAuth } from "@/context/AuthContext" ;
54import { useAuthModal } from "@/context/AuthModalContext" ;
65import { User } from "@/types" ;
76import { Dialog , DialogContent } from "@/Components/ui/dialog" ;
87import { Button } from "@/Components/ui/button" ;
8+ import { GoogleOAuthProvider } from "@react-oauth/google" ;
9+ import GoogleLoginButton from "./GoogleLoginButton" ;
910
1011const LoginPage = ( {
1112 open,
@@ -78,8 +79,10 @@ const LoginPage = ({
7879 handleClose ( ) ;
7980 } ;
8081
81- const handleLogin = async ( ) => {
82- if ( ! email || ! password || ! isValid ) {
82+ const handleLogin = async (
83+ demoData : { email : string ; password : string } | null = null ,
84+ ) => {
85+ if ( ! demoData && ( ! email || ! password || ! isValid ) ) {
8386 setEmailError ( "Email is required" ) ;
8487 setPasswordError ( "Password is required" ) ;
8588 setIsValid ( false ) ;
@@ -89,7 +92,7 @@ const LoginPage = ({
8992 try {
9093 const userData : User = await postData (
9194 "auth/login" ,
92- { email, password } ,
95+ demoData || { email, password } ,
9396 setErrorMessage ,
9497 ) ;
9598 if ( userData ) {
@@ -103,19 +106,42 @@ const LoginPage = ({
103106 }
104107 } ;
105108
109+ const handleGoogleLogin = async ( code : string ) => {
110+ if ( ! code ) return setErrorMessage ( "Code is required" ) ;
111+
112+ try {
113+ const userData = await postData (
114+ "auth/login/google" ,
115+ { code } ,
116+ setErrorMessage ,
117+ ) ;
118+ if ( userData ) {
119+ await login ( userData ) ;
120+ handleDialogClose ( ) ;
121+ }
122+ } catch ( err : unknown ) {
123+ let errorMessage = "" ;
124+ if ( err instanceof Error ) errorMessage = err . message ;
125+ setErrorMessage ( `Error sending data to server: ${ errorMessage } ` ) ;
126+ }
127+ } ;
128+
129+ const handleDemoLogin = async ( email : string , password : string ) => {
130+ await handleLogin ( { email, password } ) ;
131+ } ;
132+
106133 return (
107134 < Dialog open = { open } onOpenChange = { handleDialogClose } modal >
108135 < DialogContent className = "px-3 py-8 w-full max-w-full h-full sm:w-auto sm:max-w-4xl sm:h-auto rounded-none sm:rounded-lg items-center" >
109136 < div className = "flex gap-8 md:p-8 md:w-3xl" >
110137 { /* Left Section */ }
111138 < div className = "flex-1 md:flex-1/2 px-2 py-3 space-y-2" >
112139 < h5 className = "flex justify-center text-3xl" > Sign In</ h5 >
113- < Button
114- variant = "ghost"
115- className = "my-2 w-full p-5 font-normal text-lg bg-foreground text-background hover:bg-foreground hover:text-background active:scale-95"
140+ < GoogleOAuthProvider
141+ clientId = { import . meta. env . VITE_GOOGLE_OAUTH_CLIENT_ID }
116142 >
117- < Google className = "size-8" /> Sign in with Google
118- </ Button >
143+ < GoogleLoginButton onLogin = { handleGoogleLogin } />
144+ </ GoogleOAuthProvider >
119145 < div className = "my-4 text-foreground/60 text-center" > OR</ div >
120146 < label className = "block" >
121147 Email:
@@ -162,7 +188,7 @@ const LoginPage = ({
162188 < Button
163189 variant = "default"
164190 className = "w-full active:scale-95"
165- onClick = { handleLogin }
191+ onClick = { ( ) => handleLogin ( ) }
166192 >
167193 Sign in
168194 </ Button >
@@ -179,6 +205,27 @@ const LoginPage = ({
179205 Sign up
180206 </ Button >
181207 </ p >
208+ < p className = "text-sm flex gap-1 items-center justify-center" >
209+ < span className = "italic" > Demo login:</ span >
210+ < Button
211+ variant = { "outline" }
212+ className = "hover:bg-primary"
213+ onClick = { ( ) =>
214+ handleDemoLogin ( "[email protected] " , "B5TTP76m2NSBbye" ) 215+ }
216+ >
217+ John Doe
218+ </ Button >
219+ < Button
220+ variant = { "outline" }
221+ className = "hover:bg-primary"
222+ onClick = { ( ) =>
223+ handleDemoLogin ( "[email protected] " , "B5TTP76m2NSBbye" ) 224+ }
225+ >
226+ Sarah Smith
227+ </ Button >
228+ </ p >
182229 </ div >
183230
184231 { /* Right Section */ }
0 commit comments