-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ef1627
commit 76db1dd
Showing
12 changed files
with
267 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<!DOCTYPE html> | ||
<!doctype html> | ||
<html lang="en" class="dark"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,22 @@ | ||
import { hc } from "hono/client" | ||
import { type ApiRoutes } from "@server/app" | ||
import { queryOptions } from "@tanstack/react-query" | ||
|
||
const client = hc<ApiRoutes>("/") | ||
|
||
export const api = client.api | ||
|
||
async function getCurrentUser() { | ||
const result = await api.me.$get() | ||
if (!result.ok) { | ||
throw new Error('error') | ||
} | ||
const data = await result.json() | ||
return data | ||
} | ||
|
||
export const userQueryOptions = queryOptions({ | ||
queryKey: ['getCurrentUser'], | ||
queryFn: getCurrentUser, | ||
staleTime: Infinity | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Button } from "@/components/ui/button" | ||
import { userQueryOptions } from "@/lib/api" | ||
import { createFileRoute, Outlet } from "@tanstack/react-router" | ||
|
||
const Login = () => { | ||
return ( | ||
<div className="flex items-center gap-2 max-w-xl m-auto py-2"> | ||
<Button className="min-w-6"> | ||
<a href="/api/login">Login</a> | ||
</Button> | ||
<span>You have to login</span> | ||
</div> | ||
) | ||
} | ||
|
||
const Component = () => { | ||
const { user } = Route.useRouteContext() | ||
if (!user) { | ||
return <Login /> | ||
} | ||
|
||
return <Outlet /> | ||
} | ||
|
||
// src/routes/_authenticated.tsx | ||
export const Route = createFileRoute("/_authenticated")({ | ||
beforeLoad: async ({ context }) => { | ||
const queryClient = context.queryClient | ||
|
||
try { | ||
const data = await queryClient.fetchQuery(userQueryOptions) | ||
return data | ||
} catch (e) { | ||
console.error(e) | ||
return { user: null } | ||
} | ||
// userQueryOptions | ||
// check if the user is authenticated | ||
return { user: { name: "John Doe" } } | ||
// return { user: null } | ||
}, | ||
component: Component, | ||
}) |
Oops, something went wrong.