Next.js Discord

Discord Forum

Redirect logic

Answered
Irish Terrier posted this in #help-forum
Open in Discord
Irish TerrierOP
Hi! I have a landing page, and an auth button. When a user is authenticated, I want to redirect them to a /user page, meaning that they are not able to open the landing page. Where is the perfect location to put a redirect logic in the app?
Answered by James4u
something like this
View full answer

11 Replies

@Irish Terrier do you use server actions for auth?
if your landing page, you can put redirection logic before rendering the UI.
something like this
Answer
in your case redirect("/user");
Irish TerrierOP
Thanks @James4u ! Yeah, I know there are a variety of options, but I thought maybe I should put it inside the middleware.ts

but I think your proposal is better and I should leave it on a page level
sure, and by middleware.ts you mean the global middleware?
Irish TerrierOP
yeah. i wasn't sure if it was a good from the architectural point of view
but as far as i can tell, this is a decent solution. not sure if it's the best
yeah, if you are on app router - no way to doubt this approach
please mark solution if everything is clear 😅
Irish TerrierOP
let's gooo
thanks @James4u