修复认证循环重定向问题
This commit is contained in:
@@ -174,6 +174,7 @@ async function postCall<R = undefined>(rawResp: Promise<ApiResponse<R>>) {
|
||||
].some(item => item.test(pathname))
|
||||
|
||||
if (match && !resp.success && resp.status === 401) {
|
||||
console.log('🚗🚗🚗🚗🚗 非正常重定向 🚗🚗🚗🚗🚗')
|
||||
redirect('/login?force=true')
|
||||
}
|
||||
|
||||
|
||||
@@ -95,35 +95,35 @@ export default function Extract(props: ExtractProps) {
|
||||
const type = useRef<'copy' | 'open'>('open')
|
||||
const onSubmit = async (values: z.infer<typeof schema>) => {
|
||||
switch (type.current) {
|
||||
case 'copy':
|
||||
const url = new URL(window.location.href).origin
|
||||
const text = `${url}${params}`
|
||||
case 'copy':
|
||||
const url = new URL(window.location.href).origin
|
||||
const text = `${url}${params}`
|
||||
|
||||
// 使用 clipboard API 复制链接
|
||||
let copied = false
|
||||
try {
|
||||
await navigator.clipboard.writeText(text)
|
||||
copied = true
|
||||
}
|
||||
catch (e) {
|
||||
console.log('剪贴板 API 调用失败,尝试备选方案')
|
||||
}
|
||||
// 使用 clipboard API 复制链接
|
||||
let copied = false
|
||||
try {
|
||||
await navigator.clipboard.writeText(text)
|
||||
copied = true
|
||||
}
|
||||
catch (e) {
|
||||
console.log('剪贴板 API 调用失败,尝试备选方案')
|
||||
}
|
||||
|
||||
// 使用 document.execCommand 作为备选方案
|
||||
if (!copied) {
|
||||
const textarea = document.createElement('textarea')
|
||||
textarea.value = text
|
||||
document.body.appendChild(textarea)
|
||||
textarea.select()
|
||||
document.execCommand('copy')
|
||||
document.body.removeChild(textarea)
|
||||
}
|
||||
// 使用 document.execCommand 作为备选方案
|
||||
if (!copied) {
|
||||
const textarea = document.createElement('textarea')
|
||||
textarea.value = text
|
||||
document.body.appendChild(textarea)
|
||||
textarea.select()
|
||||
document.execCommand('copy')
|
||||
document.body.removeChild(textarea)
|
||||
}
|
||||
|
||||
toast.success('链接已复制到剪贴板')
|
||||
break
|
||||
case 'open':
|
||||
window.open(params, '_blank')
|
||||
break
|
||||
toast.success('链接已复制到剪贴板')
|
||||
break
|
||||
case 'open':
|
||||
window.open(params, '_blank')
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,24 +28,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}`)
|
||||
// 验证访问令牌
|
||||
if (request.cookies.get('auth_token')) {
|
||||
|
||||
// 如果刷新访问令牌成功,则继续访问之前的页面
|
||||
const isLogin = request.nextUrl.pathname === '/login'
|
||||
const hasRedirect = request.nextUrl.searchParams.get('redirect')
|
||||
if (isLogin && hasRedirect) {
|
||||
return NextResponse.redirect(`${request.nextUrl.origin}${hasRedirect}`)
|
||||
}
|
||||
}
|
||||
|
||||
// 没有访问令牌不允许访问 admin 页面
|
||||
const isAdmin = [
|
||||
RegExp(`^/admin.*`),
|
||||
].some(item => item.test(request.nextUrl.pathname))
|
||||
|
||||
const accessToken = request.cookies.get('auth_token')
|
||||
|
||||
if (isAdmin && !accessToken) {
|
||||
console.log('🚗🚗🚗🚗🚗 非正常重定向 🚗🚗🚗🚗🚗')
|
||||
return NextResponse.redirect(`${request.nextUrl.origin}/login?redirect=${request.nextUrl.pathname}`)
|
||||
else {
|
||||
|
||||
// 没有访问令牌不允许访问 admin 页面
|
||||
const isAdmin = request.nextUrl.pathname.startsWith('/admin')
|
||||
if (isAdmin) {
|
||||
return NextResponse.redirect(`${request.nextUrl.origin}/login?redirect=${request.nextUrl.pathname}`)
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next({request})
|
||||
|
||||
Reference in New Issue
Block a user