Working on authenticated Recipes routes.

This commit is contained in:
Jesse Brault 2024-08-01 08:23:30 -05:00
parent 68702595c5
commit 159f0177eb
7 changed files with 60 additions and 61 deletions

View File

@ -11,19 +11,14 @@
// Import Routes // Import Routes
import { Route as rootRoute } from './routes/__root' import { Route as rootRoute } from './routes/__root'
import { Route as RecipesImport } from './routes/recipes'
import { Route as LoginImport } from './routes/login' import { Route as LoginImport } from './routes/login'
import { Route as AuthImport } from './routes/_auth' import { Route as AuthImport } from './routes/_auth'
import { Route as AuthIndexImport } from './routes/_auth/index' import { Route as AuthIndexImport } from './routes/_auth/index'
import { Route as RecipesUsernameSlugImport } from './routes/recipes_/$username.$slug' import { Route as AuthRecipesImport } from './routes/_auth/recipes'
import { Route as AuthRecipesUsernameSlugImport } from './routes/_auth/recipes_/$username.$slug'
// Create/Update Routes // Create/Update Routes
const RecipesRoute = RecipesImport.update({
path: '/recipes',
getParentRoute: () => rootRoute,
} as any)
const LoginRoute = LoginImport.update({ const LoginRoute = LoginImport.update({
path: '/login', path: '/login',
getParentRoute: () => rootRoute, getParentRoute: () => rootRoute,
@ -39,9 +34,14 @@ const AuthIndexRoute = AuthIndexImport.update({
getParentRoute: () => AuthRoute, getParentRoute: () => AuthRoute,
} as any) } as any)
const RecipesUsernameSlugRoute = RecipesUsernameSlugImport.update({ const AuthRecipesRoute = AuthRecipesImport.update({
path: '/recipes',
getParentRoute: () => AuthRoute,
} as any)
const AuthRecipesUsernameSlugRoute = AuthRecipesUsernameSlugImport.update({
path: '/recipes/$username/$slug', path: '/recipes/$username/$slug',
getParentRoute: () => rootRoute, getParentRoute: () => AuthRoute,
} as any) } as any)
// Populate the FileRoutesByPath interface // Populate the FileRoutesByPath interface
@ -62,12 +62,12 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof LoginImport preLoaderRoute: typeof LoginImport
parentRoute: typeof rootRoute parentRoute: typeof rootRoute
} }
'/recipes': { '/_auth/recipes': {
id: '/recipes' id: '/_auth/recipes'
path: '/recipes' path: '/recipes'
fullPath: '/recipes' fullPath: '/recipes'
preLoaderRoute: typeof RecipesImport preLoaderRoute: typeof AuthRecipesImport
parentRoute: typeof rootRoute parentRoute: typeof AuthImport
} }
'/_auth/': { '/_auth/': {
id: '/_auth/' id: '/_auth/'
@ -76,12 +76,12 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof AuthIndexImport preLoaderRoute: typeof AuthIndexImport
parentRoute: typeof AuthImport parentRoute: typeof AuthImport
} }
'/recipes/$username/$slug': { '/_auth/recipes/$username/$slug': {
id: '/recipes/$username/$slug' id: '/_auth/recipes/$username/$slug'
path: '/recipes/$username/$slug' path: '/recipes/$username/$slug'
fullPath: '/recipes/$username/$slug' fullPath: '/recipes/$username/$slug'
preLoaderRoute: typeof RecipesUsernameSlugImport preLoaderRoute: typeof AuthRecipesUsernameSlugImport
parentRoute: typeof rootRoute parentRoute: typeof AuthImport
} }
} }
} }
@ -89,10 +89,12 @@ declare module '@tanstack/react-router' {
// Create and export the route tree // Create and export the route tree
export const routeTree = rootRoute.addChildren({ export const routeTree = rootRoute.addChildren({
AuthRoute: AuthRoute.addChildren({ AuthIndexRoute }), AuthRoute: AuthRoute.addChildren({
AuthRecipesRoute,
AuthIndexRoute,
AuthRecipesUsernameSlugRoute,
}),
LoginRoute, LoginRoute,
RecipesRoute,
RecipesUsernameSlugRoute,
}) })
/* prettier-ignore-end */ /* prettier-ignore-end */
@ -104,29 +106,31 @@ export const routeTree = rootRoute.addChildren({
"filePath": "__root.tsx", "filePath": "__root.tsx",
"children": [ "children": [
"/_auth", "/_auth",
"/login", "/login"
"/recipes",
"/recipes/$username/$slug"
] ]
}, },
"/_auth": { "/_auth": {
"filePath": "_auth.tsx", "filePath": "_auth.tsx",
"children": [ "children": [
"/_auth/" "/_auth/recipes",
"/_auth/",
"/_auth/recipes/$username/$slug"
] ]
}, },
"/login": { "/login": {
"filePath": "login.tsx" "filePath": "login.tsx"
}, },
"/recipes": { "/_auth/recipes": {
"filePath": "recipes.tsx" "filePath": "_auth/recipes.tsx",
"parent": "/_auth"
}, },
"/_auth/": { "/_auth/": {
"filePath": "_auth/index.tsx", "filePath": "_auth/index.tsx",
"parent": "/_auth" "parent": "/_auth"
}, },
"/recipes/$username/$slug": { "/_auth/recipes/$username/$slug": {
"filePath": "recipes_/$username.$slug.tsx" "filePath": "_auth/recipes_/$username.$slug.tsx",
"parent": "/_auth"
} }
} }
} }

