aaeea7a7b0
The WebApi no longer authenticates, so the login validates against the accounts in auth.service.ts and the session lives in sessionStorage. The guard, the domain guard and the filtering of the import workspace and governance stay as they were; the interceptor and the /auth proxy entries go away because nothing answers 401.
62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
import { Component, OnInit, inject, signal } from '@angular/core';
|
|
import { FormsModule } from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { AuthService } from './auth.service';
|
|
|
|
/**
|
|
* Inicio de sesión: valida contra las cuentas locales de `AuthService` y, si el
|
|
* usuario ya tiene sesión, salta al destino. `returnUrl` lo pone el guard cuando
|
|
* una ruta protegida lo trajo aquí.
|
|
*/
|
|
@Component({
|
|
selector: 'app-login-page',
|
|
imports: [FormsModule, MatButtonModule, MatFormFieldModule, MatIconModule, MatInputModule],
|
|
styleUrl: './login-page.scss',
|
|
templateUrl: './login-page.html',
|
|
})
|
|
export class LoginPage implements OnInit {
|
|
private readonly auth = inject(AuthService);
|
|
private readonly router = inject(Router);
|
|
private readonly ruta = inject(ActivatedRoute);
|
|
|
|
protected usuario = '';
|
|
protected contrasena = '';
|
|
protected readonly verContrasena = signal(false);
|
|
protected readonly enviando = signal(false);
|
|
protected readonly error = signal<string | null>(null);
|
|
protected readonly anio = new Date().getFullYear();
|
|
|
|
ngOnInit(): void {
|
|
// Quien ya inició sesión y abre /login vuelve derecho a la aplicación.
|
|
this.auth.sesion().subscribe((usuario) => {
|
|
if (usuario) void this.router.navigateByUrl(this.destino());
|
|
});
|
|
}
|
|
|
|
protected ingresar(): void {
|
|
if (!this.usuario.trim() || !this.contrasena || this.enviando()) return;
|
|
this.enviando.set(true);
|
|
this.error.set(null);
|
|
this.auth.login(this.usuario.trim(), this.contrasena).subscribe({
|
|
next: () => void this.router.navigateByUrl(this.destino()),
|
|
error: () => {
|
|
this.enviando.set(false);
|
|
this.error.set('Usuario o contraseña incorrectos.');
|
|
},
|
|
});
|
|
}
|
|
|
|
protected mostrarContrasena(): void {
|
|
this.verContrasena.update((visible) => !visible);
|
|
}
|
|
|
|
private destino(): string {
|
|
const pedido = this.ruta.snapshot.queryParamMap.get('returnUrl');
|
|
return pedido?.startsWith('/') && !pedido.startsWith('/login') ? pedido : '/';
|
|
}
|
|
}
|