Created
October 6, 2024 11:57
-
-
Save paulweezydesign/fe06462c3d61b5c5b508a89c26375175 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 Signup = () => { | |
const [email, setEmail] = useState(''); | |
const [password, setPassword] = useState(''); | |
const [confirmPassword, setConfirmPassword] = useState(''); | |
const [error, setError] = useState(null); | |
const { signup } = useContext(AuthContext); | |
const navigate = useNavigate(); | |
const handleSubmit = async (event) => { | |
event.preventDefault(); | |
if (password !== confirmPassword) { | |
setError('Passwords do not match'); | |
return; | |
} | |
try { | |
await signup(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">Sign up</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> | |
<div className="mb-4"> | |
<label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="confirmPassword"> | |
Confirm 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="confirmPassword" | |
type="password" | |
value={confirmPassword} | |
onChange={(event) => setConfirmPassword(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" | |
> | |
Sign up | |
</button> | |
</form> | |
<p className="text-gray-600 text-sm mt-4"> | |
Already have an account?{' '} | |
<a href="/login" className="text-blue-500 hover:text-blue-700"> | |
Login | |
</a> | |
</p> | |
</div> | |
); | |
}; | |
export default Signup; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment