| 6 | import { fetchJson } from '../services/api'; |
| 7 | |
| 8 | export default function SSOEntry() { |
| 9 | const { t } = useTranslation(); |
| 10 | const [searchParams] = useSearchParams(); |
| 11 | const navigate = useNavigate(); |
| 12 | const setAuth = useAuthStore((s) => s.setAuth); |
| 13 | const sid = searchParams.get('sid'); |
| 14 | const complete = searchParams.get('complete') === '1'; |
| 15 | const [error, setError] = useState(''); |
| 16 | const [providers, setProviders] = useState<any[]>([]); |
| 17 | const [loading, setLoading] = useState(true); |
| 18 | // Initialize polling=true when complete=1 to avoid briefly showing |
| 19 | // "No SSO providers configured." before the first poll completes. |
| 20 | const [polling, setPolling] = useState(complete); |
| 21 | |
| 22 | useEffect(() => { |
| 23 | if (!sid) { |
| 24 | setError(t('sso.missingSessionId')); |
| 25 | setLoading(false); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | // 1. Mark as scanned |
| 30 | fetchJson(`/sso/session/${sid}/scan`, { method: 'PUT' }).catch(() => {}); |
| 31 | |
| 32 | // 2. Load SSO configs (skip auto-redirect on completion step) |
| 33 | if (!complete) { |
| 34 | fetchJson<any[]>(`/sso/config?sid=${sid}`) |
| 35 | .then(data => { |
| 36 | setProviders(data); |
| 37 | setLoading(false); |
| 38 | |
| 39 | // 3. Detect UA and Auto-redirect if possible |
| 40 | const ua = navigator.userAgent.toLowerCase(); |
| 41 | let targetProvider = ''; |
| 42 | |
| 43 | if (ua.includes('lark') || ua.includes('feishu')) { |
| 44 | targetProvider = 'feishu'; |
| 45 | } else if (ua.includes('dingtalk')) { |
| 46 | targetProvider = 'dingtalk'; |
| 47 | } else if (ua.includes('wxwork')) { |
| 48 | targetProvider = 'wecom'; |
| 49 | } |
| 50 | |
| 51 | if (targetProvider) { |
| 52 | const p = data.find(it => it.provider_type === targetProvider); |
| 53 | if (p && p.url) { |
| 54 | window.location.href = p.url; |
| 55 | } |
| 56 | } |
| 57 | }) |
| 58 | .catch(() => { |
| 59 | setError(t('sso.failedToLoadConfig')); |
| 60 | setLoading(false); |
| 61 | }); |
| 62 | } else { |
| 63 | setLoading(false); |
| 64 | } |
| 65 | }, [sid, complete]); |