Conectar dialog by kind of source; plain-language canvas; login page and guard; loading indicators as the old app; survival curves of her clinical scenarios in the CaMa record
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
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';
|
||||
|
||||
/** Who is signed in, and the domains the app shows them. */
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
is_superuser: boolean;
|
||||
domains: Domain[];
|
||||
}
|
||||
|
||||
/** The web app's sign-in (the API's fastapi-users, a session cookie). It decides only what
|
||||
* the app shows: the API answers without a session. */
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class Auth {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly root = inject(ApiConfiguration).rootUrl;
|
||||
/** undefined: not asked yet; null: nobody signed in. */
|
||||
private readonly current = signal<User | null | undefined>(undefined);
|
||||
readonly user = this.current.asReadonly();
|
||||
readonly signedIn = computed(() => !!this.current());
|
||||
|
||||
/** Whether the signed-in user may see the domain (an administrator sees every one). */
|
||||
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;
|
||||
}
|
||||
|
||||
/** Signs in; a wrong user or password comes back as words for the form. */
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Pages show only to someone signed in; the others go to the sign-in page and come back. */
|
||||
export const signedIn: CanActivateFn = async (_route, state) => {
|
||||
const auth = inject(Auth);
|
||||
const user = auth.user() === undefined ? await auth.load() : auth.user();
|
||||
return user
|
||||
? true
|
||||
: inject(Router).createUrlTree(['/login'], { queryParams: { next: state.url } });
|
||||
};
|
||||
|
||||
/** A domain's pages show only to whom may see it. */
|
||||
export const domainAllowed: CanActivateFn = (route) => {
|
||||
const auth = inject(Auth);
|
||||
const domain = route.paramMap.get('domain');
|
||||
if (!domain || auth.canSee(domain)) return true;
|
||||
const first = auth.user()?.domains[0];
|
||||
return inject(Router).createUrlTree(first ? ['/indicadores', first] : ['/login']);
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
<section class="login" aria-labelledby="login-title">
|
||||
<div class="box">
|
||||
<img class="logo" src="assets/branding/isssteleon-logo.png" alt="ISSSTELEON" />
|
||||
<h1 class="title" id="login-title">Inicio de sesión</h1>
|
||||
<form class="form" (ngSubmit)="enter()" novalidate>
|
||||
<mat-form-field appearance="outline" class="field" subscriptSizing="dynamic">
|
||||
<input
|
||||
matInput
|
||||
name="email"
|
||||
[(ngModel)]="email"
|
||||
placeholder="Usuario"
|
||||
aria-label="Usuario"
|
||||
autocomplete="username"
|
||||
autocapitalize="none"
|
||||
spellcheck="false"
|
||||
/>
|
||||
</mat-form-field>
|
||||
<mat-form-field appearance="outline" class="field" subscriptSizing="dynamic">
|
||||
<input
|
||||
matInput
|
||||
name="password"
|
||||
[(ngModel)]="password"
|
||||
[type]="show() ? 'text' : 'password'"
|
||||
placeholder="Contraseña"
|
||||
aria-label="Contraseña"
|
||||
autocomplete="current-password"
|
||||
/>
|
||||
<button
|
||||
mat-icon-button
|
||||
matSuffix
|
||||
type="button"
|
||||
class="eye"
|
||||
(click)="show.set(!show())"
|
||||
[attr.aria-label]="show() ? 'Ocultar contraseña' : 'Mostrar contraseña'"
|
||||
[attr.aria-pressed]="show()"
|
||||
>
|
||||
<mat-icon>{{ show() ? 'visibility_off' : 'visibility' }}</mat-icon>
|
||||
</button>
|
||||
</mat-form-field>
|
||||
@if (error(); as message) {
|
||||
<p class="error" role="alert">{{ message }}</p>
|
||||
}
|
||||
<button type="submit" class="enter" [disabled]="!email.trim() || !password || sending()">
|
||||
{{ sending() ? 'Ingresando…' : 'Ingresar' }}
|
||||
</button>
|
||||
</form>
|
||||
<p class="legal">©{{ year }} Todos los derechos reservados.</p>
|
||||
</div>
|
||||
</section>
|
||||
@@ -0,0 +1,141 @@
|
||||
:host {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
--login-background: #f8f9fa;
|
||||
--login-border: #e9ecef;
|
||||
}
|
||||
|
||||
.login {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
flex: 1;
|
||||
justify-content: center;
|
||||
padding: 72px 24px 56px;
|
||||
}
|
||||
|
||||
.box {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
max-width: 340px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* The brand opens the screen and is the biggest thing on it. */
|
||||
.logo {
|
||||
align-self: center;
|
||||
animation: logo-in 600ms ease-out both;
|
||||
display: block;
|
||||
width: 260px;
|
||||
}
|
||||
|
||||
@keyframes logo-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(-8px);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.logo {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* The title between two rules. */
|
||||
.title {
|
||||
align-items: center;
|
||||
color: var(--color-text);
|
||||
display: flex;
|
||||
font-family: var(--font-heading);
|
||||
font-size: 1.375rem;
|
||||
font-weight: 600;
|
||||
gap: 16px;
|
||||
margin: 0 0 8px;
|
||||
white-space: nowrap;
|
||||
|
||||
&::before,
|
||||
&::after {
|
||||
background: var(--color-border);
|
||||
content: '';
|
||||
flex: 1;
|
||||
height: 1px;
|
||||
}
|
||||
}
|
||||
|
||||
.form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.field {
|
||||
--mat-form-field-container-height: 44px;
|
||||
--mat-form-field-container-vertical-padding: 12px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.eye {
|
||||
color: var(--color-text-secondary);
|
||||
}
|
||||
|
||||
.error {
|
||||
color: var(--mat-sys-error);
|
||||
font-size: 0.875rem;
|
||||
margin: -4px 0 0;
|
||||
}
|
||||
|
||||
/* One column with the fields; its border turns to the action color once the form is complete. */
|
||||
.enter {
|
||||
align-items: center;
|
||||
background: var(--login-background);
|
||||
border: 1px solid var(--login-border);
|
||||
border-radius: 10px;
|
||||
color: var(--color-text-secondary);
|
||||
cursor: default;
|
||||
display: flex;
|
||||
font: inherit;
|
||||
font-size: 0.9375rem;
|
||||
font-weight: 500;
|
||||
justify-content: center;
|
||||
margin-top: 8px;
|
||||
min-height: 46px;
|
||||
transition:
|
||||
background-color 260ms ease,
|
||||
border-color 260ms ease,
|
||||
color 260ms ease;
|
||||
width: 100%;
|
||||
|
||||
&:not(:disabled) {
|
||||
border-color: var(--color-action);
|
||||
color: var(--color-text);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
&:hover:not(:disabled) {
|
||||
background: var(--color-action-hover);
|
||||
}
|
||||
|
||||
&:focus-visible {
|
||||
outline: 2px solid var(--color-action);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.legal {
|
||||
color: var(--color-text-secondary);
|
||||
font-size: 0.8125rem;
|
||||
margin: 16px 0 0;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.login {
|
||||
padding: 48px 16px;
|
||||
}
|
||||
|
||||
.logo {
|
||||
width: 208px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Component, inject, input, signal } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { Router } from '@angular/router';
|
||||
import { Auth } from './auth';
|
||||
|
||||
/** "Inicio de sesión": the brand, two fields and one button. */
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
imports: [FormsModule, MatButtonModule, MatFormFieldModule, MatIconModule, MatInputModule],
|
||||
templateUrl: './login.html',
|
||||
styleUrl: './login.scss',
|
||||
})
|
||||
export class Login {
|
||||
private readonly auth = inject(Auth);
|
||||
private readonly router = inject(Router);
|
||||
/** Where to go once signed in (the page that asked for it). */
|
||||
readonly next = input<string>('');
|
||||
protected email = '';
|
||||
protected password = '';
|
||||
protected readonly show = signal(false);
|
||||
protected readonly sending = signal(false);
|
||||
protected readonly error = signal('');
|
||||
protected readonly year = new Date().getFullYear();
|
||||
|
||||
protected async enter(): Promise<void> {
|
||||
if (!this.email.trim() || !this.password || this.sending()) return;
|
||||
this.sending.set(true);
|
||||
this.error.set('');
|
||||
const problem = await this.auth.login(this.email, this.password);
|
||||
this.sending.set(false);
|
||||
if (problem) {
|
||||
this.error.set(problem);
|
||||
return;
|
||||
}
|
||||
const first = this.auth.user()?.domains[0] ?? 'cacu';
|
||||
await this.router.navigateByUrl(this.next() || `/indicadores/${first}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user