nextauth 回调似乎没有运行。

huangapple go评论71阅读模式
英文:

nextauth callbacks dosen't seems to run

问题

以下是您要翻译的内容:

"So I'm trying to set authentication with nextauth on my next.js app.
My callbacks from the GoogleProvider doesn't seem to run. If I add a console log I can not see it on console. And also if I change the return to false it doesn't seem to change anything,
that's my code on pages/api/auth/[...nextauth].js

import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';

export const authOptions = {
  // 配置一个或多个身份验证提供程序
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbacks: {
        async signIn({ account, profile }) {
          console.log('a comment'); // 评论不会打印
          return true; // 即使返回 false,仍然有效
        },
      },
    }),
    // ...在这里添加更多提供程序
  ],
  secret: process.env.NEXTAUTH_SECRET,
};
export default NextAuth(authOptions);

What am I missing?

callbacks: {
  async signIn({ account, profile }) {
    console.log('a comment'); // 评论不会打印
    return true; // 即使返回 false,仍然有效
  },
}

I want to use this callback function and can't seem to make it work"

英文:

So I'm trying to set authentication with nextauth on my next.js app.
My callbacks from the GoogleProvider doesn't seems to run. If I add a console log I can not see it on console. And also if I change the return to false it doesn't seems to change anything,
thats my code on pages/api/auth/[...nextauth].js

import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';

export const authOptions = {
  // Configure one or more authentication providers
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbacks: {
        async signIn({ account, profile }) {
          console.log('a comment'); // comment dossent print
          return true; // still working with return false
        },
      },
    }),
    // ...add more providers here
  ],
  secret: process.env.NEXTAUTH_SECRET,
};
export default NextAuth(authOptions);

What am I missing?

callbacks: {
  async signIn({ account, profile }) {
    console.log("a comment"); // comment dossent print
    return true; // still working with return false
  },
}

I want to use this callback function and can't seem to make it work

答案1

得分: 1

我认为你在分配回调块到提供者块下面犯了一个错误。我认为它应该在提供者块之外。请查看下面的文档链接。

const authOptions = {
  providers: [...],
  callbacks: {
    async signIn({ account, profile }) {
      if (account.provider === "google") {
        return profile.email_verified && profile.email.endsWith("@example.com")
      }
      return true // 对于没有 "email_verified" 的其他提供者执行不同的验证
    },
  }
  // ...
}

在这里查看文档,https://next-auth.js.org/providers/google#example

英文:

I think you made a mistake on assigning the callback block under the providers block. I think it should be outside of providers block. See the docs link below.

const authOptions = {
  providers: [...],
  callbacks: {
    async signIn({ account, profile }) {
      if (account.provider === "google") {
        return profile.email_verified && profile.email.endsWith("@example.com")
      }
      return true // Do different verification for other providers that don't have `email_verified`
    },
  }
  ...
}

See the docs here, https://next-auth.js.org/providers/google#example

huangapple
  • 本文由 发表于 2023年3月12日 12:44:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定