nextauthjs / nextauthjs/next-auth

Nextjs13: Not redirected after signin by the middleware.ts file in the deployed version of the project.

Open
#8,978 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug triage
Dominant language
TypeScript
Stars
28.4k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

Environment

System:
OS: macOS 13.0.1
CPU: (8) arm64 Apple M1
Memory: 365.48 MB / 8.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.17.1 - /usr/local/bin/node
npm: 9.6.7 - /usr/local/bin/npm
next: 13.4.9 -> 14.0.0
next-auth: 4.20.1
Browsers:
Brave Browser: 117.1.58.137
Chrome: 118.0.5993.117
Safari: 16.1

Reproduction URL

https://github.com/danishrp102/TypeThreads

Describe the issue

So, I have used next-auth for Google authentication. For my nextjs13 project, I have created a middleware.ts file in the src directory for route protection. I have no problems with the app functioning in development, but after I deploy it to vercel, I am not able to access the dashboard after successful signIn. I can say that I am successfully signed in because the Redis database shows the user stored in the DB. Please help me to solve this issue.

How to reproduce

Head over to https://type-threads.vercel.app/ and try to signIn through your google account

OR

Visit my repository https://github.com/danishrp102/TypeThreads to clone it and run it locally.
This would require you to create an upstash account, pusher account and install all the dependencies as mentioned int the package.json

OR

This is my middleware.ts file:

// prevents relogins and accessing sensitive routes without logins

import { getToken } from "next-auth/jwt";
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";

export default withAuth(
    async function middleware(req) {
        const pathname = req.nextUrl.pathname;

        // Manage the route protection
        const isAuth = await getToken({ req });
        const isLoginPage = pathname.startsWith('/login');

        const sensitiveRoutes = ['/dashboard'];
        const isAccessingSensitiveRoutes = sensitiveRoutes.some((route) => pathname.startsWith(route));

        if (isLoginPage) {
            if (isAuth) {
                return NextResponse.redirect(new URL('/dashboard', req.url));
            }

            return NextResponse.next();
        }

        if (!isAuth && isAccessingSensitiveRoutes) {
            return NextResponse.redirect(new URL('/login', req.url));
        }

        if (pathname === '/') {
            return NextResponse.redirect(new URL('/dashboard', req.url));
        }
    }, {
    callbacks: {
        async authorized() {
            return true;
        },
    }
}
)

export const config = {
    matcher: ['/', '/login', '/dashboard/:path*']
}

Add this to your project and implement simple google authentication using next-auth.

Expected behavior

I should be able to successfully signIn, but I am not redirected to the dashboard. Instead, the page is refreshed while the token values are apparently not received in the middleware file in the deployed version.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the sign-in flow at https://type-threads.vercel.app/ and inspect the referenced middleware.ts and package.json in the TypeThreads repository. Compare how the middleware receives authentication tokens locally and in the deployed version, using the listed Next.js and next-auth versions. Done means a successful Google sign-in redirects to /dashboard in the deployed app.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript
Domain
authentication, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.