|
|
|
@@ -5,14 +5,34 @@ import {PeopleModel} from '../models/people.model'; |
|
|
|
import {HttpClient} from '@angular/common/http'; |
|
|
|
import {Router} from '@angular/router'; |
|
|
|
import {NotificationService} from '@progress/kendo-angular-notification'; |
|
|
|
import {debounce} from 'ts-debounce'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
export class SessionService { |
|
|
|
public loggedIn = ApiV1LoginResponse.EmptyNew(); |
|
|
|
loginSuccess = new EventEmitter <ApiV1LoginResponse>(); |
|
|
|
|
|
|
|
debouncedLocalStorageMonitor = debounce( this.localStorageChange, 1000); // to avoid to frequent local storage changes |
|
|
|
|
|
|
|
constructor(private config: AppConfig, private http: HttpClient, private router: Router, private ns: NotificationService){ } |
|
|
|
constructor(private config: AppConfig, private http: HttpClient, private router: Router, private ns: NotificationService){ |
|
|
|
const self = this; |
|
|
|
window.addEventListener('storage', event => { |
|
|
|
if (event.storageArea === localStorage && event.key === this.config.storageKey) { |
|
|
|
// It's local storage |
|
|
|
self.debouncedLocalStorageMonitor(event).then( r => {}); |
|
|
|
} |
|
|
|
}, false); |
|
|
|
} |
|
|
|
|
|
|
|
private localStorageChange(event: StorageEvent): void { |
|
|
|
console.log(event); |
|
|
|
const sfm: ApiV1LoginResponse = JSON.parse(localStorage.getItem(this.config.storageKey)); |
|
|
|
if ( sfm && sfm.session && sfm.User && sfm.User.Id && this.loggedIn.User.Id) { |
|
|
|
if ( sfm.session === this.loggedIn.session && sfm.User.Id !== this.loggedIn.User.Id){ |
|
|
|
this.logoutWithoutPersistingStorage(); // silently logout without touching any storage |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public saveSessionInfo(): void { |
|
|
|
localStorage.setItem(this.config.storageKey, JSON.stringify(this.loggedIn)); |
|
|
|
@@ -32,7 +52,7 @@ export class SessionService { |
|
|
|
} |
|
|
|
|
|
|
|
public isCurrentUser(id: string): boolean { |
|
|
|
return this.loggedIn.login === true && this.loggedIn.User !== undefined && this.loggedIn.User.Id === id; |
|
|
|
return id !== '' && this.loggedIn.login === true && this.loggedIn.User !== undefined && this.loggedIn.User.Id === id; |
|
|
|
} |
|
|
|
|
|
|
|
public UpdatePeopleInfo(people: PeopleModel): void{ |
|
|
|
@@ -47,24 +67,45 @@ export class SessionService { |
|
|
|
return; |
|
|
|
} |
|
|
|
this.loggedIn = ApiV1LoginResponse.EmptyNew(); |
|
|
|
localStorage.removeItem(this.config.storageKey); |
|
|
|
this.loginSuccess.emit(this.loggedIn); |
|
|
|
this.router.navigate(['/login']).then(r => { |
|
|
|
this.show(); |
|
|
|
} ); |
|
|
|
this.router.navigate(['/login']); |
|
|
|
localStorage.removeItem(this.config.storageKey); |
|
|
|
this.http.post(`${this.config.apiUrl}logout`, '').subscribe( |
|
|
|
resp => { |
|
|
|
console.log('successfully logout from server'); |
|
|
|
this.show(); |
|
|
|
}, |
|
|
|
event => { |
|
|
|
console.log('error logout from server', event); |
|
|
|
this.show('logout from server (with warnings)'); |
|
|
|
} |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
public show(): void { |
|
|
|
logoutWithoutPersistingStorage(): void { |
|
|
|
if ( !this.loggedIn.login ){ |
|
|
|
return; |
|
|
|
} |
|
|
|
this.loggedIn = ApiV1LoginResponse.EmptyNew(); |
|
|
|
this.loginSuccess.emit(this.loggedIn); |
|
|
|
this.router.navigate(['/login']).then(r => { |
|
|
|
this.show(); |
|
|
|
} ); |
|
|
|
} |
|
|
|
|
|
|
|
logoutAndClearLocalStorage(): void { |
|
|
|
if ( !this.loggedIn.login ){ |
|
|
|
return; |
|
|
|
} |
|
|
|
localStorage.removeItem(this.config.storageKey); |
|
|
|
this.loggedIn = ApiV1LoginResponse.EmptyNew(); |
|
|
|
this.loginSuccess.emit(this.loggedIn); |
|
|
|
this.router.navigate(['/login']).then(r => { |
|
|
|
this.show(); |
|
|
|
} ); |
|
|
|
} |
|
|
|
|
|
|
|
public show( msg: string = 'Successfully logged out'): void { |
|
|
|
this.ns.show({ |
|
|
|
content: 'Successfully logged out', |
|
|
|
content: msg, |
|
|
|
cssClass: 'button-notification', |
|
|
|
animation: { type: 'slide', duration: 400 }, |
|
|
|
position: { horizontal: 'center', vertical: 'top' }, |