11import { Injectable } from '@angular/core' ;
2- import { Observable , Subscription , interval } from 'rxjs ' ;
2+ import { Router } from '@angular/router ' ;
33import * as moment from 'moment' ;
4- import { LocalStorageService } from "./local-storage.service" ;
5- import { LoginInfo } from '../models/interfaces' ;
4+ import { Observable , Subscription , interval } from 'rxjs' ;
65import { LoginServiceService } from 'src/app/services/login-service.service' ;
7- import { environment } from "src/environments/environment" ;
8- import { Router } from '@angular/router' ;
6+ import { LoginInfo } from '../models/interfaces' ;
7+ import { EventMessageService } from './event-message.service' ;
8+ import { LocalStorageService } from "./local-storage.service" ;
9+ import { NotificationService } from './notification.service' ;
910
1011@Injectable ( {
1112 providedIn : 'root'
@@ -17,12 +18,14 @@ export class RefreshLoginServiceService {
1718 constructor (
1819 private localStorage : LocalStorageService ,
1920 private api : LoginServiceService ,
20- private router : Router
21- ) {
21+ private router : Router ,
22+ private eventMessage : EventMessageService ,
23+ private notificationService : NotificationService
24+ ) {
2225 //this.intervalObservable = interval(1000); // Default interval duration set to 1000 milliseconds (1 second)
2326 }
2427
25- startInterval ( intervalDuration : number , data :any ) : void {
28+ startInterval ( intervalDuration : number , data : any ) : void {
2629 this . intervalObservable = interval ( intervalDuration ) ;
2730
2831 this . intervalSubscription = this . intervalObservable . subscribe ( ( ) => {
@@ -45,18 +48,13 @@ export class RefreshLoginServiceService {
4548 // Start the interval only if the token has been really refreshed
4649 // Otherwise close the session
4750 if ( refreshed . expire > moment ( ) . unix ( ) + 4 ) {
48- this . startInterval ( ( ( refreshed . expire - moment ( ) . unix ( ) ) - 4 ) * 1000 , refreshed )
51+ this . startInterval ( ( ( refreshed . expire - moment ( ) . unix ( ) ) - 4 ) * 1000 , refreshed )
4952 } else {
50- this . stopInterval ( ) ;
51- this . localStorage . setObject ( 'login_items' , { } ) ;
52- this . api . logout ( ) . catch ( ( err ) => {
53- } )
54-
55- this . router . navigate ( [ '/dashboard' ] ) . then ( ( ) => {
56- window . location . reload ( )
57- } ) . catch ( ( err ) => {
58- } )
53+ this . logout ( ) ;
5954 }
55+ } ) . catch ( error => {
56+ console . error ( 'Error refreshing token' , error ) ;
57+ this . logout ( ) ;
6058 } )
6159 } ) ;
6260 }
@@ -66,4 +64,22 @@ export class RefreshLoginServiceService {
6664 this . intervalSubscription . unsubscribe ( ) ;
6765 }
6866 }
69- }
67+
68+ private logout ( ) : void {
69+ this . stopInterval ( ) ;
70+ // Only logout if user is logged
71+ const aux = this . localStorage . getObject ( 'login_items' ) as any ;
72+ if ( aux && Object . keys ( aux ) . length > 0 ) {
73+ this . localStorage . setObject ( 'login_items' , { } ) ;
74+ this . eventMessage . emitLogin ( { } as LoginInfo ) ;
75+ this . api . logout ( )
76+ . catch ( ( err ) => {
77+ console . error ( 'Error during logout:' , err ) ;
78+ } )
79+
80+ this . router . navigate ( [ '/dashboard' ] ) . then ( ( ) => {
81+ this . notificationService . showInfo ( 'Your session has expired. Please log in again.' ) ;
82+ } )
83+ }
84+ }
85+ }
0 commit comments