Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

_authed.tsx

36 lines974 Bytes

1import { createFileRoute } from '@tanstack/react-router'
2import { createServerFn } from '@tanstack/react-start'
3import { Login } from '../components/Login'
4import { getSupabaseServerClient } from '../utils/supabase'
5​
6export const loginFn = createServerFn({ method: 'POST' })
7 .validator((d: { email: string; password: string }) => d)
8 .handler(async ({ data }) => {
9 const supabase = getSupabaseServerClient()
10 const { error } = await supabase.auth.signInWithPassword({
11 email: data.email,
12 password: data.password,
13 })
14​
15 if (error) {
16 return {
17 error: true,
18 message: error.message,
19 }
20 }
21 })
22​
23export const Route = createFileRoute('/_authed')({
24 beforeLoad: ({ context }) => {
25 if (!context.user) {
26 throw new Error('Not authenticated')
27 }
28 },
29 errorComponent: ({ error }) => {
30 if (error instanceof Error && error.message === 'Not authenticated') {
31 return <Login />
32 }
33​
34 throw error
35 },
36})