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

index.tsx

49 lines1.3 KB

1import { createFileRoute, redirect } from '@tanstack/react-router'
2import { projectCollection, todoCollection } from '@/lib/collections'
3import { authClient } from '@/lib/auth-client'
4​
5export const Route = createFileRoute(`/_authenticated/`)({
6 component: () => null,
7 ssr: false,
8 beforeLoad: async () => {
9 const res = await authClient.getSession()
10 if (!res.data?.session) {
11 throw redirect({
12 to: `/login`,
13 search: {
14 redirect: location.href,
15 },
16 })
17 }
18​
19 await projectCollection.preload()
20 await todoCollection.preload()
21​
22 const projects = projectCollection.toArray
23 if (projects.length === 0) {
24 const id = Math.floor(Math.random() * 100000)
25 const tx = projectCollection.insert({
26 id,
27 name: `Default`,
28 description: `Default project`,
29 owner_id: res.data.user.id,
30 shared_user_ids: [],
31 created_at: new Date(),
32 updated_at: new Date(),
33 })
34 await tx.isPersisted.promise
35 const serverProjectId = projectCollection.toArray[0].id
36 throw redirect({
37 to: `/project/$projectId`,
38 params: { projectId: serverProjectId.toString() },
39 replace: true,
40 })
41 }
42​
43 throw redirect({
44 to: `/project/$projectId`,
45 params: { projectId: projects[0].id.toString() },
46 replace: true,
47 })
48 },
49})