admin管理员组

文章数量:1125600

I'm working on a project using Next.js v15.0.1 and NextAuth.js v5.0.0-beta.25. When I run my app, I encounter the following error:

Unknown module type  
This module doesn't have an associated type. Use a known file extension, or register a loader for it.  

Read more:   

The issue occurs when I try to get the session data in the middleware.js file using the auth function provided by NextAuth.js in the auth.js file I use only the credentials provider in NextAuth. Here's my code:

// middleware.js
import { NextResponse } from 'next/server';
import { auth } from './auth';

export function middleware(request) {
    const { data } = auth();
    const token = request.cookies.get('authjs.session-token');

    console.log('token', token);
    console.log(data);

    return NextResponse.next();
}

export const config = {
    matcher: ['/login', '/signup'],
};
// auth.js
import NextAuth from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { authorizeUser } from '@/lib/auth/user';

export const { auth, handlers, signIn, signOut } = NextAuth({
    providers: [
        Credentials({
            credentials: {
                email: {},
                password: {},
            },
            authorize: authorizeUser,
        }),
    ],
    secret: process.env.NEXTAUTH_SECRET,
});
// next.config.mjs
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;

I tried using the auth() function from auth.js to get the session data inside middleware.js and log it to the console.I expected the session data to be returned and for the application to compile without errors.

本文标签: javascriptFailed to compile Unknown module type in Nextjs with NextAuthjsStack Overflow