Skip to main content

Overview

The LoginComponent handles user authentication, allowing users to log in with their credentials and redirecting them based on their role (Admin or User).

Component Metadata

Selector: app-login Imports:
  • CommonModule - Angular common directives
  • ReactiveFormsModule - Reactive forms support
  • RouterLink - Router navigation
Template: ./login.html

Properties

mensajeError

Stores error messages to display to the user when login fails or validation errors occur.

loginForm

Reactive form group containing username and password controls with required validators.

Constructor Dependencies

  • FormBuilder - Creates reactive form controls
  • AuthService - Handles authentication API calls
  • TokenStorageService - Manages authentication tokens
  • Router - Navigates to different routes after login

Lifecycle Hooks

ngOnInit

Currently empty, reserved for future initialization logic.

Methods

onSubmit

Handles form submission and authentication:
  1. Validates the form is complete
  2. Extracts username and password from the form
  3. Calls the AuthService.login() method
  4. On success:
    • Saves the session using AuthService.saveSession()
    • Redirects to /libros if user is Admin
    • Redirects to /catalogo if user is a regular User
  5. On error: Displays error message “Usuario o contraseña incorrectos”
Example usage in template:

Full Source Code