Skip to main content

Overview

The NavbarComponent is a shared navigation bar displayed across the application when users are authenticated. It provides role-based navigation links and logout functionality. Selector: app-navbar Location: src/app/shared/components/navbar/navbar.ts:13 Type: Standalone component

Metadata

Constructor

Uses Angular’s inject() function to obtain service instances.

Properties

isLoggedIn

Dynamic getter that determines if the user is logged in and not on authentication pages.
Returns: boolean - True if user has a token and is not on login/registro pages Logic:
  • Checks for token existence via TokenStorageService
  • Excludes /auth/login and /auth/registro routes
  • Used to conditionally display navigation elements
Example Usage in Template:

usuarioNombre

Dynamic getter that returns the current user’s username.
Returns: string - Username from localStorage or “Usuario” as fallback Storage Key: username Example Usage:

rol

Dynamic getter that returns the current user’s role.
Returns: string - Role from localStorage (e.g., “ROLE_ADMIN”, “ROLE_USER”) Storage Key: role

isAdmin

Checks if the current user has admin role.
Returns: boolean - True if role is “ROLE_ADMIN” Example Usage:

isUser

Checks if the current user has regular user role.
Returns: boolean - True if role is “ROLE_USER” Example Usage:

Methods

ngOnInit()

Lifecycle hook called after component initialization.
Currently empty implementation. Available for future initialization logic.

logout()

Logs out the current user and redirects to login page.
Actions:
  1. Calls TokenStorageService.signOut() to clear the JWT token
  2. Clears all localStorage data
  3. Navigates to /auth/login
Example Usage in Template:
Example Implementation:

Template Structure

The navbar typically includes:
  • Logo/Brand: Application name or logo
  • Role-based Navigation: Different links for admins vs regular users
  • User Info: Display username
  • Logout Button: Triggers the logout() method
Example Admin Navigation:

Usage

The navbar is typically included in the root AppComponent template: