jangular-cli
Version:
A powerful CLI tool for rapidly bootstrapping Angular 17 & Spring Boot (Java 21) applications with integrated security, services, and enterprise-ready best practices.
54 lines (47 loc) • 1.4 kB
text/typescript
import { Component, OnInit } from '@angular/core';
import { User, UserService } from '../../services/user/user.service';
import { AuthService } from '../../services/auth/auth.service';
import { Router } from '@angular/router';
import { CommonModule, NgClass, NgIf } from '@angular/common';
({
selector: 'app-dashboard',
standalone: true,
imports: [NgIf, NgClass, CommonModule],
templateUrl: './dashboard.component.html',
styleUrl: './dashboard.component.css'
})
export class DashboardComponent implements OnInit {
user: User | null = null;
loading: boolean = true;
error: string = '';
constructor(
private authService: AuthService,
private userService: UserService,
private router: Router
) { }
ngOnInit(): void {
this.loadUserProfile();
}
loadUserProfile(): void {
this.loading = true;
this.userService.getCurrentUser().subscribe({
next: (userData) => {
this.user = userData;
this.loading = false;
},
error: (err) => {
this.error = 'Failed to load user profile';
this.loading = false;
// If unauthorized, redirect to login
if (err.status === 401) {
this.authService.logout();
this.router.navigate(['/auth/login']);
}
}
});
}
logout(): void {
this.authService.logout();
this.router.navigate(['/auth/login']);
}
}