2026-05-18 19:11:01 -03:00
|
|
|
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3001';
|
2026-03-19 18:22:18 -03:00
|
|
|
|
2026-05-18 19:11:01 -03:00
|
|
|
async function parseJsonResponse(response) {
|
|
|
|
|
const data = await response.json().catch(() => null);
|
2026-03-19 18:22:18 -03:00
|
|
|
|
2026-05-18 19:11:01 -03:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(data?.message || 'Nao foi possivel autenticar.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAuthConfig() {
|
|
|
|
|
const response = await fetch(`${API_BASE_URL}/auth/config`);
|
|
|
|
|
return parseJsonResponse(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function loginWithAd(credentials) {
|
|
|
|
|
const response = await fetch(`${API_BASE_URL}/auth/login`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify(credentials),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return parseJsonResponse(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function startMicrosoftLogin() {
|
|
|
|
|
window.location.href = `${API_BASE_URL}/auth/oauth/microsoft/start`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function storeAuthSession(authResult) {
|
|
|
|
|
window.localStorage.setItem('authToken', authResult.token);
|
|
|
|
|
window.localStorage.setItem('authUser', JSON.stringify(authResult.user));
|
2026-03-19 18:22:18 -03:00
|
|
|
}
|