Spaces:
Sleeping
Sleeping
File size: 1,404 Bytes
0d37119 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import { useState, useEffect, useCallback } from 'react';
const BACKEND_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:8000';
export default function useGoogleAuth() {
const [authenticated, setAuthenticated] = useState(false);
const [oauthAvailable, setOauthAvailable] = useState(false);
const [loading, setLoading] = useState(true);
const checkStatus = useCallback(async () => {
try {
const res = await fetch(`${BACKEND_URL}/api/auth/status`);
const data = await res.json();
setAuthenticated(data.authenticated);
setOauthAvailable(data.oauth_available);
} catch {
setAuthenticated(false);
setOauthAvailable(false);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
checkStatus();
// Check if we just came back from OAuth callback
const params = new URLSearchParams(window.location.search);
if (params.get('auth') === 'success') {
// Clean URL and recheck
window.history.replaceState({}, '', window.location.pathname);
checkStatus();
}
}, [checkStatus]);
const login = useCallback(() => {
window.location.href = `${BACKEND_URL}/api/auth/login`;
}, []);
const logout = useCallback(async () => {
await fetch(`${BACKEND_URL}/api/auth/logout`);
setAuthenticated(false);
}, []);
return { authenticated, oauthAvailable, loading, login, logout };
}
|