主要更新: - 更新代理商端文档,明确项目由品牌方分配流程 - 新增Brief配置详情页(已配置)设计稿 - 完善工作台紧急待办中品牌新任务功能 - 整理Pencil设计文件中代理商端页面顺序 - 新增后端FastAPI框架及核心API - 新增前端Next.js页面和组件库 - 添加.gitignore排除构建和缓存文件 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import { useAuth } from '@/contexts/AuthContext'
|
|
import { UserRole } from '@/types/auth'
|
|
|
|
interface AuthGuardProps {
|
|
children: React.ReactNode
|
|
allowedRoles?: UserRole[]
|
|
}
|
|
|
|
export function AuthGuard({ children, allowedRoles }: AuthGuardProps) {
|
|
const router = useRouter()
|
|
const { user, isAuthenticated, isLoading } = useAuth()
|
|
|
|
useEffect(() => {
|
|
if (!isLoading) {
|
|
if (!isAuthenticated) {
|
|
router.push('/login')
|
|
return
|
|
}
|
|
|
|
if (allowedRoles && user && !allowedRoles.includes(user.role)) {
|
|
// 重定向到用户对应的默认页面
|
|
switch (user.role) {
|
|
case 'creator':
|
|
router.push('/creator')
|
|
break
|
|
case 'agency':
|
|
router.push('/agency')
|
|
break
|
|
case 'brand':
|
|
router.push('/brand')
|
|
break
|
|
default:
|
|
router.push('/login')
|
|
}
|
|
}
|
|
}
|
|
}, [isLoading, isAuthenticated, user, allowedRoles, router])
|
|
|
|
// 加载中
|
|
if (isLoading) {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center">
|
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
// 未认证
|
|
if (!isAuthenticated) {
|
|
return null
|
|
}
|
|
|
|
// 角色不匹配
|
|
if (allowedRoles && user && !allowedRoles.includes(user.role)) {
|
|
return null
|
|
}
|
|
|
|
return <>{children}</>
|
|
}
|