【发布时间】:2022-02-11 04:16:06
【问题描述】:
我是 next.js 的新手,我想知道是否可以通过中间件保护整个 API 路由。因此,例如,如果我想保护 /api/users 我可以创建 /api/users/_middleware.ts 并在中间件中处理身份验证,而不必担心实际 api 端点中的身份验证吗?如果是这样,我将如何去做?我现在使用的库是@auth0\nextjs-auth0,所以我猜它看起来像这样? (如果我写错了,请原谅我,我在stackoverflow编辑器中这样做)
export default authMiddleware(req,res)=>{
const {user,error,isLoading} = whateverTheNameOfTheAuth0HookIs()
if(user)
{
// Allow the request to the api route
}
else
{
// Deny the request with HTTP 401
}
}
我的总体思路是否正确?
【问题讨论】:
标签: authentication next.js middleware auth0