Best practice to re-route user if no session exists with t3
Hi!
What’s the best practice to re-route users when they arent signed in? I see a lot of examples where they use like useSession() inside of each component. But I would like the users who doesnt have a session yet to be presented with a landing page about the product.
5 Replies
Check out middlewares 🙂
Clerk:
https://clerk.com/docs/nextjs/middleware
Auth.js:
https://github.com/nextauthjs/next-auth-example/blob/main/middleware.ts
Next.js Middleware | Clerk
Details about Clerk middleware for your Next.js application
GitHub
next-auth-example/middleware.ts at main · nextauthjs/next-auth-exam...
Example showing how to use NextAuth.js with Next.js - next-auth-example/middleware.ts at main · nextauthjs/next-auth-example
I am using NextAuth with PlanetScale. Have some memory that middlewares isn’t implemented yet for database sessions, only for JWT sessions.
Any examples or solutions of usage with NextAuth and PlanetScale?
you can use gSSP or using next auth
on each page
or you can use the next auth custom client handling
https://next-auth.js.org/getting-started/client#custom-client-session-handling
Client API | NextAuth.js
The NextAuth.js client library makes it easy to interact with sessions from React applications.