簡體   English   中英

Next-auth CredentialProvider 配置和重定向

[英]Next-auth CredentialProvider config and redirect

我對憑據提供程序和重定向的實現有點困惑。 該文檔說憑據提供者不支持回調及其對於 OAuth 提供者的支持。 這可以。 但是,它沒有像本視頻中那樣停留在頁面上並顯示錯誤消息甚至登錄,而是重定向到https://localhost/api/auth/callback/[credentials-provider-name] 其中甚至不包括我正在使用的端口。 如果我明確設置一個 id,它會在 url 的末尾使用它。

這就是我對提供者的看法

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",

  },

})

我已經查看了文檔,但我不確定我是否在這里進行了一些大規模的監督。 但我的一些主要困惑是:

  • 此回調設置在哪里以及如何在默認提供程序中關閉(如果可能)。

  • 我不認為authorize function 有效。 如果我在其中放置一個控制台日志。 它不會打印到終端。 所以我什至不知道它是否被調用。

問題是我沒有正確設置NEXTAUTH_URL變量。 如果未在提供的地址中設置協議,則該模塊顯然會附加https 無論您使用127.0.0.1還是localhost都是這種情況。 如果您將本地地址用於測試或開發目的,則解決回調問題的解決方案是傳入不安全的http協議,如下所示:

NEXTAUTH_URL='http://127.0.0.1:3001'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM