Add redirects rule inside t3 app
Hi how can I add a redirects rule in the t3 next.config file.
Currently my file looks like this:
Kind of confused where and how to set the redirects rule, basically in my case I want to redirect to sign-in if the user is not signed-in, else he should be redirected to /dashboard
9 Replies
I think what you are looking for is NextJS Middleware. Take a look at the docs here: https://nextjs.org/docs/advanced-features/middleware
Advanced Features: Middleware | Next.js
Learn how to use Middleware to run code before a request is completed.
So if you want normal 301 (or 302) redirects, you add it to your next config
if you want to do it based on session you can do it elsewhere
Serverside Session Handler:
index.tsx
@dinozoidyt - is this not the best way to do it?
@fotoflo this works in the config but getting a type error:
error
i think there's somewhere in t3app where they define the types for the config file
i did mine like this but i dunno how secure it is. Did it like that incase i ever need static pages that wont be server generated:
this is in the middleware.ts file
the matcher sets in which routes this will run on
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
oh wow that makes a lot of sense ! What i did probably wasn't too secure anyways... Should probably get around finishing reading those docs
@arcanist Would you create like a protected which which contains that logic on unauth?
Let me try that out looks promising