使用 NextJS 14 和应用程序路由器时,NextAuth 中间件可以保护页面吗?

问题描述 投票:0回答:1

我尝试了各种各样的方法,但是要么服务器产生错误,导致代码由于某种原因无法在中间件中运行,要么中间件无法检测到请求何时具有有效会话。在某些情况下,服务器日志具有这种令人愉快的无用性(例如当我尝试调用 nextauth getServerSession 时,我认为这在中间件中不起作用 - 感谢 nextauth 没有记录这一点):)

⨯ node_modules/oidc-token-hash/lib/shake256.js (3:0) @ <unknown>

我知道我的服务器端页面可以调用 nextauth 的 getServerSession()。我尝试将内置中间件与 nextauth 一起使用,但没有注意到有效的会话(它甚至没有看到令牌)。看来 nextauth 在这方面有点被破坏,但我很高兴看到 nextauth 中间件的工作示例(我正在使用 AWS Cogito,但怀疑其他配置中存在问题)。下面的代码例如从来没有看到过令牌。

import { withAuth } from "next-auth/middleware";
import authOptions from './app/api/auth/[...nextauth]/authOptions';
import { getServerSession } from "next-auth/next";

export default withAuth(
    function middleware(req) {
        console.log(req);
        console.log(req.nextauth.token)
    }, {
    callbacks: {
      authorized: ({ token }) => { console.log("Token", token) ; return !!token} ,
    },
  })


// Tried this and it didn't work either - and many other incantations that don't work
// import NextAuth from 'next-auth';
// import authOptions from './app/api/auth/[...nextauth]/authOptions';

// export default NextAuth(authOptions).auth;


export const config = { matcher: ["/creator/:path*"] }

以下是我的配置:

import type { NextAuthOptions } from 'next-auth';
import CognitoIdentityServiceProvider from "next-auth/providers/cognito"

const authOptions: NextAuthOptions = {
    providers: [
        CognitoIdentityServiceProvider({
            clientId: process.env.COGNITO_CLIENT_ID!,
            clientSecret: process.env.COGNITO_CLIENT_SECRET!,
            issuer: process.env.COGNITO_ISSUER!
        }),
    ],
    secret: process.env.NEXTAUTH_SECRET,
    callbacks: {
        async jwt({user, token, account, profile}) {
            // console.log("Profile", profile)
            // console.log('Token', token)
            // console.log('Account', account)

            // Initial sign in
            if (user && account && profile) {
                token.sub = profile.sub; // Persist the 'sub' from the profile
            }
            // console.log("Token sub", token.sub)
            return token;
        },
    
        async session({session, token}) {
            // console.log("session callback: Token", token)

            session.user.sub = token.sub; // Make 'sub' available on the session object
            // console.log("Session user sub", session.user.sub)

            return session;
        },
    },
    // events: {
    //     signOut: async (message) => {   
    //       console.log('User signed out', message);
    //     },
    // },
}

export default authOptions;
amazon-cognito next.js13 next-auth app-router
1个回答
0
投票

getServerSession
v4 中的
next-auth
,不会在 NextJS 13.4 / 14 应用程序路由器中间件上运行。问题是 getServerSession 被设计为在具有 NodeJS 运行时的服务器上运行,但应用程序路由器的中间件在
edge
运行时上运行,该运行时严格仅支持 Web API。

目前有一个 next-auth v5 处于 Beta 阶段,他们重新构建了整个库,使其变得不可知,可以在任何运行时上运行;服务器、边缘或客户端。

您可以在 v5 升级文档(下面的链接)中看到,以前不支持边缘运行时,新的不可知解决方案是将

getServerSession
替换为
auth

https://authjs.dev/guides/upgrade-to-v5#authenticating-server-side

© www.soinside.com 2019 - 2024. All rights reserved.