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