-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e600e66
commit 0b8030d
Showing
8 changed files
with
442 additions
and
123 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,86 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
// import { useRouter } from 'next/navigation'; | ||
import TextInput from '@/components/TextInput'; | ||
import { StyledButton, StyledForm } from '@/components/TextInput/styles'; | ||
import COLORS from '@/styles/colors'; | ||
import { H2, P3 } from '@/styles/text'; | ||
import { useAuth } from '../../../utils/AuthProvider'; | ||
|
||
export default function Login() { | ||
const { signIn } = useAuth(); // Use `signIn` function from AuthProvider | ||
const [email, setEmail] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
const router = useRouter(); | ||
const [showPassword, setShowPassword] = useState(false); | ||
const [invalidEmailError, setInvalidEmailError] = useState(''); | ||
const [invalidPasswordError, setInvalidPasswordError] = useState(''); | ||
|
||
const isFormValid = email && password; | ||
|
||
// const { push } = useRouter(); | ||
const handleLogin = async () => { | ||
// Define handleLogin | ||
try { | ||
await signIn(email, password); | ||
router.push('/'); // Redirect to the home page on success | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
console.error('Login Error:', error.message); | ||
const { error } = await signIn(email, password); | ||
// push('/'); | ||
|
||
if (error) { | ||
// Match error messages from Supabase | ||
if (error.message.includes('Invalid login credentials')) { | ||
setInvalidEmailError('Invalid email address'); | ||
setInvalidPasswordError('Invalid password'); | ||
} | ||
return; | ||
} | ||
|
||
// Clear errors on success | ||
setInvalidEmailError(''); | ||
setInvalidPasswordError(''); | ||
} catch (err) { | ||
console.error('Login Error:', err); | ||
setInvalidEmailError('An unexpected error occurred. Please try again.'); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<input | ||
name="email" | ||
onChange={e => setEmail(e.target.value)} | ||
value={email} | ||
placeholder="Email" | ||
/> | ||
{/* Email input*/} | ||
<input | ||
type="password" | ||
name="password" | ||
onChange={e => setPassword(e.target.value)} | ||
value={password} | ||
placeholder="Password" | ||
/> | ||
<button type="button" onClick={handleLogin}> | ||
Sign in | ||
</button>{' '} | ||
{/* Sign in button */} | ||
</> | ||
<StyledForm onSubmit={handleLogin}> | ||
<div style={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}> | ||
<H2 style={{ color: COLORS.shrub }}>Log In</H2> | ||
<div> | ||
<TextInput | ||
id="email-input" | ||
type="email" | ||
label="Email" | ||
onChange={setEmail} | ||
value={email} | ||
error={!!invalidEmailError} | ||
/> | ||
{/* Email input*/} | ||
<P3 $color={COLORS.errorRed}>{invalidEmailError}</P3> | ||
</div> | ||
<div> | ||
<TextInput | ||
id="password-input" | ||
label="Password" | ||
type="password" | ||
onChange={setPassword} | ||
value={password} | ||
isVisible={showPassword} | ||
toggleVisibility={() => setShowPassword(!showPassword)} | ||
error={!!invalidPasswordError} | ||
/> | ||
<P3 $color={COLORS.errorRed}>{invalidPasswordError}</P3> | ||
{/* Password input*/} | ||
</div> | ||
<StyledButton | ||
type="button" | ||
onClick={handleLogin} | ||
disabled={!isFormValid} | ||
> | ||
Log in | ||
</StyledButton>{' '} | ||
{/* Sign in button */} | ||
</div> | ||
</StyledForm> | ||
); | ||
} |
This file contains 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
Oops, something went wrong.