# All files / src/app/shared/services auth-storage.service.ts

95.65% Statements 22/23
100% Branches 4/4
85.71% Functions 6/7
95% Lines 19/20

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
    4193x

    93x
    93x

    93x

    93x
    10x
    10x
    10x

    93x
    4x
    4x

    93x

    93x
    10x

    93x
    1x

    93x
    295x

    93x
     import { Injectable } from '@angular/core';

    import { Permissions } from '../models/permissions';
    import { ServicesModule } from './services.module';

    @Injectable({
      providedIn: ServicesModule
    })
    export class AuthStorageService {
      constructor() {}

      set(username: string, token: string, permissions: object = {}) {
        localStorage.setItem('dashboard_username', username);
        localStorage.setItem('access_token', token);
        localStorage.setItem('dashboard_permissions', JSON.stringify(new Permissions(permissions)));
      }

      remove() {
        localStorage.removeItem('access_token');
        localStorage.removeItem('dashboard_username');
      }

      getToken(): string {
        return localStorage.getItem('access_token');
      }

      isLoggedIn() {
        return localStorage.getItem('dashboard_username') !== null;
      }

      getUsername() {
        return localStorage.getItem('dashboard_username');
      }

      getPermissions(): Permissions {
        return JSON.parse(
          localStorage.getItem('dashboard_permissions') || JSON.stringify(new Permissions({}))
        );
      }
    }