简体   繁体   English

Next-auth CredentialProvider 配置和重定向

[英]Next-auth CredentialProvider config and redirect

I'm a bit confused on the implementation of the credentials provider and the redirects.我对凭据提供程序和重定向的实现有点困惑。 The documentation says that the credentials provider doesn't support a callback and its for the OAuth providers.该文档说凭据提供者不支持回调及其对于 OAuth 提供者的支持。 This is fine.这可以。 However, instead of staying on the page and flashing an error message or even logging in like in this video it redirects to https://localhost/api/auth/callback/[credentials-provider-name] .但是,它没有像本视频中那样停留在页面上并显示错误消息甚至登录,而是重定向到https://localhost/api/auth/callback/[credentials-provider-name] Which doesn't even include the port I'm working with.其中甚至不包括我正在使用的端口。 If I explicitly set an id it uses that at the end of the url in instead.如果我明确设置一个 id,它会在 url 的末尾使用它。

This is what I have for the provider这就是我对提供者的看法

import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"

export default NextAuth({
  // Configure one or more authentication providers
  providers: [
    CredentialsProvider({
      credentials: {
        username: { label: "Username", type: "text", placeholder: "someuser69" },
        password: { label: "Password", type: "password" },
      },
      name: "User Pass",
      type: "credentials",
      async authorize(credentials, req) {
        // Add logic here to look up the user from the credentials supplied
        return {
          id: 2,
          name: "user",
          email: "user@gmail.com",
        }
        return null;
      }
    })
    // ...add more providers here
  ],
  callbacks: {
    async jwt({ token, account }) {
      // Persist the OAuth access_token to the token right after signin
      if (account) {
        token.accessToken = account.access_token
      }
      return token
    },
    async session({ session, token, user }) {
      // Send properties to the client, like an access_token from a provider.
      session.accessToken = token.accessToken
      return session
    },
    async redirect({ url, baseUrl, }) {
      console.log("");
      return baseUrl;
    },
    async signIn({ user, account, profile, email, credentials }) {
      return '/home';
    }
  },
  session: {
    jwt: true,
    maxAge: 30 * 24 * 60 * 60,

  },
  secret: "CHANGE!!!",
  jwt: {
    maxAge: 60 * 60 * 24 * 30,
    secret: "afdsfi",

  },

})

I've looked through the docs and I'm not sure if I'm making some massive oversight, here.我已经查看了文档,但我不确定我是否在这里进行了一些大规模的监督。 But some of my major confusions are:但我的一些主要困惑是:

  • Where is this callback set and how do I turn in off in the default provider (if possible).此回调设置在哪里以及如何在默认提供程序中关闭(如果可能)。

  • I don't think the authorize function works.我不认为authorize function 有效。 If I put a console log in it.如果我在其中放置一个控制台日志。 It doesn't print to the terminal.它不会打印到终端。 So I don't even know if it's being called.所以我什至不知道它是否被调用。

The issue is that I hadn't set the NEXTAUTH_URL variable correctly.问题是我没有正确设置NEXTAUTH_URL变量。 The module apparently appends https if the protocol isn't set in the provided address.如果未在提供的地址中设置协议,则该模块显然会附加https This is the case whether you are using 127.0.0.1 or localhost .无论您使用127.0.0.1还是localhost都是这种情况。 The solution to fixing the callback issues is to pass in the unsecured http protocol if you're using a local address for testing or development purposes like so:如果您将本地地址用于测试或开发目的,则解决回调问题的解决方案是传入不安全的http协议,如下所示:

NEXTAUTH_URL='http://127.0.0.1:3001'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM