如何在应用程序目录中的 next js 13 beta 服务器组件中使用 next auth getServerSession

Sha*_*san 19 beta session prisma next-auth next.js13

我将 next auth v4 与 next js 13 beta 和服务器组件一起使用,一切正常。但我有一种情况,我需要知道登录的用户 ID,因为我正在使用下一个身份验证,我可以访问会话,我可以使用 useSession() 但随后我需要使该组件成为客户端组件,所以我想在服务器上使用它,我可以在 API 中使用 getServerSession 因为我可以访问 req & res 对象,但在下一个带有新应用程序目录的 js beta 中,我不能这样做。如果您知道如何解决该问题,请告诉我。谢谢

import { getServerSession } from "next-auth";
import { authOptions } from "@/pages/api/auth/[...nextauth]";

const Test = async () => {
    const user_id = 1; // How do I get user id from session, user_id is available in session

    // I don't have access req & res object in server component.
    const data = await getServerSession(request, response, authOptions);

    console.log(data);

});
    return (
        <></>
    );
};

export default Test;
Run Code Online (Sandbox Code Playgroud)

没有找到足够的信息

Sha*_*san 21

我找到了一个答案,在下一个 js 13 中,您不需要使用请求和响应对象,只需使用 authOptions,它就会工作

import { getServerSession } from "next-auth";
import { authOptions } from "@/pages/api/auth/[...nextauth]";

const Test = async () => {

    const data = await getServerSession(authOptions);

    console.log(data);

});
    return (
        <></>
    );
};

export default Test;
Run Code Online (Sandbox Code Playgroud)

  • @DavidConlisk,当然https://gist.github.com/shakibhasan09/c37eb67910a992f881e722313531b2fb (2认同)

Yil*_*maz 7

选项文档

import { NextAuthOptions } from 'next-auth'
import { getServerSession } from 'next-auth'
Run Code Online (Sandbox Code Playgroud)

这是 NextAuthOptions 类型

export interface AuthOptions {
  providers: Provider[];
  secret?: string;
  session?: Partial<SessionOptions>;
  jwt?: Partial<JWTOptions>;
  pages?: Partial<PagesOptions>;
  callbacks?: Partial<CallbacksOptions>;
  events?: Partial<EventCallbacks>;
  adapter?: Adapter;
  debug?: boolean;
  logger?: Partial<LoggerInstance>;
  theme?: Theme;
  useSecureCookies?: boolean;
  cookies?: Partial<CookiesOptions>;
}
Run Code Online (Sandbox Code Playgroud)

这就是你获得会话的方式

const session = await getServerSession(authOptions)
Run Code Online (Sandbox Code Playgroud)

基于AuthOptions接口

const authOption: NextAuthOptions = {
  // Since you tagged prisma
  adapter: PrismaAdapter(yourDbConfig),
  session: {
    strategy: 'jwt',
  },
  // https://next-auth.js.org/configuration/pages
  pages: {
    signIn: '/login',
  },
  providers: [
    GoogleProvider({
      clientId: clientId,
      clientSecret: clientSecret,
    }),
  ],
  callbacks: {
    async session({ token, session }) {
      if (token) {
        // set session here
      }
      return session
    },
    async jwt({ token, user }) {
      const dbUser = getUserByTokenEmail

      //add logic here
    },
    
  },
}
Run Code Online (Sandbox Code Playgroud)

这就是我们用来authOptions设置 next-auth 的方式

// app/auth/[...nextauth].ts (I htink after next 13.2)
// pages/api/auth/[...nextauth].ts before

import { authOptions } from 'folderLocationOfauthOptions'
import NextAuth from 'next-auth'


export default NextAuth(authOptions)
Run Code Online (Sandbox Code Playgroud)