Question from the Next.js test

The `redirect` function sends a temporary redirect (HTTP 307), suitable for temporary state changes, such as redirecting an unauthenticated user to the login page.

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 161 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!