Sam*_*oza 2 javascript next.js next.js13
当我没有得到 jwt 后端时,我试图进行重定向。我在 next.js 13 中处于appdir活动状态。
中间件.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(req: NextRequest) {
const token = req.cookies.get('token')
if (!token) {
return NextResponse.redirect('/login')
}
return NextResponse.next()
}
Run Code Online (Sandbox Code Playgroud)
这应该有效
\nreturn NextResponse.redirect(new URL('/login', req.url))\nRun Code Online (Sandbox Code Playgroud)\n看起来需要绝对url
\n当用户访问 \xe2\x80\x9clogin\xe2\x80\x9d 页面时,不应运行此中间件。
\nconst pathname = req.nextUrl.pathname\nRun Code Online (Sandbox Code Playgroud)\n检查登录页面的路径名,根据它是否 \xe2\x80\x9cpathname===\xe2\x80\x9clogin\xe2\x80\x9d 调用 \xe2\x80\x9cnext\xe2\x80\x9d。不要运行页面的中间件login。因为login页面不应该受到保护
| 归档时间: |
|
| 查看次数: |
3183 次 |
| 最近记录: |