View File

@ -22,11 +22,18 @@ const RootLayout = () => {
return ( return (
<> <>
<div> <header>
<h1>Hello, World.</h1> <h1>Meals Made Easy</h1>
<nav>
<button onClick={onLogout}>Logout</button> <button onClick={onLogout}>Logout</button>
</nav>
</header>
<main>
<Outlet /> <Outlet />
</div> </main>
<footer>
<p>Copyright 2024 Jesse R. Brault. All rights reserved.</p>
</footer>
<TanStackRouterDevtools position="bottom-right" /> <TanStackRouterDevtools position="bottom-right" />
</> </>
) )

View File

@ -1,19 +1,7 @@
import { useQuery } from '@tanstack/react-query' import { createFileRoute, redirect } from '@tanstack/react-router'
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/_auth/')({ export const Route = createFileRoute('/_auth/')({
component: () => { beforeLoad() {
const client = useQuery({ throw redirect({ to: '/recipes' })
queryKey: ['index'],
queryFn() {
return 'Hello, Jesse!'
}
})
return (
<div>
<h2>Index Page You are logged in.</h2>
{client.data ? <h3>{client.data}</h3> : null}
</div>
)
} }
}) })

View File

@ -0,0 +1,6 @@
import { createFileRoute } from '@tanstack/react-router'
import Recipes from '../../pages/recipes/Recipes'
export const Route = createFileRoute('/_auth/recipes')({
component: Recipes
})

View File

@ -4,11 +4,11 @@ import {
useLoaderData, useLoaderData,
useParams useParams
} from '@tanstack/react-router' } from '@tanstack/react-router'
import { ApiError } from '../../api/ApiError' import { ApiError } from '../../../api/ApiError'
import getRecipe from '../../api/getRecipe' import getRecipe from '../../../api/getRecipe'
import Recipe from '../../pages/recipe/Recipe' import Recipe from '../../../pages/recipe/Recipe'
export const Route = createFileRoute('/recipes/$username/$slug')({ export const Route = createFileRoute('/_auth/recipes/$username/$slug')({
loader: ({ abortController, context, params }) => loader: ({ abortController, context, params }) =>
context.queryClient.ensureQueryData({ context.queryClient.ensureQueryData({
queryKey: ['recipe', params.username, params.slug], queryKey: ['recipe', params.username, params.slug],
@ -22,7 +22,7 @@ export const Route = createFileRoute('/recipes/$username/$slug')({
}), }),
component() { component() {
const recipe = useLoaderData({ const recipe = useLoaderData({
from: '/recipes/$username/$slug' from: '/_auth/recipes/$username/$slug'
}) })
return <Recipe {...{ recipe }} /> return <Recipe {...{ recipe }} />
}, },
@ -38,7 +38,7 @@ export const Route = createFileRoute('/recipes/$username/$slug')({
}, },
notFoundComponent() { notFoundComponent() {
const { username, slug } = useParams({ const { username, slug } = useParams({
from: '/recipes/$username/$slug' from: '/_auth/recipes/$username/$slug'
}) })
return ( return (
<p> <p>

View File

@ -27,7 +27,7 @@ const Login = () => {
if (loginResult._tag === 'success') { if (loginResult._tag === 'success') {
auth.putToken(loginResult.loginView.accessToken, async () => { auth.putToken(loginResult.loginView.accessToken, async () => {
await router.invalidate() await router.invalidate()
await navigate({ to: search.redirect ?? '/' }) await navigate({ to: search.redirect ?? '/recipes' })
}) })
} else { } else {
setError(loginResult.error) setError(loginResult.error)
@ -58,7 +58,7 @@ export const Route = createFileRoute('/login')({
}), }),
beforeLoad({ context, search }) { beforeLoad({ context, search }) {
if (context.auth.token) { if (context.auth.token) {
throw redirect({ to: search.redirect || '/' }) throw redirect({ to: search.redirect || '/recipes' })
} }
}, },
component: Login component: Login

View File

@ -1,6 +0,0 @@
import { createFileRoute } from '@tanstack/react-router'
import Recipes from '../pages/recipes/Recipes'
export const Route = createFileRoute('/recipes')({
component: Recipes
})