mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
chore: auth context
This commit is contained in:
@@ -13,7 +13,7 @@ export function createRouter() {
|
||||
defaultPreload: "intent",
|
||||
defaultErrorComponent: DefaultCatchBoundary,
|
||||
defaultNotFoundComponent: () => <NotFound />,
|
||||
context: { queryClient },
|
||||
context: { queryClient, auth: undefined! },
|
||||
}),
|
||||
queryClient,
|
||||
)
|
||||
|
||||
@@ -44,6 +44,7 @@ export const fetchClerkAuth = createServerFn("GET", async (_, ctx) => {
|
||||
})
|
||||
|
||||
export const Route = createRootRouteWithContext<{
|
||||
auth: { userId: string }
|
||||
queryClient: QueryClient
|
||||
}>()({
|
||||
meta: () => [
|
||||
@@ -77,15 +78,13 @@ export const Route = createRootRouteWithContext<{
|
||||
{ rel: "manifest", href: "/site.webmanifest", color: "#fffff" },
|
||||
{ rel: "icon", href: "/favicon.ico" },
|
||||
],
|
||||
beforeLoad: async ({ cause }) => {
|
||||
if (cause !== "stay") {
|
||||
const auth = await fetchClerkAuth()
|
||||
return { auth }
|
||||
beforeLoad: async ({ context }) => {
|
||||
if (context.auth) {
|
||||
return { auth: context.auth }
|
||||
}
|
||||
|
||||
return {
|
||||
auth: null,
|
||||
}
|
||||
const auth = await fetchClerkAuth()
|
||||
return { auth }
|
||||
},
|
||||
errorComponent: (props) => {
|
||||
return (
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { createFileRoute, Outlet, redirect } from "@tanstack/react-router"
|
||||
|
||||
export const Route = createFileRoute("/_layout/_pages/_protected")({
|
||||
beforeLoad: ({ context, location, cause }) => {
|
||||
if (cause === "stay") return
|
||||
beforeLoad: async ({ context, location, cause }) => {
|
||||
if (!context?.auth?.userId) {
|
||||
throw redirect({
|
||||
to: "/sign-in/$",
|
||||
|
||||
Reference in New Issue
Block a user