Next.js Discord

Discord Forum

May i can switch this in middleware ?

Unanswered
Ninhache posted this in #help-forum
Open in Discord
Here's a component :

export default async function DashboardPage() {
  const { role } = await getServerSession(authOptions)

  if (!role) {
    redirect('/login')
  }

  switch(role.toLowerCase()) {
    case 'artiste':
    case 'organisateur':
    case 'admin':
      redirect(`/dashboard/${role.toLowerCase()}`)
      break;
    default:   
  }
  return (
    <></>
  );
}

Simplified component.. but yes this is a component, to redirect to the great route, do you think i can move this the middleware ?
I feel i can't because i'm getting the user's role from the session

13 Replies

i dont feel like you need to
don't forget to redirect on default:
@alfon i dont feel like you need to
Mh.. i wanted to move it to middleware to ease the implementation of i18n
because in my middleware, i intent to parse request to know which trad i've to use..
If i'm on the route /en/dashboard, dashboard should be in english and so on
ah fair enough
since there's no real plugin to handle it correctly, i've to do this in the middleware :/
@alfon ah fair enough
And then.. i can't use the session.. or i'm wrong?
@alfon why not?
because...? Idk how i could get the session :kappa:
Japanese anchovy
it should be in the request headers which you have access to in the middleware
i've to try i didnt find it yet.. and i'm stuck on something else rn