How to use Authentication based routing #85
Replies: 1 comment
-
You've asked this question a while ago, so I believe you'd already have your answer as well. Shadcn-admin is just a Single Page Application (SPA), which means you don't have to worry about server-side rendering and client-side rendering complexities. Here's the basic flow:
In shadcn-admin, you can check the auth inside export const Route = createFileRoute('/_authenticated')({
beforeLoad: ({ location }) => {
const { accessToken, setUser } = useAuthStore.getState().auth // get the token and `setUser` method from Zustand state
const { isValid, token } = checkAuthToken(accessToken) // check if the token is valid
// if no token found or token is not valid, redirect back to `/sign-in` page
if (!isValid || !token) {
setUser(null)
throw redirect({
to: "/sign-in",
search: { redirect: `${location.href}` },
mask: { to: "/sign-in" },
})
}
setUser(token)
},
component: RouteComponent,
}) This example is not perfect yet. Usually, you might have to deal with refresh token as well. I won't be showing an example code for that. However, here's the basic flow:
|
Beta Was this translation helpful? Give feedback.
-
I want the sign up / login page to appear as the user access the website.
I am new to this, hence i am not able to exactly figure out how to do it.
Beta Was this translation helpful? Give feedback.
All reactions