Skip to content

Commit c208f20

Browse files
authored
Fix auth check (#340)
1 parent c32ab25 commit c208f20

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

examples/react/projects/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ cp .env.example .env
2525

2626
## Running the Application
2727

28-
__Note: Docker is required to run this starter__
28+
**Note: Docker is required to run this starter**
2929

3030
To run this application:
3131

examples/react/projects/src/routes/_authenticated/index.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
import { createFileRoute, useNavigate } from "@tanstack/react-router"
1+
import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router"
22
import { useLiveQuery } from "@tanstack/react-db"
33
import { useEffect } from "react"
44
import { projectCollection, todoCollection } from "@/lib/collections"
5+
import { authClient } from "@/lib/auth-client"
56

67
export const Route = createFileRoute(`/_authenticated/`)({
78
component: IndexRedirect,
89
ssr: false,
10+
beforeLoad: async () => {
11+
const res = await authClient.getSession()
12+
if (!res.data?.session) {
13+
throw redirect({
14+
to: `/login`,
15+
search: {
16+
// Use the current location to power a redirect after login
17+
// (Do not use `router.state.resolvedLocation` as it can
18+
// potentially lag behind the actual current location)
19+
redirect: location.href,
20+
},
21+
})
22+
}
23+
},
924
loader: async () => {
10-
const timeout = setTimeout(() => {
11-
window.location.href = `/login`
12-
}, 500)
1325
await projectCollection.preload()
1426
await todoCollection.preload()
15-
clearTimeout(timeout)
1627

1728
return null
1829
},

0 commit comments

Comments
 (0)