import { Component, OnInit } from ‘@angular/core’; import { UsuarioService } from…
- User
import { Component, OnInit } from ‘@angular/core’; import { UsuarioService } from ‘…/services/usuario.service’; import { Router } from ‘@angular/router’; import Swal from ‘sweetalert2’;
@Component({ selector: ‘app-usuario-login’, templateUrl: ‘./usuario-login.component.html’, styleUrl: ‘./usuario-login.component.css’ }) export class UsuarioLoginComponent implements OnInit{
username: string = ‘’; password: string = ‘’;
constructor(private usuarioService: UsuarioService, private router: Router) {}
ngOnInit(): void { this.verificarLogueo(); }
login(): void { this.usuarioService.login(this.username, this.password).subscribe({ next: response => { console.log(response); localStorage.setItem(‘isAuthenticated’, ‘true’); this.router.navigate([‘/dashboard’]);
const Toast = Swal.mixin({ toast: true, position: “top-end”, showConfirmButton: false, timer: 3000, timerProgressBar: true, didOpen: (toast) => { toast.onmouseenter = Swal.stopTimer; toast.onmouseleave = Swal.resumeTimer; } }); Toast.fire({ icon: “success”, title: “Inicio de sesión exitoso” });
}, error: error => { console.error(‘Login error’, error); } }); }
verificarLogueo(): void { if (localStorage.getItem(‘isAuthenticated’)) { this.router.navigate([‘/dashboard’]); } }
}
- uso angular y estoy en un componente para logueo, tengo un sweetalert2 que muestar un mensaje de confurmacion, el prblema que en el app cpmponent tengo esto ngOnInit(): void { this.checkAuthentication(); }
checkAuthentication(): void { this.isAuthenticated = !!localStorage.getItem(‘isAuthenticated’); } - que verifica la variabe que se crea en el componente login y hace que se meustre o no un ngif - <div class=“fixed-bottom” *ngIf=“isAuthenticated”>
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit{ title = 'ProyectoFinal';
isAuthenticated!: boolean;
constructor() {}
ngOnInit(): void { this.checkAuthentication(); }
checkAuthentication(): void { this.isAuthenticated = !!localStorage.getItem('isAuthenticated'); }
logout(): void { localStorage.removeItem('isAuthenticated'); window.location.href = '/login'; }
}
- Assistant