Next.js Discord

Discord Forum

Nextjs protected routes

Answered
Rock Dove posted this in #help-forum
Open in Discord
Rock DoveOP
is there a way to implement client route protection without middleware.ts file configuration. Im storing the user details in context, how can i implement the route protection
Answered by American
yes, or u can just wrap all protected page and use same layout. like this
View full answer

9 Replies

Rock DoveOP
problem is nextjs not allows hooks inside middleware.ts file
are there any workarounds
European sprat
cookie, local storage, db
American
u can check the user context in page or layout like this
@American u can check the user context in page or layout like this
Rock DoveOP
in this case, I have to do this validation in all of my pages, am I correct
@European sprat cookie, local storage, db
Rock DoveOP
is Localstorage is a good practice
@Rock Dove in this case, I have to do this validation in all of my pages, am I correct
American
yes, or u can just wrap all protected page and use same layout. like this
Answer
@American yes, or u can just wrap all protected page and use same layout. like this
Rock DoveOP
thak you that woud work for me
@American yes, or u can just wrap all protected page and use same layout. like this
Rock DoveOP
export default async function MenuLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  try {
    const data = await instance.get('/auth');

    if (!data) return notFound();
  } catch (error) {
    console.error(error);
    return notFound();
  }

  return (
    <MsalAuthenticationTemplate interactionType={InteractionType.Redirect}>
      <RequestInterceptor>
        <main className='d-flex gap-3'>
          <SideBar />
          {children}
        </main>
      </RequestInterceptor>
    </MsalAuthenticationTemplate>
  );
}