Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] User Auth: Created a simple login page that supports sign up and sign in. #5

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions app/(auth)/login/page.tsx
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>
</>
);
}