'use client' import { useEffect } from 'react' import { AppShell, Group, Text, Button, Avatar, Paper, Stack, Badge, Code, Divider, SimpleGrid, Menu, } from '@mantine/core' import { IconLogout, IconUser, IconShield, IconServer, IconKey, } from '@tabler/icons-react' interface UserInfo { id: string account: string name: string phone: string roles: string[] } export default function DashboardPage({ user }: { user: UserInfo | null }) { // 未登录 -> 跳转 SSO useEffect(() => { if (!user) { const appId = 'app_demo_001' const callbackUrl = encodeURIComponent('http://localhost:4000/callback') window.location.href = `http://localhost:5501/login?app_id=${appId}&app_url=${callbackUrl}` } }, [user]) if (!user) { return ( 正在跳转到 SSO 登录页面... ) } const handleLogout = async () => { await fetch('/api/logout', { method: 'POST' }) // 跳到 SSO 登录页(带上 app_id 让 SSO 知道是哪个应用) const appId = 'app_demo_001' const callbackUrl = encodeURIComponent('http://localhost:4000/callback') window.location.href = `http://localhost:5501/login?app_id=${appId}&app_url=${callbackUrl}` } return ( 外部应用 Demo 已登录 {user.name?.[0] || 'U'} {user.name} }>{user.account} } color="red" onClick={handleLogout}> 退出登录 登录成功! 您已通过 Mock SSO 认证登录到此应用。 用户信息 ID {user.id} 账号 {user.account} 姓名 {user.name} 手机 {user.phone} 角色权限 {user.roles?.map((role: string) => ( {role} ))} SSO 流程说明 1. 您打开此应用(http://localhost:4000 2. 检测到未登录,自动跳转到 Mock SSO(http://localhost:5501/login 3. 在 SSO 页面完成登录,SSO 返回 sso_code 4. 此应用用 sso_code 调用 POST /api/auth/verify 换取用户信息 5. 用户信息存入 Cookie,显示此页面 ) }