Created
October 6, 2024 11:57
-
-
Save paulweezydesign/58815b3e0a367af81cb559575dcbc60b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, { useState, useContext } from 'react'; | |
import { AuthContext } from './AuthProvider'; | |
import { useNavigate } from 'react-router-dom'; | |
const Login = () => { | |
const [email, setEmail] = useState(''); | |
const [password, setPassword] = useState(''); | |
const [error, setError] = useState(null); | |
const { login } = useContext(AuthContext); | |
const navigate = useNavigate(); | |
const handleSubmit = async (event) => { | |
event.preventDefault(); | |
try { | |
await login(email, password); | |
} catch (error) { | |
setError(error.message); | |
} | |
}; | |
return ( | |
<div className="max-w-md mx-auto p-4 mt-10 bg-white rounded-lg shadow-md"> | |
<h2 className="text-2xl font-bold mb-4">Login</h2> | |
<form onSubmit={handleSubmit}> | |
<div className="mb-4"> | |
<label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="email"> | |
</label> | |
<input | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
id="email" | |
type="email" | |
value={email} | |
onChange={(event) => setEmail(event.target.value)} | |
required | |
/> | |
</div> | |
<div className="mb-4"> | |
<label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="password"> | |
Password | |
</label> | |
<input | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
id="password" | |
type="password" | |
value={password} | |
onChange={(event) => setPassword(event.target.value)} | |
required | |
/> | |
</div> | |
{error && ( | |
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> | |
{error} | |
</div> | |
)} | |
<button | |
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" | |
type="submit" | |
> | |
Login | |
</button> | |
</form> | |
<p className="text-gray-600 text-sm mt-4"> | |
Don't have an account?{' '} | |
<a href="/signup" className="text-blue-500 hover:text-blue-700"> | |
Sign up | |
</a> | |
</p> | |
</div> | |
); | |
}; | |
export default Login; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment