Overview
TheNavbarComponent 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
inject() function to obtain service instances.
Properties
isLoggedIn
Dynamic getter that determines if the user is logged in and not on authentication pages.boolean - True if user has a token and is not on login/registro pages
Logic:
- Checks for token existence via
TokenStorageService - Excludes
/auth/loginand/auth/registroroutes - Used to conditionally display navigation elements
usuarioNombre
Dynamic getter that returns the current user’s username.string - Username from localStorage or “Usuario” as fallback
Storage Key: username
Example Usage:
rol
Dynamic getter that returns the current user’s role.string - Role from localStorage (e.g., “ROLE_ADMIN”, “ROLE_USER”)
Storage Key: role
isAdmin
Checks if the current user has admin role.boolean - True if role is “ROLE_ADMIN”
Example Usage:
isUser
Checks if the current user has regular user role.boolean - True if role is “ROLE_USER”
Example Usage:
Methods
ngOnInit()
Lifecycle hook called after component initialization.logout()
Logs out the current user and redirects to login page.- Calls
TokenStorageService.signOut()to clear the JWT token - Clears all localStorage data
- Navigates to
/auth/login
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
Usage
The navbar is typically included in the rootAppComponent template:
Related
- TokenStorageService - Manages JWT token storage
- AuthService - Handles authentication logic
- Authorization Guards - Protect routes based on roles