Medium
You are working on an app, which implements a redirection functionality based on the user's role. The application has an app folder structure with adashboard
folder for authenticated users. You have aroute.js
file inapp/dashboard
that handles the redirection logic based on the user role.
import{ redirect, permanentRedirect } from 'next/navigation';
export async function route({ session }){
if(!session || !session.user){
return redirect('/login');
}
if(session.user.role !== 'admin'){
return permanentRedirect('/unauthorized');
}
// Continue with rendering the dashboard for the admin
//...
}
// In this context,`session` is an object containing the user's session information.
Regarding the above scenario and code, what statement(s) is/are correct about handling redirects in Next.js with the App Router?
Author: AnasStatus: PublishedQuestion passed 447 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!
2
How to manipulate HTTP response headers within a `getServerSideProps` function in Next.js1
Where to place static files in a Next.js project.4
How to use Incremental Static Regeneration (ISR) in Next.js to update blog posts periodically after their initial publication.3
Create a NextJS page with a navbar and a footer2
What are the benefits of using dynamic imports in Next.js?5
What is the path to the about page in Next.js?7
Load blog post data at build time for a static render.