refactor: dynamically generated login page

This commit is contained in:
Dominik 2025-05-12 21:22:56 +02:00 committed by Dominik
parent ddcb14e564
commit 20eb6ae04a
4 changed files with 75 additions and 17 deletions

View file

@ -6,23 +6,24 @@ export default function LabeledInput({
label,
placeholder,
value,
name,
}: {
type: 'text' | 'email' | 'password';
label: string;
placeholder?: string;
value?: string;
name?: string;
}) {
const elementId = Math.random().toString(36).substring(2, 15);
return (
<div className='flex flex-col gap-1'>
<Label htmlFor={elementId}>{label}</Label>
<Label htmlFor={name}>{label}</Label>
<Input
type={type}
placeholder={placeholder}
defaultValue={value}
id={elementId}
id={name}
name={name}
/>
</div>
);

View file

@ -1,18 +1,38 @@
import { signIn } from '@/auth';
import LabeledInput from '@/components/labeled-input';
import { Button } from '@/components/ui/button';
import { AuthError } from 'next-auth';
import { redirect } from 'next/navigation';
const SIGNIN_ERROR_URL = '/error';
export default function LoginForm() {
return (
<form className='flex flex-col gap-5 w-full'>
<form
className='flex flex-col gap-5 w-full'
action={async (formData) => {
'use server';
try {
await signIn('credentials', formData);
} catch (error) {
if (error instanceof AuthError) {
return redirect(`${SIGNIN_ERROR_URL}?error=${error.type}`);
}
throw error;
}
}}
>
<LabeledInput
type='email'
label='E-Mail'
placeholder='Enter your E-Mail'
name='email'
/>
<LabeledInput
type='password'
label='Password'
placeholder='Enter your Password'
name='password'
/>
<Button
className='hover:bg-blue-600 hover:text-white'