feat(navbar): implement login/logout buttons based on state
This commit is contained in:
parent
325bc118ee
commit
e5bd173be9
5 changed files with 57 additions and 16 deletions
|
@ -1,15 +1,33 @@
|
|||
import { Component } from '@angular/core';
|
||||
import { AsyncPipe } from '@angular/common';
|
||||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
|
||||
import { RouterModule } from '@angular/router';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
|
||||
@Component({
|
||||
selector: 'app-navbar',
|
||||
templateUrl: './navbar.component.html',
|
||||
standalone: true,
|
||||
imports: [CommonModule, RouterModule],
|
||||
imports: [RouterModule, AsyncPipe],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class NavbarComponent {
|
||||
isMenuOpen = false;
|
||||
private keycloakService: KeycloakService = inject(KeycloakService);
|
||||
|
||||
isLoggedIn = this.keycloakService.isLoggedIn();
|
||||
|
||||
login() {
|
||||
try {
|
||||
const baseUrl = window.location.origin;
|
||||
this.keycloakService.login({ redirectUri: `${baseUrl}/home` });
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
logout() {
|
||||
this.keycloakService.logout();
|
||||
}
|
||||
|
||||
toggleMenu() {
|
||||
this.isMenuOpen = !this.isMenuOpen;
|
||||
|
|
Reference in a new issue