diff --git a/alimento-nextjs/actions/Admin/admin-login.tsx b/alimento-nextjs/actions/Admin/admin-login.tsx new file mode 100644 index 0000000..6bc9c48 --- /dev/null +++ b/alimento-nextjs/actions/Admin/admin-login.tsx @@ -0,0 +1,30 @@ +'use server'; +import { generateAndSendOTP } from '@/lib/auth'; +import prismadb from '@/lib/prismadb'; +import { Prisma, Admin } from '@prisma/client'; + +export async function AdminVerify({ + email, +}: { + email: string; +}): Promise<{ success: boolean; error?: string; data?: Admin }> { + const exitingAdmin = await prismadb.admin.findUnique({ + where: { + email: email, + }, + }); + + if (!exitingAdmin) { + return { + success: false, + error: 'Admin does not exists', + }; + } + const resp = await generateAndSendOTP(email, 'admin'); + + if (!resp) { + return { success: false, error: 'Error occured in sending otp' }; + } + + return { success: true }; +} diff --git a/alimento-nextjs/app/admin/[adminId]/page.tsx b/alimento-nextjs/app/admin/[adminId]/page.tsx index 4c21631..9231368 100644 --- a/alimento-nextjs/app/admin/[adminId]/page.tsx +++ b/alimento-nextjs/app/admin/[adminId]/page.tsx @@ -1,6 +1,6 @@ const AdminPage = () => { return ( -
hi from admin
+
hiiii
); } diff --git a/alimento-nextjs/app/admin/auth/components/login-form.tsx b/alimento-nextjs/app/admin/auth/components/login-form.tsx index 0dd9209..7e9f80c 100644 --- a/alimento-nextjs/app/admin/auth/components/login-form.tsx +++ b/alimento-nextjs/app/admin/auth/components/login-form.tsx @@ -1,15 +1,17 @@ -"use client"; +'use client'; -import * as React from "react"; -import { Button } from "@/components/ui/button"; -import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; -import { Spinner } from "@/components/ui/spinner"; -import { cn } from "@/lib/utils"; -import toast, { Toaster } from "react-hot-toast"; +import * as React from 'react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { Spinner } from '@/components/ui/spinner'; +import { OtpForm } from './otp-form'; +import { AdminVerify } from '@/actions/Admin/admin-login'; +import { cn } from '@/lib/utils'; +import toast, { Toaster } from 'react-hot-toast'; interface AdminAuthFormProps extends React.HTMLAttributes { - authType: "signup" | "login"; + authType: 'signup' | 'login'; } export function AdminLoginForm({ @@ -18,52 +20,72 @@ export function AdminLoginForm({ ...props }: AdminAuthFormProps) { const [isLoading, setIsLoading] = React.useState(false); - const [email, setEmail] = React.useState(""); + const [email, setEmail] = React.useState(''); + const [otpOpen, setOtpOpen] = React.useState(false); async function onSubmit(event: React.SyntheticEvent) { event.preventDefault(); setIsLoading(true); - setIsLoading(false); + if (!email) { + toast.error('Please enter an email.'); + setIsLoading(false); + return; + } + + try { + const res = await AdminVerify({ email }); + + if (!res.success) { + toast.error(res.error || 'Error verifying admin.'); + setIsLoading(false); + return; + } + + toast.success('Admin verified! Please enter the OTP.'); + setOtpOpen(true); + } catch (error) { + toast.error('Verification failed, please try again.'); + } finally { + setIsLoading(false); + } } return ( -
+
- -
-
-
- - setEmail(e.target.value)} - /> + {!otpOpen && ( + +
+
+ + setEmail(e.target.value)} + /> +
+
- -
- + + )} + {otpOpen && }
); } diff --git a/alimento-nextjs/app/admin/auth/components/otp-form.tsx b/alimento-nextjs/app/admin/auth/components/otp-form.tsx new file mode 100644 index 0000000..3eca5c0 --- /dev/null +++ b/alimento-nextjs/app/admin/auth/components/otp-form.tsx @@ -0,0 +1,146 @@ +'use client'; + +import { + InputOTP, + InputOTPGroup, + InputOTPSlot, +} from '@/components/ui/input-otp'; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from '@/components/ui/form'; +import { z } from 'zod'; +import { zodResolver } from '@hookform/resolvers/zod'; +import * as React from 'react'; +import { cn } from '@/lib/utils'; +import { Button } from '@/components/ui/button'; +import { Toaster, toast } from 'react-hot-toast'; +import { signIn, useSession } from 'next-auth/react'; +import { useForm } from 'react-hook-form'; +import { ChevronLeftCircleIcon } from 'lucide-react'; +import { useRouter } from 'next/navigation'; // use 'next/navigation' for Next.js 13 App Router + +interface UserAuthFormProps extends React.HTMLAttributes { + roleType: 'user' | 'seller' | 'admin'; + email: string; + setOtpOpen: (otp: boolean) => void; +} + +const FormSchema = z.object({ + pin: z + .string() + .length(6, { message: 'Your one-time password must be 6 characters.' }), +}); + +export function OtpForm({ + className, + roleType, + email, + setOtpOpen, + ...props +}: UserAuthFormProps) { + const [isLoading, setIsLoading] = React.useState(false); + const [redirectUrl, setRedirectUrl] = React.useState(null); + const { data: session } = useSession(); // Access the session + const router = useRouter(); + + const form = useForm>({ + resolver: zodResolver(FormSchema), + defaultValues: { pin: '' }, + }); + + async function onOTPSubmit(data: z.infer) { + setIsLoading(true); + + console.log(email,data,roleType) + + + const result = await signIn('credentials', { + email, + otp: data.pin, + role: roleType, + redirect: false, + }); + console.log(result) + if (!result?.ok) { + toast.error('Invalid email or OTP'); + } else { + toast.success('Welcome!'); + + // Set redirect URL based on user role + if (roleType === 'user') { + setRedirectUrl('/'); // Redirect to home for user + } else if (roleType === 'seller') { + setRedirectUrl(`/seller/${email}`); // Temporarily set to seller's page + } + else{ + setRedirectUrl(`/admin/${email}`); + } + } + setIsLoading(false); + } + + React.useEffect(() => { + if (redirectUrl && session) { + const userId = session.user?.id; + const userRole = session.user?.role; + + if (userRole === "seller" && userId) { + router.push(`/seller/${userId}`); // Redirect to seller's page + } else if (userRole === "user") { + router.push(redirectUrl); // Redirect to the specified URL or home for a user + } else if (userRole === "admin") { + router.push(`/admin/${userId}`); // Redirect to admin dashboard + } + } + }, [redirectUrl, session, router]); + + + return ( +
+ +
+ + ( + + + setOtpOpen(false)} + className="h-5 w-5" + /> + One-Time Password + + + + + + + + + + + + + + + + )} + /> + + + +
+ ); +}