Skip to content

getServerSession not working in middleware using Next.js 13 app directory #7732

@thexpand

Description

@thexpand

Question 💬

I can't get the getServerSession to work in a middleware. I'm using the new Next 13 app directory and I placed the middleware.ts file in the root of my project. When I try to open a URL that matches the middleware and goes through it, I get the following error:

- error node_modules\oidc-token-hash\lib\shake256.js (3:0) @ <unknown>
- error Cannot read properties of undefined (reading 'substring')

How to reproduce ☕️

import type { NextRequest } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/utils/auth-options';

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
  const session = await getServerSession(authOptions);
  console.log('session', session);
}

// See "Matching Paths" below to learn more
export const config = {
  matcher: [
    '/((?!api|_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml).*)',
  ],
};

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsk how to do something or how something works

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions