2024-10-30 20:49:46 +05:30
|
|
|
'use client';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-10-30 20:49:46 +05:30
|
|
|
import Link from 'next/link';
|
|
|
|
|
import { useRouter } from 'next/navigation';
|
|
|
|
|
import { useActionState, useEffect, useState } from 'react';
|
2025-03-04 17:25:46 -08:00
|
|
|
import { toast } from '@/components/toast';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-15 10:14:25 -05:00
|
|
|
import { AuthForm } from '@/components/auth-form';
|
|
|
|
|
import { SubmitButton } from '@/components/submit-button';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-15 12:18:17 -05:00
|
|
|
import { login, type LoginActionState } from '../actions';
|
2025-04-25 23:40:15 -07:00
|
|
|
import { useSession } from 'next-auth/react';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
export default function Page() {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
2024-10-30 20:49:46 +05:30
|
|
|
const [email, setEmail] = useState('');
|
2024-11-08 17:59:06 +03:00
|
|
|
const [isSuccessful, setIsSuccessful] = useState(false);
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
const [state, formAction] = useActionState<LoginActionState, FormData>(
|
|
|
|
|
login,
|
|
|
|
|
{
|
2024-10-30 20:49:46 +05:30
|
|
|
status: 'idle',
|
2024-11-15 13:00:15 -05:00
|
|
|
},
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
|
2025-04-25 23:40:15 -07:00
|
|
|
const { update: updateSession } = useSession();
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
useEffect(() => {
|
2024-10-30 20:49:46 +05:30
|
|
|
if (state.status === 'failed') {
|
2025-03-04 17:25:46 -08:00
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
description: 'Invalid credentials!',
|
|
|
|
|
});
|
2024-10-30 20:49:46 +05:30
|
|
|
} else if (state.status === 'invalid_data') {
|
2025-03-04 17:25:46 -08:00
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
description: 'Failed validating your submission!',
|
|
|
|
|
});
|
2024-10-30 20:49:46 +05:30
|
|
|
} else if (state.status === 'success') {
|
2024-11-08 17:59:06 +03:00
|
|
|
setIsSuccessful(true);
|
2025-04-25 23:40:15 -07:00
|
|
|
updateSession();
|
2024-10-11 18:00:22 +05:30
|
|
|
router.refresh();
|
|
|
|
|
}
|
2025-09-19 14:40:53 +01:00
|
|
|
}, [state.status, router, updateSession]);
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
const handleSubmit = (formData: FormData) => {
|
2024-10-30 20:49:46 +05:30
|
|
|
setEmail(formData.get('email') as string);
|
2024-10-11 18:00:22 +05:30
|
|
|
formAction(formData);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="flex h-dvh w-screen items-start justify-center bg-background pt-12 md:items-center md:pt-0">
|
|
|
|
|
<div className="flex w-full max-w-md flex-col gap-12 overflow-hidden rounded-2xl">
|
2024-10-11 18:00:22 +05:30
|
|
|
<div className="flex flex-col items-center justify-center gap-2 px-4 text-center sm:px-16">
|
2025-09-09 15:44:07 -04:00
|
|
|
<h3 className="font-semibold text-xl dark:text-zinc-50">Sign In</h3>
|
|
|
|
|
<p className="text-gray-500 text-sm dark:text-zinc-400">
|
2024-10-11 18:00:22 +05:30
|
|
|
Use your email and password to sign in
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<AuthForm action={handleSubmit} defaultEmail={email}>
|
2024-11-08 17:59:06 +03:00
|
|
|
<SubmitButton isSuccessful={isSuccessful}>Sign in</SubmitButton>
|
2025-09-09 15:44:07 -04:00
|
|
|
<p className="mt-4 text-center text-gray-600 text-sm dark:text-zinc-400">
|
2024-10-11 18:00:22 +05:30
|
|
|
{"Don't have an account? "}
|
|
|
|
|
<Link
|
|
|
|
|
href="/register"
|
|
|
|
|
className="font-semibold text-gray-800 hover:underline dark:text-zinc-200"
|
|
|
|
|
>
|
|
|
|
|
Sign up
|
|
|
|
|
</Link>
|
2024-10-30 20:49:46 +05:30
|
|
|
{' for free.'}
|
2024-10-11 18:00:22 +05:30
|
|
|
</p>
|
|
|
|
|
</AuthForm>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|