diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 4207d60..6fa60d8 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,5 @@ import { Component, Inject, PLATFORM_ID } from '@angular/core'; -import { RouterOutlet } from '@angular/router'; +import { Router, RouterOutlet } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { StorageService } from './shared/services/storage.service'; import { isPlatformBrowser } from '@angular/common'; @@ -15,31 +15,49 @@ import { NotificationsComponent } from './shared/components/notifications/notifi }) export class AppComponent { direction: any; - title = 'ACONNECT-UX'; + title = 'aConnect'; - constructor(private translateService: TranslateService, private storageService: StorageService, + constructor( + private translateService: TranslateService, + private storageService: StorageService, + private router: Router, @Inject(PLATFORM_ID) private platformId: object ) { } ngOnInit() { - if (this.storageService.getItem('language')) { - const currentLanguage = this.storageService.getItem('language')!; - if (isPlatformBrowser(this.platformId)) { - this.translateService.setDefaultLang(currentLanguage); - this.translateService.use(currentLanguage); - } + if (!isPlatformBrowser(this.platformId)) return; - this.direction = this.storageService.getItem('direction'); - } - else { - if (isPlatformBrowser(this.platformId)) { - this.translateService.setDefaultLang('English'); - this.translateService.use('English'); - } + const currentLanguage = this.storageService.getItem('language') || supportedLanguages.ENGLISH; + this.storageService.setItem('language', currentLanguage); + this.translateService.setDefaultLang(currentLanguage); + this.translateService.use(currentLanguage); - this.storageService.setItem('language', supportedLanguages.ENGLISH); - this.direction = directions.LTR; - this.storageService.setItem('direction', this.direction); + this.direction = this.storageService.getItem('direction') || directions.LTR; + this.storageService.setItem('direction', this.direction); + + const userStr = this.storageService.getItem('user'); + if (userStr) { + try { + const data = JSON.parse(userStr); + if (data?.token) { + if (this.router.url === '/' || this.router.url === '/login') { + this.router.navigate(['/home/dashboard']); + } + } else { + if (this.router.url === '/') { + this.router.navigate(['/login']); + } + } + } catch { + this.storageService.removeItem('user'); + if (this.router.url === '/') { + this.router.navigate(['/login']); + } + } + } else { + if (this.router.url === '/') { + this.router.navigate(['/login']); + } } } } diff --git a/src/app/shared/guards/authentication.guard.ts b/src/app/shared/guards/authentication.guard.ts index c925c4e..d7296c9 100644 --- a/src/app/shared/guards/authentication.guard.ts +++ b/src/app/shared/guards/authentication.guard.ts @@ -1,7 +1,6 @@ import { LocationStrategy } from '@angular/common'; import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; -import { AuthenticationResponse } from '../../authenticate/authenticate'; import { AuthenticationService } from '../../services/authenticate.service'; import { CredentialService } from '../../services/credential.service'; import { FormConstants } from '../../utils/enums'; diff --git a/src/app/shared/services/storage.service.ts b/src/app/shared/services/storage.service.ts index fa38aa9..8e8fa9a 100644 --- a/src/app/shared/services/storage.service.ts +++ b/src/app/shared/services/storage.service.ts @@ -30,4 +30,8 @@ export class StorageService { localStorage.clear(); } } + + removeItem(key: string) { + localStorage.removeItem(key); + } }