import { client } from '@/ui/api/client' import { AuthLayout } from '@/ui/components/auth-layout' import { Button } from '@/ui/components/ui/button' import { ShumaiLogo } from '@/ui/components/ui/icons' import { Input } from '@/ui/components/ui/input' import { Label } from '@/ui/components/ui/label' import { signIn } from '@/ui/lib/auth-client' import { useAuthStore } from '@/ui/stores/auth' import { zodResolver } from '@hookform/resolvers/zod' import { useQuery } from '@tanstack/react-query' import { createFileRoute, redirect, useNavigate } from '@tanstack/react-router' import { ArrowRight, Loader2, Lock, LogIn, Mail } from 'lucide-react' import { useEffect, useState } from 'react' import { useForm } from 'react-hook-form' import { z } from 'zod' import { m } from '@/ui/src/paraglide/messages.js' const loginSchema = z.object({ email: z.string().email('Invalid email address'), password: z.string().min(1, 'Password is required'), }) type LoginSchema = z.infer function LoginPage() { const navigate = useNavigate() const setUser = useAuthStore((state) => state.setUser) const [error, setError] = useState(null) const [loading, setLoading] = useState(false) const { data: signupInfo, isLoading: isSignupInfoLoading } = useQuery({ queryKey: ['/signup-info'], queryFn: async () => { const res = await client.api['signup-info'].$get() if (!res.ok) throw new Error('Failed to fetch signup info') return await res.json() }, }) useEffect(() => { if (signupInfo && !signupInfo.initialized) { navigate({ to: '/signup', search: { inviteCode: '' } }) } }, [signupInfo, navigate]) const isDemoEnv = signupInfo?.demoMode === true const form = useForm({ resolver: zodResolver(loginSchema), }) const onSubmit = async (data: LoginSchema) => { setLoading(true) setError(null) const { data: session, error: signInError } = await signIn.email({ email: data.email, password: data.password, }) setLoading(false) if (signInError) { setError(signInError.message || 'Invalid credentials') return } if (session) { setUser(session.user) navigate({ to: '/' }) } } if (isSignupInfoLoading) { return (
) } return (

{m.welcome_back()}

Enter your credentials to access your account.

{isDemoEnv && (

Demo Access: Use "foo@bar.com" as the email and{' '} "foo" as the password to login.

)} {error && (
{error}
)}
{form.formState.errors.email && (

{form.formState.errors.email.message}

)}
{form.formState.errors.password && (

{form.formState.errors.password.message}

)}
Don't have an account? Sign up
) } export const Route = createFileRoute('/login')({ component: LoginPage, beforeLoad: () => { if (useAuthStore.getState().user) { throw redirect({ to: '/', }) } }, })