nextauth 回调似乎没有运行。

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

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

  1. import NextAuth from 'next-auth';
  2. import GoogleProvider from 'next-auth/providers/google';
  3. export const authOptions = {
  4. // 配置一个或多个身份验证提供程序
  5. providers: [
  6. GoogleProvider({
  7. clientId: process.env.GOOGLE_CLIENT_ID,
  8. clientSecret: process.env.GOOGLE_CLIENT_SECRET,
  9. callbacks: {
  10. async signIn({ account, profile }) {
  11. console.log('a comment'); // 评论不会打印
  12. return true; // 即使返回 false,仍然有效
  13. },
  14. },
  15. }),
  16. // ...在这里添加更多提供程序
  17. ],
  18. secret: process.env.NEXTAUTH_SECRET,
  19. };
  20. export default NextAuth(authOptions);

What am I missing?

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

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

  1. import NextAuth from 'next-auth';
  2. import GoogleProvider from 'next-auth/providers/google';
  3. export const authOptions = {
  4. // Configure one or more authentication providers
  5. providers: [
  6. GoogleProvider({
  7. clientId: process.env.GOOGLE_CLIENT_ID,
  8. clientSecret: process.env.GOOGLE_CLIENT_SECRET,
  9. callbacks: {
  10. async signIn({ account, profile }) {
  11. console.log('a comment'); // comment dossent print
  12. return true; // still working with return false
  13. },
  14. },
  15. }),
  16. // ...add more providers here
  17. ],
  18. secret: process.env.NEXTAUTH_SECRET,
  19. };
  20. export default NextAuth(authOptions);

What am I missing?

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

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

答案1

得分: 1

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

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

在这里查看文档,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.

  1. const authOptions = {
  2. providers: [...],
  3. callbacks: {
  4. async signIn({ account, profile }) {
  5. if (account.provider === "google") {
  6. return profile.email_verified && profile.email.endsWith("@example.com")
  7. }
  8. return true // Do different verification for other providers that don't have `email_verified`
  9. },
  10. }
  11. ...
  12. }

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:

确定