()
| 12 | } |
| 13 | |
| 14 | export default function OAuthCallback() { |
| 15 | const { t } = useTranslation(); |
| 16 | const navigate = useNavigate(); |
| 17 | const { provider = '' } = useParams(); |
| 18 | const setAuth = useAuthStore((s) => s.setAuth); |
| 19 | const [error, setError] = useState(''); |
| 20 | const [tenants, setTenants] = useState<TenantChoice[] | null>(null); |
| 21 | const [pendingToken, setPendingToken] = useState(''); |
| 22 | const [loading, setLoading] = useState(false); |
| 23 | |
| 24 | useEffect(() => { |
| 25 | if (tenants) return; // Already showing selection UI |
| 26 | |
| 27 | const code = new URLSearchParams(window.location.search).get('code'); |
| 28 | const state = new URLSearchParams(window.location.search).get('state') || ''; |
| 29 | const oauthError = new URLSearchParams(window.location.search).get('error'); |
| 30 | |
| 31 | if (oauthError) { |
| 32 | setError(oauthError); |
| 33 | return; |
| 34 | } |
| 35 | if (!provider || !code) { |
| 36 | setError(t('oauth.missingParams')); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | fetchJson<any>(`/auth/${provider}/callback`, { |
| 41 | method: 'POST', |
| 42 | body: JSON.stringify({ |
| 43 | code, |
| 44 | state, |
| 45 | redirect_uri: `${window.location.origin}/oauth/callback/${provider}`, |
| 46 | }), |
| 47 | }) |
| 48 | .then((res) => { |
| 49 | // Step 1 result: multi-tenant selection needed |
| 50 | if (res.requires_tenant_selection) { |
| 51 | setTenants(res.tenants); |
| 52 | setPendingToken(res.pending_token); |
| 53 | return; |
| 54 | } |
| 55 | // Normal single-tenant login |
| 56 | setAuth(res.user, res.access_token); |
| 57 | if (res.needs_company_setup || !res.user?.tenant_id) { |
| 58 | navigate('/setup-company', { replace: true }); |
| 59 | return; |
| 60 | } |
| 61 | navigate('/', { replace: true }); |
| 62 | }) |
| 63 | .catch((err: any) => { |
| 64 | setError(err.message || t('oauth.oauthLoginFailed')); |
| 65 | }); |
| 66 | }, [navigate, provider, setAuth, t, tenants]); |
| 67 | |
| 68 | const handleTenantSelect = async (tenantId: string) => { |
| 69 | setLoading(true); |
| 70 | setError(''); |
| 71 | try { |
nothing calls this directly
no test coverage detected