Skip to content

Commit

Permalink
added placeholders and redirects to home page
Browse files Browse the repository at this point in the history
  • Loading branch information
rachaelch3n committed Oct 9, 2024
1 parent 7b9e101 commit d387c90
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions app/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
'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) {
throw new Error(`An error occurred trying to sign up: ${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 signInWithEmail = async () => {
const handleSignInWithEmail = async () => {
const { data, error } = await supabase.auth.signInWithPassword({
email,
password,
Expand All @@ -29,6 +43,8 @@ export default function Login() {
throw new Error(`An error occurred trying to sign in: ${error}`);
}

push('/');

return data;
};

Expand All @@ -38,17 +54,19 @@ export default function Login() {
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={signInWithEmail}>
<button type="button" onClick={handleSignInWithEmail}>
Sign in
</button>
</>
Expand Down

0 comments on commit d387c90

Please sign in to comment.