Skip to content

Next-auth middleware broken after installing sentry #9681

@maliesa96

Description

@maliesa96

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

7.82.0

Framework Version

No response

Link to Sentry event

https://autores.sentry.io/issues/4676163831/events/d4dbf6490ef14a9392b2e02880e667ac/

SDK Setup

Dependencies:

  • next v13.4.5 (using the app dir)
  • next-auth v4.23.1 (also tried with the latest version, didn't help)
  • sentry v7.82.0

My middleware.ts file (in the root dir, not src):

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 token = await getToken({ req })
        const isAuth = !!token
        // more auth logic, omitted
        return NextResponse.redirect(new URL("/", req.url))
    },
    {
        callbacks: {
            async authorized() {
                // This is a work-around for handling redirect on auth pages.
                // We return true here so that the middleware function above
                // is always called.
                return true
            },
        },
    }
)

// specify paths to be handled by this middleware
export const config = {
    matcher: [ "/dashboard"],
}

My SDK setup just uses the default values generated by the sentry setup wizard.

After installing sentry and running npm run dev, I get this error:

Attempted import error: 'withAuth' is not exported from 'next-auth/middleware' (imported as 'withAuth').
- error middleware.ts (5:0) @ <unknown>
- error (0 , next_auth_middleware__WEBPACK_IMPORTED_MODULE_2__.withAuth) is not a function

I reverted all the changes made by the sentry wizard and ran the dev server again, confirmed no issues.

Steps to Reproduce

  1. create a next app using create-next-app
  2. install next-auth and create a middleware.ts file in the root dir with the contents above
  3. install @sentry/nextjs and run the sentry setup wizard
  4. run npm run dev

Expected Result

No errors when running the dev server

Actual Result

import error in middleware.ts:

Attempted import error: 'withAuth' is not exported from 'next-auth/middleware' (imported as 'withAuth').
- error middleware.ts (5:0) @ <unknown>
- error (0 , next_auth_middleware__WEBPACK_IMPORTED_MODULE_2__.withAuth) is not a function

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nextjsIssues related to the Sentry Nextjs SDK

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions