All files / src/app/core/services session.service.ts

47.82% Statements 22/46
33.33% Branches 3/9
60% Functions 9/15
55.55% Lines 15/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6033x 33x 33x 33x   33x         33x   10x   10x     10x 10x 10x 8x             5x       42x       1x                                                
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { SessionStorageService } from './session-storage.service';
import { Router } from '@angular/router';
import { SessionInformation } from '@core/model/session-information.interface';
import { NotificationService } from './notification.service';
 
@Injectable({
  providedIn: 'root'
})
export class SessionService {
 
  public logged = false;
 
  private isLoggedSubject = new BehaviorSubject<boolean>(this.logged);
 
  constructor(
    private sessionStorageService: SessionStorageService, 
    private router: Router,
    private notificationService: NotificationService) {
    IIif(this.sessionStorageService.getSessionInformation() != null) {
      this.logged = true;
      this.next();
    }
  }
 
  public isLogged() :boolean {
    return this.logged;
  }
  
  public $isLogged(): Observable<boolean> {
    return this.isLoggedSubject.asObservable();
  }
 
  public $getSessionInformation(): BehaviorSubject<SessionInformation|null> {
    return this.sessionStorageService.$getSessionInformation();
  }
 
  public logIn(data: SessionInformation): void {
    this.sessionStorageService.saveSessionInformation(data);
    this.logged = true;
    this.next();
  }
 
  public logOut(redirect: boolean = true): void {
    // clear user and session related data from storage
    this.sessionStorageService.clearSessionInformation();
    this.logged = false;
    this.next();
    Iif(redirect) {
      this.router.navigate(['']);
    }
    this.notificationService.information($localize `:@@message.disconnected:You have been disconnected.`);
  }
 
  private next(): void {
    this.isLoggedSubject.next(this.logged);
  }
}