File size: 1,632 Bytes
420bd16 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import React from 'react';
import { authService } from '../services/auth_service';
import { useAuthState } from 'react-firebase-hooks/auth';
import { auth } from '../services/firebase';
export function Login() {
const [user, loading, error] = useAuthState(auth);
const handleGoogleSignIn = async () => {
try {
await authService.signInWithGoogle();
} catch (error) {
console.error('Login failed:', error);
}
};
const handleSignOut = async () => {
try {
await authService.signOut();
} catch (error) {
console.error('Logout failed:', error);
}
};
if (loading) {
return (
<div className="login-container">
<p>Loading...</p>
</div>
);
}
if (error) {
return (
<div className="login-container">
<p>Error: {error.message}</p>
</div>
);
}
return (
<div className="login-container">
{user ? (
<div className="user-info">
<img
src={user.photoURL}
alt={user.displayName}
className="user-avatar"
/>
<div className="user-details">
<p className="user-name">{user.displayName}</p>
<p className="user-email">{user.email}</p>
<button
onClick={handleSignOut}
className="sign-out-btn"
>
Sign Out
</button>
</div>
</div>
) : (
<button
onClick={handleGoogleSignIn}
className="google-sign-in-btn"
>
Sign in with Google
</button>
)}
</div>
);
} |