frontend: Refactor authentication handling in components to use updated AuthService methods

This commit is contained in:
juancarmore 2025-03-21 01:41:20 +01:00
parent 411388ba53
commit 280dbea6dc
4 changed files with 23 additions and 8 deletions

View File

@ -24,6 +24,6 @@ export class ConsoleComponent {
constructor(private authService: AuthService) {} constructor(private authService: AuthService) {}
async logout() { async logout() {
await this.authService.adminLogout(); await this.authService.logout('console/login');
} }
} }

View File

@ -43,7 +43,15 @@ export class ConsoleLoginComponent {
const { username, password } = this.loginForm.value; const { username, password } = this.loginForm.value;
try { try {
await this.authService.adminLogin(username!, password!); await this.authService.login(username!, password!);
// Check if the user has the expected role
if (!this.authService.isAdmin()) {
this.authService.logout();
this.loginErrorMessage = 'Invalid username or password';
return;
}
this.router.navigate(['console']); this.router.navigate(['console']);
} catch (error) { } catch (error) {
if ((error as HttpErrorResponse).status === 429) { if ((error as HttpErrorResponse).status === 429) {

View File

@ -44,8 +44,15 @@ export class LoginComponent {
const { username, password } = this.loginForm.value; const { username, password } = this.loginForm.value;
try { try {
// TODO: Replace with user login await this.authService.login(username!, password!);
await this.authService.adminLogin(username!, password!);
// Check if the user has the expected role
if (this.authService.isAdmin()) {
this.authService.logout();
this.loginErrorMessage = 'Invalid username or password';
return;
}
this.router.navigate(['']); this.router.navigate(['']);
} catch (error) { } catch (error) {
if ((error as HttpErrorResponse).status === 429) { if ((error as HttpErrorResponse).status === 429) {

View File

@ -6,7 +6,7 @@ import { MatIconButton, MatButton } from '@angular/material/button';
import { NgClass } from '@angular/common'; import { NgClass } from '@angular/common';
import { MatToolbar } from '@angular/material/toolbar'; import { MatToolbar } from '@angular/material/toolbar';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { ContextService, HttpService } from '../../services/index'; import { AuthService, ContextService, HttpService } from '../../services/index';
import { OpenViduMeetRoom, OpenViduMeetRoomOptions } from '../../typings/ce/room'; import { OpenViduMeetRoom, OpenViduMeetRoomOptions } from '../../typings/ce/room';
import { animals, colors, Config, uniqueNamesGenerator } from 'unique-names-generator'; import { animals, colors, Config, uniqueNamesGenerator } from 'unique-names-generator';
@ -30,6 +30,7 @@ export class RoomCreatorComponent implements OnInit {
constructor( constructor(
private router: Router, private router: Router,
private httpService: HttpService, private httpService: HttpService,
private authService: AuthService,
private contextService: ContextService private contextService: ContextService
) {} ) {}
@ -38,8 +39,7 @@ export class RoomCreatorComponent implements OnInit {
this.openviduLogoUrl = this.contextService.getOpenViduLogoUrl(); this.openviduLogoUrl = this.contextService.getOpenViduLogoUrl();
this.backgroundImageUrl = this.contextService.getBackgroundImageUrl(); this.backgroundImageUrl = this.contextService.getBackgroundImageUrl();
// TODO: Retrieve actual username this.username = this.authService.getUsername();
this.username = 'user';
} }
generateRoomName(event: any) { generateRoomName(event: any) {
@ -53,7 +53,7 @@ export class RoomCreatorComponent implements OnInit {
async logout() { async logout() {
try { try {
await this.httpService.userLogout(); await this.authService.logout('login');
} catch (error) { } catch (error) {
console.error('Error doing logout ', error); console.error('Error doing logout ', error);
} }