新增dashboard组件

This commit is contained in:
tangly1024.com
2024-11-19 12:53:35 +08:00
parent 21d9bc03f7
commit 7b0efe5647
11 changed files with 242 additions and 95 deletions

View File

@@ -12,7 +12,9 @@ export const config = {
// 限制登录访问的路由
const isTenantRoute = createRouteMatcher([
'/user/organization-selector(.*)',
'/user/orgid/(.*)'
'/user/orgid/(.*)',
'/dashboard',
'/dashboard/(.*)'
])
// 限制权限访问的路由
@@ -32,27 +34,35 @@ const noAuthMiddleware = async (req: any, ev: any) => {
// 如果没有配置 Clerk 相关环境变量,返回一个默认响应或者继续处理请求
return NextResponse.next()
}
/**
* 鉴权中间件
*/
const authMiddleware = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
? clerkMiddleware(
(auth, req) => {
// 限制管理员路由访问权限
if (isTenantAdminRoute(req)) {
auth().protect(has => {
return (
has({ permission: 'org:sys_memberships:manage' }) ||
has({ permission: 'org:sys_domains_manage' })
)
})
? clerkMiddleware(async (auth, req) => {
const { userId } = auth()
// 处理 /dashboard 路由的登录保护
if (isTenantRoute(req)) {
if (!userId) {
// 用户未登录,重定向到 /sign-in
const url = new URL('/sign-in', req.url)
url.searchParams.set('redirectTo', req.url) // 保存重定向目标
return NextResponse.redirect(url)
}
// 限制组织路由访问权限
if (isTenantRoute(req)) auth().protect()
}
// { debug: process.env.npm_lifecycle_event === 'dev' } // 开发调试模式打印日志
)
// 处理管理员相关权限保护
if (isTenantAdminRoute(req)) {
auth().protect(has => {
return (
has({ permission: 'org:sys_memberships:manage' }) ||
has({ permission: 'org:sys_domains_manage' })
)
})
}
// 默认继续处理请求
return NextResponse.next()
})
: noAuthMiddleware
export default authMiddleware