-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] User Auth: Created a simple login page that supports sign up a…
…nd sign in. (#5)
- Loading branch information
1 parent
c28c283
commit 8fe8603
Showing
1 changed file
with
74 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import supabase from '../../../api/supabase/createClient'; | ||
|
||
export default function Login() { | ||
const [email, setEmail] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
const { push } = useRouter(); | ||
|
||
const handleSignUp = async () => { | ||
const { data, error } = await supabase.auth.signUp({ | ||
email, | ||
password, | ||
}); | ||
|
||
if (error) { | ||
// Check if the error is due to an already registered email | ||
if (error.message.includes('User already registered')) { | ||
throw new Error( | ||
'This email is already registered. Please try signing in instead.', | ||
); | ||
} else { | ||
throw new Error( | ||
`An error occurred trying to sign up: ${error.message}`, | ||
); | ||
} | ||
} | ||
|
||
push('/'); | ||
|
||
return data; | ||
}; | ||
|
||
const handleSignInWithEmail = async () => { | ||
const { data, error } = await supabase.auth.signInWithPassword({ | ||
email, | ||
password, | ||
}); | ||
|
||
if (error) { | ||
throw new Error(`An error occurred trying to sign in: ${error}`); | ||
} | ||
|
||
push('/'); | ||
|
||
return data; | ||
}; | ||
|
||
return ( | ||
<> | ||
<input | ||
name="email" | ||
onChange={e => setEmail(e.target.value)} | ||
value={email} | ||
placeholder="Email" | ||
/> | ||
<input | ||
type="password" | ||
name="password" | ||
onChange={e => setPassword(e.target.value)} | ||
value={password} | ||
placeholder="Password" | ||
/> | ||
<button type="button" onClick={handleSignUp}> | ||
Sign up | ||
</button> | ||
<button type="button" onClick={handleSignInWithEmail}> | ||
Sign in | ||
</button> | ||
</> | ||
); | ||
} |