import { redirect, createFileRoute } from '@tanstack/react-router' import { createServerFn, useServerFn } from '@tanstack/react-start' import { useMutation } from '../hooks/useMutation' import { Auth } from '../components/Auth' import { getSupabaseServerClient } from '../utils/supabase' export const signupFn = createServerFn({ method: 'POST' }) .validator( (d: { email: string; password: string; redirectUrl?: string }) => d, ) .handler(async ({ data }) => { const supabase = getSupabaseServerClient() const { error } = await supabase.auth.signUp({ email: data.email, password: data.password, }) if (error) { return { error: true, message: error.message, } } // Redirect to the prev page stored in the "redirect" search param throw redirect({ href: data.redirectUrl || '/', }) }) export const Route = createFileRoute('/signup')({ component: SignupComp, }) function SignupComp() { const signupMutation = useMutation({ fn: useServerFn(signupFn), }) return ( { const formData = new FormData(e.target as HTMLFormElement) signupMutation.mutate({ data: { email: formData.get('email') as string, password: formData.get('password') as string, }, }) }} afterSubmit={ signupMutation.data?.error ? ( <>
{signupMutation.data.message}
) : null } /> ) }