import { createLazyFileRoute, 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"; export const Route = createLazyFileRoute("/login/")({ component: LoginPage, }); type FormSchema = { username: string; password: string; }; const formSchema = z.object({ username: z.string().min(1, "This field is required"), password: z.string().min(1, "This field is required"), }); export default function LoginPage() { const [errorMessage, setErrorMessage] = useState(""); const navigate = useNavigate(); const { isAuthenticated, saveAuthData } = useAuth(); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { username: "", password: "", }, }); useEffect(() => { if (isAuthenticated) { navigate({ to: "/dashboard", 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, }, data.accessToken ); }, onError: async (error) => { console.log("error!"); if (error instanceof Response) { const body = await error.json(); setErrorMessage(body.message as string); return; } console.log("bukan error"); }, }); const handleSubmit = (values: FormSchema) => { loginMutation.mutate(values); }; return (
Amati

Sign In

New to this app?{' '} Register now

{errorMessage && (

{errorMessage}

)} ( Email/Username )} /> ( Password )} />

Forgot Password?

); }