43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
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}`);
|
|
}
|
|
}
|