为什么凭据授予的凭证选项?

发布于 2025-01-22 19:06:23 字数 1730 浏览 0 评论 0原文

在页面中使用自定义符号的NextAuth.js凭据提供商的许多代码示例都不包含凭证选项中的凭据选项。如文档中所述( https://next-auth.js.s.s.s.ss.org/providers/凭据#示例)凭据选项的目的是根据附加到此选项的属性自动“在页面中的符号上生成合适的表单”。 由于我在页面中使用自定义符号,因此我不需要这些自动生成的字段,因为我手动编码它们,就像文档中所建议的那样( https://next-auth.js.s.org/configuration/pages#credentials-sign-sign-in-in ) 当我使用TypeScript时,当不提供凭据选项时,它会丢弃类型错误。由于我不需要它,所以我想遗漏它。是否有解决我的问题的解决方案,还是由于其他原因需要和需要的凭证选项?

具有凭据选项的代码示例:

...
providers: [
  CredentialsProvider({
    name: "Credentials",
    credentials: {
      username: { label: "Username", type: "text", placeholder: "jsmith" },
      password: {  label: "Password", type: "password" }
    },
    async authorize(credentials, req) {
      const user = { id: 1, name: "J Smith", email: "[email protected]" }

      if (user) {
        return user
      } else {
        return null
      }
    }
  })
]
...

没有凭据的代码示例选项:

import CredentialsProvider from "next-auth/providers/credentials";
...
providers: [
  CredentialsProvider({
    name: "Credentials",
    async authorize(credentials, req) {
      const user = { id: 1, name: "J Smith", email: "[email protected]" }

      if (user) {
        return user
      } else {
        return null
      }
    }
  })
]
...

PS:使用自定义注册页面时,即使名称选项也不是真正的必要。我也希望能够排除这一点...

Many code examples for the NextAuth.js credentials provider that are using custom sign in pages do not include the credentials option in the CredentialsProvider. As stated in the documentiaion (https://next-auth.js.org/providers/credentials#example) the credentials option's purpose is to automatically "generate a suitable form on the sign in page" based on the properties appended to this option.
As I am using a custom sign in page I do not need these automaically generated fields because I coded them manually as also suggested in the documentation (https://next-auth.js.org/configuration/pages#credentials-sign-in)
As I am using Typescript it is throwing a type error when not providing the credentials option. As I do not need it I would like to leave it out. Is there a solution for my problem or is the credentials option required and needed for other reasons?

Code example with credentials option:

...
providers: [
  CredentialsProvider({
    name: "Credentials",
    credentials: {
      username: { label: "Username", type: "text", placeholder: "jsmith" },
      password: {  label: "Password", type: "password" }
    },
    async authorize(credentials, req) {
      const user = { id: 1, name: "J Smith", email: "[email protected]" }

      if (user) {
        return user
      } else {
        return null
      }
    }
  })
]
...

Code example without credentials option:

import CredentialsProvider from "next-auth/providers/credentials";
...
providers: [
  CredentialsProvider({
    name: "Credentials",
    async authorize(credentials, req) {
      const user = { id: 1, name: "J Smith", email: "[email protected]" }

      if (user) {
        return user
      } else {
        return null
      }
    }
  })
]
...

PS: Even the name option is not really neccessary when using a custom sign up page. I would like to be able to exclude that too...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

破晓 2025-01-29 19:06:23

在NextAuth.js中,凭据选项 cordentialsprovider 并不是严格要求的,但它具有特定的目的。如您提到的那样,其主要作用是根据凭据对象中定义的属性自动在登录页面上生成合适的表单。

如果您使用的是自定义登录页面并手动编码了表单字段,则可以安全省略凭据选项。但是,如果由于缺少凭据选项而使用typescript和遇到类型错误,则有一些选项:

  1. 提供虚拟凭据对象:

您可以提供一个空或虚拟凭证对象满足打字稿类型检查。这样,您可以保留自定义登录页面并避免键入错误。这是一个示例:

import { CredentialsProvider } from "next-auth/providers/credentials";

const credentials = {}; // Empty credentials object

export default NextAuth({
  providers: [
    CredentialsProvider({
      credentials,
      authorize: async (credentials) => {
        // Your custom authorization logic
      },
    }),
    // Other providers...
  ],
  // Other NextAuth options...
});
  1. 类型铸造:

如果您不希望没有空的凭据对象,则可以使用类型铸造来满足打字稿。这告诉Typescript,凭据对象是类型的任何类型,有效地绕过该特定属性的类型检查。这是一个示例:

import { CredentialsProvider } from "next-auth/providers/credentials";

const credentials = {} as any; // Type casting to any

export default NextAuth({
  providers: [
    CredentialsProvider({
      credentials,
      authorize: async (credentials) => {
        // Your custom authorization logic
      },
    }),
    // Other providers...
  ],
  // Other NextAuth options...
});

这两种方法都允许您省略凭据选项,同时仍然满足打字稿的类型检查。请记住,如果您决定切换回NextAuth.js自动生成的表单字段,以便将来重新访问凭据选项。

In NextAuth.js, the credentials option in the CredentialsProvider is not strictly required, but it serves a specific purpose. As you mentioned, its primary role is to automatically generate a suitable form on the sign-in page based on the properties defined within the credentials object.

If you are using a custom sign-in page and have manually coded your form fields, you can safely omit the credentials option. However, if you are using TypeScript and encounter type errors due to the missing credentials option, you have a few options:

  1. Provide a Dummy credentials Object:

You can provide an empty or dummy credentials object to satisfy TypeScript type checking. This way, you can keep your custom sign-in page and avoid type errors. Here's an example:

import { CredentialsProvider } from "next-auth/providers/credentials";

const credentials = {}; // Empty credentials object

export default NextAuth({
  providers: [
    CredentialsProvider({
      credentials,
      authorize: async (credentials) => {
        // Your custom authorization logic
      },
    }),
    // Other providers...
  ],
  // Other NextAuth options...
});
  1. Type Casting:

If you prefer not to have an empty credentials object, you can use type casting to satisfy TypeScript. This tells TypeScript that the credentials object is of type any, effectively bypassing type checking for that specific property. Here's an example:

import { CredentialsProvider } from "next-auth/providers/credentials";

const credentials = {} as any; // Type casting to any

export default NextAuth({
  providers: [
    CredentialsProvider({
      credentials,
      authorize: async (credentials) => {
        // Your custom authorization logic
      },
    }),
    // Other providers...
  ],
  // Other NextAuth options...
});

Both of these approaches allow you to omit the credentials option while still satisfying TypeScript's type checking. Just keep in mind that you may want to revisit the credentials option in the future if you decide to switch back to the NextAuth.js auto-generated form fields for your sign-in page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文