Enhance app initialization and storage service

Refactored AppComponent initialization to set default language and direction, handle user authentication state, and redirect accordingly. Added removeItem method to StorageService for item removal. Also removed an unused import from authentication.guard.ts.
dev-pending-20-01-2026-v1
Naeem Ullah 2 weeks ago
parent 6df0195ed8
commit 879ed17c49

@ -1,5 +1,5 @@
import { Component, Inject, PLATFORM_ID } from '@angular/core'; 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 { TranslateService } from '@ngx-translate/core';
import { StorageService } from './shared/services/storage.service'; import { StorageService } from './shared/services/storage.service';
import { isPlatformBrowser } from '@angular/common'; import { isPlatformBrowser } from '@angular/common';
@ -15,31 +15,49 @@ import { NotificationsComponent } from './shared/components/notifications/notifi
}) })
export class AppComponent { export class AppComponent {
direction: any; 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 @Inject(PLATFORM_ID) private platformId: object
) { } ) { }
ngOnInit() { ngOnInit() {
if (this.storageService.getItem('language')) { if (!isPlatformBrowser(this.platformId)) return;
const currentLanguage = this.storageService.getItem('language')!;
if (isPlatformBrowser(this.platformId)) { const currentLanguage = this.storageService.getItem('language') || supportedLanguages.ENGLISH;
this.storageService.setItem('language', currentLanguage);
this.translateService.setDefaultLang(currentLanguage); this.translateService.setDefaultLang(currentLanguage);
this.translateService.use(currentLanguage); this.translateService.use(currentLanguage);
}
this.direction = this.storageService.getItem('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 { } else {
if (isPlatformBrowser(this.platformId)) { if (this.router.url === '/') {
this.translateService.setDefaultLang('English'); this.router.navigate(['/login']);
this.translateService.use('English'); }
}
} catch {
this.storageService.removeItem('user');
if (this.router.url === '/') {
this.router.navigate(['/login']);
}
}
} else {
if (this.router.url === '/') {
this.router.navigate(['/login']);
} }
this.storageService.setItem('language', supportedLanguages.ENGLISH);
this.direction = directions.LTR;
this.storageService.setItem('direction', this.direction);
} }
} }
} }

@ -1,7 +1,6 @@
import { LocationStrategy } from '@angular/common'; import { LocationStrategy } from '@angular/common';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { AuthenticationResponse } from '../../authenticate/authenticate';
import { AuthenticationService } from '../../services/authenticate.service'; import { AuthenticationService } from '../../services/authenticate.service';
import { CredentialService } from '../../services/credential.service'; import { CredentialService } from '../../services/credential.service';
import { FormConstants } from '../../utils/enums'; import { FormConstants } from '../../utils/enums';

@ -30,4 +30,8 @@ export class StorageService {
localStorage.clear(); localStorage.clear();
} }
} }
removeItem(key: string) {
localStorage.removeItem(key);
}
} }

Loading…
Cancel
Save