mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
151 lines
6.9 KiB
TypeScript
151 lines
6.9 KiB
TypeScript
'use client'
|
|
|
|
import { useState, FC, useEffect } from "react"
|
|
import Link from "next/link"
|
|
import { Form, Input } from "antd"
|
|
import { signIn, useSession } from "next-auth/react";
|
|
import { useRouter, useSearchParams } from "next/navigation"
|
|
|
|
import { Icons } from "@/components/Icons"
|
|
import { Button } from "@/components/ui/button"
|
|
import { Label } from "@/components/ui/label"
|
|
|
|
import { ToggleTheme } from '@/components/ToggleTheme';
|
|
import ForgotPassword from "../(dashboard)/profile/components/ForgotPassword";
|
|
import Logo from "../(dashboard)/components/Logo";
|
|
import { toast } from "sonner";
|
|
|
|
|
|
type Props = {};
|
|
|
|
const LoginPage: FC<Props> = ({ }) => {
|
|
const [isLoading, setIsLoading] = useState<boolean>(false)
|
|
const { data: session, status } = useSession();
|
|
|
|
const router = useRouter()
|
|
|
|
const searchParams = useSearchParams();
|
|
const error = searchParams!.get('error');
|
|
const callbackUrl = searchParams!.get('callbackUrl')
|
|
|
|
useEffect(() => {
|
|
if (session?.user.access_token && !error) {
|
|
router.replace(callbackUrl || '/admin')
|
|
}
|
|
}, [session, error])
|
|
|
|
const onFinish = async (values: any) => {
|
|
console.log('Success:', values);
|
|
|
|
setIsLoading(true)
|
|
signIn('credentials', {
|
|
email: values.email,
|
|
password: values.password,
|
|
redirect: false,
|
|
// callbackUrl: callbackUrl || '/admin'
|
|
}).then(res => {
|
|
console.log(res)
|
|
if (!res?.ok) {
|
|
toast.error("Oops! Something went wrong.", {
|
|
description: `(${res?.status}) ${res?.error}`,
|
|
})
|
|
setIsLoading(false)
|
|
} else {
|
|
console.log("NEXTAUTH LOGIN :", res)
|
|
// router.replace(callbackUrl || '/admin')
|
|
window.location.href = callbackUrl || '/admin'
|
|
}
|
|
}).catch(err => {
|
|
console.log(err)
|
|
toast.error("Oops! Something went wrong.", {
|
|
description: err.message as string,
|
|
})
|
|
setIsLoading(false)
|
|
})
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<section className="relative lg:p-8 h-full flex flex-col justify-center items-center w-full bg-white dark:bg-zinc-950 2xl:rounded-r-2xl">
|
|
<div className='flex justify-between sm:justify-end items-center absolute w-full top-5 lg:top-0 lg:p-8'>
|
|
<Logo className="sm:hidden" />
|
|
<ToggleTheme />
|
|
</div>
|
|
<div className="mx-auto flex w-full flex-col justify-center space-y-6 lg:w-[350px] relative">
|
|
{error &&
|
|
<div className="absolute -top-10 bg-destructive text-sm px-4 py-3 rounded-lg left-1/2 w-9/12 text-center text-white" style={{ transform: 'translate(-50%, -50%)' }}>{error}</div>
|
|
}
|
|
<div className="flex flex-col space-y-2 text-center">
|
|
<h1 className="text-2xl font-semibold tracking-tight">
|
|
Login an account
|
|
</h1>
|
|
<p className="text-sm text-zinc-500 dark:text-zinc-400">
|
|
Welcome back, sign in to continue access
|
|
</p>
|
|
</div>
|
|
<div className="grid gap-6">
|
|
<Form onFinish={onFinish}>
|
|
<div className="grid ">
|
|
<div className="grid gap-1">
|
|
<Label className="sr-only" htmlFor="email">
|
|
Email
|
|
</Label>
|
|
<Form.Item name='email' rules={[{ required: true, message: 'Please input your email!' }]} >
|
|
<Input id="email" type="email" placeholder="name@example.com" autoCapitalize="none" autoComplete="email" autoCorrect="off" className="input" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-1">
|
|
<Label className="sr-only" htmlFor="password">
|
|
Password
|
|
</Label>
|
|
<Form.Item name='password' rules={[{ required: true, message: 'Please input your password!' }]}>
|
|
<Input.Password id="password" placeholder="*yoursecretpassword" type="password" autoCapitalize="none" disabled={isLoading} className="input" />
|
|
</Form.Item>
|
|
</div>
|
|
<Button disabled={isLoading} type="submit">
|
|
{isLoading && (
|
|
<Icons.spinner className="h-4 w-4 animate-spin" />
|
|
)}
|
|
Sign In
|
|
</Button>
|
|
</div>
|
|
</Form>
|
|
<div className="relative">
|
|
<div className="absolute inset-0 flex items-center">
|
|
<span className="w-full border-t dark:border-zinc-800" />
|
|
</div>
|
|
<div className="relative flex justify-center text-xs uppercase">
|
|
<span className="bg-white dark:bg-zinc-950 px-2 text-zinc-500 dark:text-zinc-400">
|
|
Forgot Password ?
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<ForgotPassword>
|
|
<Button variant="outline" type="button" disabled={isLoading}>
|
|
{isLoading ? (
|
|
<Icons.spinner className="h-4 w-4 animate-spin" />
|
|
) : (
|
|
<i className="ti ti-key text-lg"></i>
|
|
)}{" "}
|
|
Request for Reset Password
|
|
</Button>
|
|
</ForgotPassword>
|
|
</div>
|
|
<p className="px-8 text-center text-sm text-zinc-500 dark:text-zinc-400">
|
|
By clicking continue, you agree to our{" "}
|
|
<Link href="/terms" className="underline underline-offset-4 hover:text-primary">
|
|
Terms of Service
|
|
</Link>{" "}
|
|
and{" "}
|
|
<Link href="/privacy-policy" className="underline underline-offset-4 hover:text-primary">
|
|
Privacy Policy
|
|
</Link>
|
|
.
|
|
</p>
|
|
</div>
|
|
</section>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default LoginPage; |