Hello guys i have an issue with get token null on server side :
Unanswered
West African Lion posted this in #help-forum
West African LionOP
middleware
Front:
always redirect to the signin page for admin only page
import { getToken } from 'next-auth/jwt';
const publicPaths = ['/signin', '/register', '/api/auth', '/signup'];
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const isPublicPath = publicPaths.some(
(path) => pathname === path || pathname.startsWith(`${path}/`),
);
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
});
if (!isPublicPath && !token) {
const loginUrl = new URL('/signin', request.url);
return NextResponse.redirect(loginUrl);
}
Front:
const result = await signIn('credentials', {
identifier: data.email,
password: data.password,
redirect: false,
});
useEffect(() => {
if (status === 'authenticated' && session?.user) {
if (session.user.accessLevel === AccessLevel.SUPERADMIN) {
console.log('redirecting to admin/clinics', new Date());
router.replace('/superAdmin');
} else if (session.user.accessLevel !== AccessLevel.ADMIN) {
console.log('redirecting to onboarding', new Date());
router.replace('/admin');
} else {
console.log('redirecting to unauthorized');
router.replace('/unauthorized');
}
}
}, [status, session, router]);
always redirect to the signin page for admin only page