import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useMutation } from "@tanstack/react-query"; import { Input } from '@/shadcn/components/ui/input.tsx'; import { Button } from '@/shadcn/components/ui/button.tsx'; import { Alert } from '@/shadcn/components/ui/alert.tsx'; import { Card } from '@/shadcn/components/ui/card.tsx'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/shadcn/components/ui/form.tsx'; import client from "../../honoClient"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { useEffect, useState } from "react"; import useAuth from "@/hooks/useAuth"; import { TbArrowNarrowRight } from "react-icons/tb"; import amatilogo from "@/assets/logos/amati-logo.png"; export const Route = createFileRoute("/login/")({ component: LoginPage, }); type FormSchema = { username: string; password: string; }; const formSchema = z.object({ username: z.string().min(1, "Kolom ini wajib diisi"), password: z.string().min(1, "Kolom ini wajib diisi"), }); export default function LoginPage() { console.log("hii"); const [errorMessage, setErrorMessage] = useState(""); const navigate = useNavigate(); const { isAuthenticated, saveAuthData } = useAuth(); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { username: "", password: "", }, }); useEffect(() => { if (isAuthenticated) { // Redirect based on user role const userRole = JSON.parse(localStorage.getItem('userRole') || '{}'); if (userRole === 'super-admin') { navigate({ to: "/users", replace: true, }); } else { navigate({ to: "/assessmentRequest", replace: true, }); } } }, [navigate, isAuthenticated]); const loginMutation = useMutation({ mutationFn: async (values: FormSchema) => { const res = await client.auth.login.$post({ form: values, }); if (res.ok) { return await res.json(); } throw res; }, onSuccess: (data) => { saveAuthData( { id: data.user.id, name: data.user.name, permissions: data.user.permissions, role: data.user.role || '', }, data.accessToken ); localStorage.setItem('userRole', JSON.stringify(data.user.role)); }, onError: async (error) => { if (error instanceof Response) { const body = await error.json(); setErrorMessage(body.message as string); return; } }, }); const handleSubmit = (values: FormSchema) => { loginMutation.mutate(values); }; return (
{/* Navbar */} {/* Background shapes */}
{/* Sign In form */}

Sign In

Baru mengenal aplikasi ini?{' '} Daftar sekarang

{errorMessage && (

{errorMessage}

)} ( Email/Username )} /> ( Kata sandi )} />

Lupa kata sandi?

); }