Ras*_*hat 6 javascript rest typescript next.js next-auth
到目前为止,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)
Run Code Online (Sandbox Code Playgroud)
但是,问题是我的其余 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',
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 async function handle(req, res) {
if (req.method === 'POST') NextAuth(req, res, options)
}
Run Code Online (Sandbox Code Playgroud)
不知道如何以 REST API 方式实现这一点。
我尝试过一种方法:请查看已解决的 API,而不发送 /api/auth/callback/credentials 的响应,这可能会导致请求停滞
| 归档时间: |
|
| 查看次数: |
2300 次 |
| 最近记录: |