omnichannel-frontend/src/modules/auth/services/authService.js

36 lines
1013 B
JavaScript
Raw Normal View History

const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3001';
async function parseJsonResponse(response) {
const data = await response.json().catch(() => null);
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));
}