Open History to see the last commit
1import { createFileRoute, redirect } from '@tanstack/react-router'
2import { createServerFn, useServerFn } from '@tanstack/start'
3import { useMutation } from '../hooks/useMutation'
4import { Auth } from '../components/Auth'
5import { getSupabaseServerClient } from '../utils/supabase'
6β
7export const signupFn = createServerFn()
8 .validator(
9 (d: unknown) =>
10 d as { email: string; password: string; redirectUrl?: string },
11 )
12 .handler(async ({ data }) => {
13 const supabase = await getSupabaseServerClient()
14 const { error } = await supabase.auth.signUp({
15 email: data.email,
16 password: data.password,
17 })
18 if (error) {
19 return {
20 error: true,
21 message: error.message,
22 }
23 }
24β
25 // Redirect to the prev page stored in the "redirect" search param
26 throw redirect({
27 href: data.redirectUrl || '/',
28 })
29 })
30β
31export const Route = createFileRoute('/signup')({
32 component: SignupComp,
33})
34β
35function SignupComp() {
36 const signupMutation = useMutation({
37 fn: useServerFn(signupFn),
38 })
39β
40 return (
41 <Auth
42 actionText="Sign Up"
43 status={signupMutation.status}
44 onSubmit={(e) => {
45 const formData = new FormData(e.target as HTMLFormElement)
46β
47 signupMutation.mutate({
48 data: {
49 email: formData.get('email') as string,
50 password: formData.get('password') as string,
51 },
52 })
53 }}
54 afterSubmit={
55 signupMutation.data?.error ? (
56 <>
57 <div className="text-red-400">{signupMutation.data.message}</div>
58 </>
59 ) : null
60 }
61 />
62 )
63}