You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After migrating my Next.js project from the standard app directory structure to the src structure, my NextAuth middleware's callbacks in the auth.config.ts are not called at all, which are essential for checking session. The authentication logic works for protecting routes, authenticated users are automatically redirected from the login page (/login) to the dashboard (/dashboard) as expected, but when I navigate to path (/) it does not redirect back to dashboard.
Problem Behavior:
Login works correctly and redirects to /dashboard after successful authentication
Issue: When logged in and manually navigating to the root page (/),
the user is NOT redirected to /dashboard as expected
Expected Behavior:
When a logged-in user visits the root page (/), they should be automatically redirected to /dashboard.
Project Details:
Next.js: 15.4.2-canary.32
NextAuth: 5.0.0-beta.29
Using App Router with Turbopack
Additional information
// middleware.ts file contents:importNextAuthfrom'next-auth';import{authConfig}from'./auth.config';exportdefaultNextAuth(authConfig).auth;exportconstconfig={matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],};// auth.config.ts file contents:importtype{NextAuthConfig}from"next-auth";exportconstauthConfig={pages: {signIn: "/login",},session: {strategy: "jwt",maxAge: 60*60,// 1 hourupdateAge: 60*15,// 15 minutes},callbacks: {asyncjwt({ token, user }){// Add user info to token when user signs inif(user){token.id=user.id;token.email=user.email;token.name=user.name;}returntoken;},asyncsession({ session, token }){console.log("session",session);console.log("token",token);// Only create session if token is valid (user exists in database)if(!token){// Force sign out by throwing errorthrownewError("Invalid session - user no longer exists");}// Add user info from token to sessionsession.user.id=token.idasstring;session.user.email=token.emailasstring;session.user.name=token.nameasstring;returnsession;},authorized({ auth,request: { nextUrl }}){console.log("########################### checking if user is logged in ###########################");console.log("auth",auth);console.log("nextUrl",nextUrl);constisLoggedIn=!!auth?.user;constisOnDashboard=nextUrl.pathname.startsWith("/dashboard");if(isOnDashboard){if(isLoggedIn)returntrue;returnfalse;// Redirect unauthenticated users to login page}elseif(isLoggedIn){returnResponse.redirect(newURL("/dashboard",nextUrl));}returntrue;},},providers: [],// Providers are configured in auth.ts}satisfiesNextAuthConfig;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
After migrating my Next.js project from the standard
app
directory structure to thesrc structure
, my NextAuth middleware'scallbacks
in theauth.config.ts
are not called at all, which are essential for checking session. The authentication logic works for protecting routes, authenticated users are automatically redirected from the login page (/login
) to the dashboard (/dashboard
) as expected, but when I navigate to path (/
) it does not redirect back to dashboard.Problem Behavior:
Login works correctly and redirects to /dashboard after successful authentication
Issue: When logged in and manually navigating to the root page (/),
the user is NOT redirected to /dashboard as expected
Before (Working):
After (Not Working):
Expected Behavior:
When a logged-in user visits the root page (/), they should be automatically redirected to /dashboard.
Project Details:
Next.js: 15.4.2-canary.32
NextAuth: 5.0.0-beta.29
Using App Router with Turbopack
Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions