7c98566579
Removed the legacy indicator layout, unused chart helpers, styles and exports, the card-actions component, fetch-all and explanatory comments; one pager shared by the four worklists, shared filter-row styles, one FHIR name and CURP reader, downloads through export/, the concept-map editor on Material controls.
75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http';
|
|
import { Injectable, computed, inject, signal } from '@angular/core';
|
|
import { CanActivateFn, Router } from '@angular/router';
|
|
import { firstValueFrom } from 'rxjs';
|
|
import { ApiConfiguration } from '../api/api-configuration';
|
|
import type { Domain } from '../api/models/domain';
|
|
|
|
export interface User {
|
|
id: string;
|
|
email: string;
|
|
name: string;
|
|
is_superuser: boolean;
|
|
domains: Domain[];
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class Auth {
|
|
private readonly http = inject(HttpClient);
|
|
private readonly root = inject(ApiConfiguration).rootUrl;
|
|
private readonly current = signal<User | null | undefined>(undefined);
|
|
readonly user = this.current.asReadonly();
|
|
readonly signedIn = computed(() => !!this.current());
|
|
|
|
canSee(domain: string): boolean {
|
|
const u = this.current();
|
|
return !!u && (u.is_superuser || u.domains.includes(domain as Domain));
|
|
}
|
|
|
|
async load(): Promise<User | null> {
|
|
try {
|
|
this.current.set(await firstValueFrom(this.http.get<User>(`${this.root}/users/me`)));
|
|
} catch {
|
|
this.current.set(null);
|
|
}
|
|
return this.current() ?? null;
|
|
}
|
|
|
|
async login(email: string, password: string): Promise<string | null> {
|
|
const body = new HttpParams().set('username', email.trim()).set('password', password);
|
|
try {
|
|
await firstValueFrom(this.http.post(`${this.root}/auth/login`, body));
|
|
} catch (e) {
|
|
if (e instanceof HttpErrorResponse && e.status === 400) {
|
|
return 'Usuario o contraseña incorrectos.';
|
|
}
|
|
return 'No hay conexión con la plataforma. Intente de nuevo.';
|
|
}
|
|
await this.load();
|
|
return null;
|
|
}
|
|
|
|
async logout(): Promise<void> {
|
|
try {
|
|
await firstValueFrom(this.http.post(`${this.root}/auth/logout`, null));
|
|
} finally {
|
|
this.current.set(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
export const signedIn: CanActivateFn = async (_route, state) => {
|
|
const auth = inject(Auth);
|
|
const router = inject(Router);
|
|
const user = auth.user() === undefined ? await auth.load() : auth.user();
|
|
return user ? true : router.createUrlTree(['/login'], { queryParams: { next: state.url } });
|
|
};
|
|
|
|
export const domainAllowed: CanActivateFn = (route) => {
|
|
const auth = inject(Auth);
|
|
const domain = route.paramMap.get('domain') ?? route.data['domain'];
|
|
if (!domain || auth.canSee(domain)) return true;
|
|
const first = auth.user()?.domains[0];
|
|
return inject(Router).createUrlTree(first ? ['/indicadores', first] : ['/login']);
|
|
};
|