目前我正在尝试使用 NextAuth 创建用户身份验证。我能够在我的 webapp 内部使用它,并且没有问题。但现在,我正在尝试使用 postman 进行登录。所以我可以共享登录端点。这里是我的[...nextauth].js
const configuration = {
secret: process.env.NEXTAUTH_SECRET,
cookie: {
secure: process.env.NODE_ENV && process.env.NODE_ENV === 'production',
},
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60
},
providers: [
CredentialsProvider({
id: "credentials",
name: "credentials",
credentials: {},
page: "/",
async authorize(credentials) {
try
{
const user = await prisma.user.findFirst({
where: {
email: credentials.email
}
});
if (user !== null)
{
const res = await confirmPasswordHash(credentials.password, user.password);
if (res === true)
{
return user;
}
else
{
console.log("Hash not matched logging in");
return null;
}
}
else {
return null;
}
}
catch (err)
{
console.log("Authorize error:", err);
}
}
}),
],
callbacks: {
async session({ session, user, token }) {
session.user = token.user;
return session;
},
async jwt({ token, user, account, profile, isNewUser }) {
if (user) {
token.user = user;
}
return token;
},
}
}
export default (req, res) => NextAuth(req, res, configuration)
当我通过邮递员打,它返回 HTML 视图。
{
"email":"myemail@email.com",
"password":"12345678"
}
数据将命中http://localhost:3000/api/auth/signin
我怎样才能实现它?提前感谢
我自己不是 Next.js 或 NextAuth 用户,但查看登录页面:https://github.com/nextauthjs/next-auth/blob/2469e44572f23f709fa8c5c65c6b7a4eb2383e9f/packages/next-auth/src/core/pages/signin.tsx
我们可以看到,“凭证”类型的提供者呈现一个“POST”到“回调”URL 的表单。
{provider.type === "credentials" && (
<form action={provider.callbackUrl} method="POST">
<input type="hidden" name="csrfToken" value={csrfToken} />
{Object.keys(provider.credentials).map((credential) => {
return (
<div key={`input-group-${provider.id}`}>
<label
className="section-header"
htmlFor={`input-${credential}-for-${provider.id}-provider`}
>
{provider.credentials[credential].label ?? credential}
</label>
<input
name={credential}
id={`input-${credential}-for-${provider.id}-provider`}
type={provider.credentials[credential].type ?? "text"}
placeholder={
provider.credentials[credential].placeholder ?? ""
}
{...provider.credentials[credential]}
/>
</div>
)
})}
<on type="submit">Sign in with {provider.name}</on>
</form>
)}
该表单预计有一个csrfToken
,如果我理解正确,您可以从/api/auth/csrf
(https://next-auth.js.org/getting-started/rest-api#get-apiauthcsrf)
您应该能够检查呈现的页面,以找出回调 URL 是什么,大概是/api/auth/signin/:provider
,但与实际的提供者。
至于邮递员(或您的移动应用程序),我希望您所需要做的就是将 POST 格式化为该 URL。由于方法是POST
,因此请求正文可能需要作为application/x-www-form-urlencoded
发送。根据我从您的问题中可以理解的内容,表单应该具有 3 个普通字段,email
3和4
哦,很抱歉所有的猜测。希望里面还有有用的东西。
添加GET端点获取 CSRF 令牌:
{{url}} / api / auth / signin
使用以下代码在 Postman 上将测试添加到此端点:
console.log("Response: Test script");
pm.cookies.each(cookie => console.log(cookie));
let csrfToken = pm.cookies.get("next-auth.csrf-token");
// let csrfToken = pm.cookies.get("__Host-next-auth.csrf-token");
let csrfTokenValue = csrfToken.split('|')[0];
let sessionTokenValue = pm.cookies.get("next-auth.session-token");
// let sessionTokenValue = pm.cookies.get("__Secure-next-auth.session-token");
console.log('csrf token value: ', csrfTokenValue);
console.log('session token value: ', sessionTokenValue);
pm.environment.set("csrfToken", csrfTokenValue, "<your-environment-name>");
pm.environment.set("sessionToken", sessionTokenValue, "<your-environment-name>");
“__Host-next-auth.csrf-token”和“__Secure-next-auth.session-token”用于在 Vercel 上部署时测试 Postman。
将POST端点添加到 Login 并添加相同的测试:
{{url}} / api / auth / callback / credentials
首先,您单击GET令牌,然后单击POST登录凭据有效负载。它将返回 HTML 视图,但是它将设置必要的 cookie,使您登录。
要注销,您需要POST端点:
{{url}} / api / auth / signout
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(38条)