我使用的是带有"凭据"的next-auth.js版本4.19.2(db)提供者和一些自定义登录, checkout 页面。我似乎无法返回它期望从授权()处理程序。我希望返回经过身份验证的用户或错误消息。我尝试了"返回用户"和"返回空值"以及解析和拒绝Promise "返回Promise. resolve"(用户)"和"返回承诺.拒绝(空)"...都不起作用。有人能发现下面的问题吗?谢谢!
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import GoogleProvider from "next-auth/providers/google";
import User from "../../../../models/User";
export const authOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
CredentialsProvider({
name: "Credentials",
async authorize(credentials, req) {
const { username, password } = credentials;
const user = await User.findOne({ email: username }).exec();
if (user) {
console.log("user", user);
await user.comparePassword(password, function (err, isMatch) {
console.log("comparePassword err, isMatch", err, isMatch);
if (err) throw err;
if (isMatch) {
console.log("IS MATCH!");
return Promise.resolve(user);
}
});
} else {
return Promise.reject(null);
}
},
}),
],
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: "/auth/signin",
signOut: "/auth/signout",
error: "/auth/error", // Error code passed in query string as ?error=
verifyRequest: "/auth/verify-request", // (used for check email message)
newUser: "/auth/new-user", // New users will be directed here on first sign in (leave the property out if not of interest)
},
};
export default NextAuth(authOptions);
像这样使用它:
<button
type="submit"
className="btn btn-primary w-100 mt-4"
onClick={() =>
signIn("credentials", {
redirect: false,
username: state.username,
password: state.password,
callbackUrl: "/",
}).then(({ ok, error }) => {
if (ok) {
alert("OK");
} else {
console.log(error);
alert("NOT OK");
}
})
}
>
Sign in
</button>
我到底做错了什么?
2条答案
按热度按时间vq8itlhq1#
你必须返回一个对象而不是一个承诺:
同样,这里您只返回
user
,而不是将其存储在session
中,为此您应该使用回调:wrrgggsh2#
这是我当前使用凭据的下一个身份验证,工作正常