Password-based Authentication
Password-based authentication block for TanStack Start
Installation
Folder structure
This block includes the Supabase client. If you already have one installed, you can skip overwriting it.
1import { Link } from '@tanstack/react-router'
2import { useState } from 'react'
3
4import { cn } from '@/lib/utils'
5import { createClient } from '@/lib/supabase/client'
6import { Button } from '@/components/ui/button'
7import {
8 Card,
9 CardContent,
10 CardDescription,
11 CardHeader,
12 CardTitle,
13} from '@/components/ui/card'
14import { Input } from '@/components/ui/input'
15import { Label } from '@/components/ui/label'
16
17export function ForgotPasswordForm({ className, ...props }: React.ComponentPropsWithoutRef<'div'>) {
18 const [email, setEmail] = useState('')
19 const [error, setError] = useState<string | null>(null)
20 const [success, setSuccess] = useState(false)
21 const [isLoading, setIsLoading] = useState(false)
22
23 const handleForgotPassword = async (e: React.FormEvent) => {
24 e.preventDefault()
25 const supabase = createClient()
26 setIsLoading(true)
27 setError(null)
28
29 try {
30 // The url which will be included in the email. This URL needs to be configured in your redirect URLs in the Supabase dashboard at https://supabase.com/dashboard/project/_/auth/url-configuration
31 const { error } = await supabase.auth.resetPasswordForEmail(email, {
32 redirectTo: 'http://localhost:3000/update-password',
33 })
34 if (error) throw error
35 setSuccess(true)
36 } catch (error: unknown) {
37 setError(error instanceof Error ? error.message : 'An error occurred')
38 } finally {
39 setIsLoading(false)
40 }
41 }
42
43 return (
44 <div className={cn('flex flex-col gap-6', className)} {...props}>
45 {success ? (
46 <Card>
47 <CardHeader>
48 <CardTitle className="text-2xl">Check Your Email</CardTitle>
49 <CardDescription>Password reset instructions sent</CardDescription>
50 </CardHeader>
51 <CardContent>
52 <p className="text-sm text-muted-foreground">
53 If you registered using your email and password, you will receive a password reset
54 email.
55 </p>
56 </CardContent>
57 </Card>
58 ) : (
59 <Card>
60 <CardHeader>
61 <CardTitle className="text-2xl">Reset Your Password</CardTitle>
62 <CardDescription>
63 Type in your email and we'll send you a link to reset your password
64 </CardDescription>
65 </CardHeader>
66 <CardContent>
67 <form onSubmit={handleForgotPassword}>
68 <div className="flex flex-col gap-6">
69 <div className="grid gap-2">
70 <Label htmlFor="email">Email</Label>
71 <Input
72 id="email"
73 type="email"
74 placeholder="m@example.com"
75 required
76 value={email}
77 onChange={(e) => setEmail(e.target.value)}
78 />
79 </div>
80 {error && <p className="text-sm text-red-500">{error}</p>}
81 <Button type="submit" className="w-full" disabled={isLoading}>
82 {isLoading ? 'Sending...' : 'Send reset email'}
83 </Button>
84 </div>
85 <div className="mt-4 text-center text-sm">
86 Already have an account?{' '}
87 <Link to="/login" className="underline underline-offset-4">
88 Login
89 </Link>
90 </div>
91 </form>
92 </CardContent>
93 </Card>
94 )}
95 </div>
96 )
97}Usage
Once you install the block in your TanStack Start project, you'll get all the necessary pages and components to set up a password-based authentication flow.
Getting started
After installing the block, you'll have the following environment variables in your .env.local file:
VITE_SUPABASE_URL=
VITE_SUPABASE_PUBLISHABLE_KEY=-
If you're using supabase.com, you can find these values in the Connect modal under App Frameworks or in your project's API settings.
-
If you're using a local instance of Supabase, you can find these values by running
supabase startorsupabase status(if you already have it running).
Adding email templates
-
Add an email template for sign-up to the Supabase project. Your signup email template should contain at least the following HTML:
<h2>Confirm your signup</h2> <p>Follow this link to confirm your user:</p> <p> <a href="{{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=email&next={{ .RedirectTo }}" >Confirm your email</a > </p>For detailed instructions on how to configure your email templates, including the use of variables like
{{ .SiteURL }},{{ .TokenHash }}, and{{ .RedirectTo }}, refer to our Email Templates guide. -
Add an email template for reset password to the Supabase project. Your reset password email template should contain at least the following HTML:
<h2>Reset Password</h2> <p>Follow this link to reset the password for your user:</p> <p> <a href="{{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=recovery&next={{ .RedirectTo }}" >Reset Password</a > </p>
Setting up routes and redirect URLs
- Set the site URL in the URL Configuration settings in the Supabase Dashboard.
- Set up the route users will visit to reset or update their password. Go to the URL Configuration settings and add the
forgot-passwordroute to the list of Redirect URLs. It should look something like:http://example.com/auth/forgot-password. - Update the redirect paths in
login.tsxandupdate-password.tsxcomponents to point to the logged-in routes in your app. 1. Our examples use/protected, but you can set this to whatever fits your app.