完善错误提示
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import {NextRequest, NextResponse} from 'next/server'
|
||||
import {refreshAuth} from '@/actions/auth'
|
||||
import { UnauthorizedError } from './lib/api'
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
@@ -28,15 +29,23 @@ export async function middleware(request: NextRequest) {
|
||||
console.log('❌ 刷新访问令牌失败', e)
|
||||
}
|
||||
|
||||
// 如果刷新访问令牌成功,则继续访问之前的页面
|
||||
const isLogin = request.nextUrl.pathname === '/login'
|
||||
const redirect = request.nextUrl.searchParams.get('redirect')
|
||||
if (isLogin && redirect) {
|
||||
console.log('redirect to', redirect)
|
||||
return NextResponse.redirect(`${request.nextUrl.origin}${redirect}`)
|
||||
}
|
||||
|
||||
// 没有访问令牌不允许访问 admin 页面
|
||||
const match = [
|
||||
const isAdmin = [
|
||||
RegExp(`^/admin.*`),
|
||||
].some(item => item.test(request.nextUrl.pathname))
|
||||
|
||||
const accessToken = request.cookies.get('auth_token')
|
||||
|
||||
if (match && !accessToken) {
|
||||
console.log('redirect!!!!!!!!!')
|
||||
if (isAdmin && !accessToken) {
|
||||
console.log('🚗🚗🚗🚗🚗 非正常重定向 🚗🚗🚗🚗🚗')
|
||||
return NextResponse.redirect(`${request.nextUrl.origin}/login?redirect=${request.nextUrl.pathname}`)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user