[英]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.