Next Auth works on Localhost but not after deployment on vercel #12470
-
I'm not getting any errors either on localhost or on prod. Weird things happen like, once I try to login in prod, my middleware is not able to get the token & role, hence the user keeps being redirected to login page cuz of middleware. This doesn't happen on localhost. Moreover even after this redirection, I'm able to print session using AuthConfig: https://github.com/Swanand-Wagh/Entomon/blob/main/src/features/auth/server/next-auth-config.ts import { prisma } from '@/db/prisma'; declare module 'next-auth' { export type SessionUser = { const providersConfig = { export const { handlers, signIn, signOut, auth } = NextAuth({
}, AuthProviders: https://github.com/Swanand-Wagh/Entomon/blob/main/src/features/auth/server/next-auth-provider.ts import { ErrorResponse } from '@/types/errors'; export default { Middleware: https://github.com/Swanand-Wagh/Entomon/blob/main/src/middleware.ts import { type routeMapping = keyof typeof ROUTE_MAPPINGS; export async function middleware(req: NextRequest) { console.log('Headers:', req.headers); const nextUrl = req.nextUrl; // Handle route mappings // Allow API auth routes // Prevent 'USER' role from accessing admin routes // Handle authentication routes // Redirect unauthenticated users away from private routes return NextResponse.next(); export const config = { I have setup all env variables: AUTH_TRUST_HOST, AUTH_SECRET, AUTH_GOOGLE_SECRET, AUTH_GOOGLE_ID, NEXT_PUBLIC_APP_URL, DATABASE_URL |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I Solved it. I'm able to get the token in the middleware now by doing the following:
|
Beta Was this translation helpful? Give feedback.
I Solved it. I'm able to get the token in the middleware now by doing the following:
const token = await getToken({ req, secret: process.env.AUTH_SECRET, secureCookie: true, cookieName: process.env.NODE_ENV !== 'production' ? 'authjs.session-token' : '__Secure-authjs.session-token', });