Next.js Discord

Discord Forum

How to set multiple entry paths based on the role of the user in Nextjs 13?

Unanswered
Band-tailed Pigeon posted this in #help-forum
Open in Discord
Band-tailed PigeonOP
I want to set different entry paths based on the role of the user, for example I want to set entries for normal users to be /home and I want to set /dashboard for admin. I have tried to use some kind of session hook but it can't be called or used outside the component, as you can see the logic for defining the entry path is outside the component. So how can I solve this problem??


import { signIn, signUp, requireNextAuth, useSession } from "@roq/nextjs"; import HomeLayout from "layout/home-layout"; import { FC, useEffect, useState } from "react"; import { useRouter } from "next/router"; let currentUser: any; function HomePage() { const { session } = useSession(); currentUser = session?.user?.roles?.[0]; const router = useRouter(); return ( <> {\*Existing code */} </> ); } } export default requireNextAuth({ redirectIfAuthenticated: true, redirectTo: currentUser === ‘admin’ ? ‘/admin’ : ‘/home’, })(HomePage);

10 Replies

Southern fire ant
what are you using for the session, is that nextAuth session?
Band-tailed PigeonOP
it’s not nextAuth session, its from @roq/nextjs
Southern fire ant
what is the requireNextAuth doing?
Band-tailed PigeonOP
I can share the code here
This is what I get
import { ComponentType } from 'react'; import { UseAuthHookInterface } from '@roq/ui-react'; export interface RequireNextAuthPropsInterface extends UseAuthHookInterface { } export interface RequireNextAuthHocParamsInterface<P> { Loader?: ComponentType<P>; redirectTo?: string; redirectIfAuthenticated?: boolean; } export declare function requireNextAuth<P extends object, T extends P & RequireNextAuthPropsInterface>(props?: RequireNextAuthHocParamsInterface<P>): (WrappedComponent: ComponentType<P>) => ComponentType<T>;
Southern fire ant
I'm not entirely clear about the requireNextAuth , may be try using 'useRouter' .
Band-tailed PigeonOP
Yeah, I can use useRouter but I thought It can be performed via redirectTo props in requireNextAuth
Southern fire ant
may be checkout @roq/nextjs npm package if there is any documentation about that
Band-tailed PigeonOP
I don't think that built for npm package only, I have got it when I generate the app using there ai