2026-05-18 19:11:01 -03:00
|
|
|
import { useEffect, useState } from 'react';
|
2026-03-19 18:22:18 -03:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2026-05-18 19:11:01 -03:00
|
|
|
import {
|
|
|
|
|
getAuthConfig,
|
|
|
|
|
loginWithAd,
|
|
|
|
|
startMicrosoftLogin,
|
|
|
|
|
storeAuthSession,
|
|
|
|
|
} from '../services/authService';
|
2026-06-01 16:56:09 -03:00
|
|
|
import { isAuthenticated } from '../services/sessionService';
|
2026-03-19 18:22:18 -03:00
|
|
|
|
2026-05-29 12:27:37 -03:00
|
|
|
function decodeAuthPayload(payload) {
|
|
|
|
|
const base64 = payload.replace(/-/g, '+').replace(/_/g, '/');
|
|
|
|
|
const paddedBase64 = base64.padEnd(base64.length + ((4 - (base64.length % 4)) % 4), '=');
|
|
|
|
|
|
|
|
|
|
return JSON.parse(atob(paddedBase64));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 18:22:18 -03:00
|
|
|
export function useLogin() {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
2026-05-18 19:11:01 -03:00
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
const [providers, setProviders] = useState({ ldap: true, microsoft: false });
|
2026-03-19 18:22:18 -03:00
|
|
|
|
2026-05-18 19:11:01 -03:00
|
|
|
useEffect(() => {
|
2026-06-01 16:56:09 -03:00
|
|
|
if (!window.location.hash.includes('auth=') && isAuthenticated()) {
|
|
|
|
|
navigate('/home', { replace: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 19:11:01 -03:00
|
|
|
getAuthConfig()
|
|
|
|
|
.then((config) => setProviders(config.providers || { ldap: true, microsoft: false }))
|
|
|
|
|
.catch(() => setProviders({ ldap: true, microsoft: false }));
|
2026-06-01 16:56:09 -03:00
|
|
|
}, [navigate]);
|
2026-05-18 19:11:01 -03:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-29 12:27:37 -03:00
|
|
|
const hashParams = new URLSearchParams(window.location.hash.replace(/^#/, ''));
|
|
|
|
|
const authPayload = hashParams.get('auth');
|
|
|
|
|
|
|
|
|
|
if (authPayload) {
|
|
|
|
|
try {
|
|
|
|
|
storeAuthSession(decodeAuthPayload(authPayload));
|
|
|
|
|
window.history.replaceState({}, document.title, window.location.pathname);
|
|
|
|
|
navigate('/home', { replace: true });
|
|
|
|
|
return;
|
|
|
|
|
} catch {
|
|
|
|
|
window.history.replaceState({}, document.title, window.location.pathname);
|
2026-06-01 16:56:09 -03:00
|
|
|
setError('Nao foi possivel concluir o login Microsoft.');
|
2026-05-29 12:27:37 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 16:56:09 -03:00
|
|
|
if (window.location.search.includes('token=') || window.location.search.includes('user=')) {
|
2026-05-18 19:11:01 -03:00
|
|
|
window.history.replaceState({}, document.title, window.location.pathname);
|
2026-06-01 16:56:09 -03:00
|
|
|
setError('Fluxo de login expirado. Tente novamente.');
|
2026-05-18 19:11:01 -03:00
|
|
|
}
|
|
|
|
|
}, [navigate]);
|
|
|
|
|
|
|
|
|
|
async function login(credentials) {
|
2026-03-19 18:22:18 -03:00
|
|
|
setIsSubmitting(true);
|
2026-05-18 19:11:01 -03:00
|
|
|
setError('');
|
2026-03-19 18:22:18 -03:00
|
|
|
|
|
|
|
|
try {
|
2026-05-18 19:11:01 -03:00
|
|
|
const authResult = await loginWithAd(credentials);
|
|
|
|
|
storeAuthSession(authResult);
|
2026-03-19 18:22:18 -03:00
|
|
|
navigate('/home');
|
2026-05-18 19:11:01 -03:00
|
|
|
} catch (loginError) {
|
|
|
|
|
setError(loginError.message || 'Falha ao autenticar.');
|
2026-03-19 18:22:18 -03:00
|
|
|
} finally {
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
isSubmitting,
|
2026-05-18 19:11:01 -03:00
|
|
|
error,
|
|
|
|
|
providers,
|
2026-03-19 18:22:18 -03:00
|
|
|
login,
|
2026-05-18 19:11:01 -03:00
|
|
|
startMicrosoftLogin,
|
2026-03-19 18:22:18 -03:00
|
|
|
};
|
|
|
|
|
}
|