如何解决如何在没有登录页面的情况下使用 NextAuth.js 进行身份验证使用邮递员
到目前为止,在我的 Next.js
应用中,我已经完成了注册。
我被要求使用 next-auth
和自定义凭据进行身份验证(登录)。我必须创建一个 rest API 端点:api/auth/login
。
我知道如何使用登录页面执行此操作(来自此 NextAuth.js Credentials Provider)。可以轻松完成[...nextauth].js
(请看一看)。
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import prisma from '../../../lib/prisma'
const options = {
providers: [
Providers.Credentials({
name: 'Credentials',credentials: {
email: { label: "Email",type: "email",placeholder: "something@example.com" },password: { label: "Password",type: "password" }
},async authorize(credentials) {
const {email,password} = credentials
const user = await prisma.user.findFirst({ where: { email,password } })
console.log(user);
// If no error and we have user data,return it
if (user) {
return user
}
// Return null if user data could not be retrieved
return null
}
})
]
}
export default (req,res) => NextAuth(req,res,options)
但是,问题是我的其余 API 端点 (api/auth/login
) 将使用 Postman 进行命中/测试。那么,我如何才能在没有前端的情况下实现与典型的 Rest API 行为 (req-res) 相同的功能。
我被困在 /pages/api/auth/login.js
文件中:
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import prisma from '../../../lib/prisma'
const options = {
providers: [
Providers.Credentials({
name: 'Credentials',return it
if (user) {
return user
}
// Return null if user data could not be retrieved
return null
}
})
]
}
export default async function handle(req,res) {
if (req.method === 'POST') NextAuth(req,options)
}
不知道如何以一种 REST API 的方式实现这一点。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。