# All files / src/app/shared/api auth.service.ts

96.15% Statements 25/26
76.92% Branches 10/13
100% Functions 6/6
95.24% Lines 20/21

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
    449x
    9x
    9x

    9x
    9x

    9x

    19x
    19x
    19x

    9x
    3x

    9x
    1x

    1x

    9x
    2x
    1x
    1x

    1x

    9x
     import { HttpClient } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    import { Router } from '@angular/router';

    import { Credentials } from '../models/credentials';
    import { LoginResponse } from '../models/login-response';
    import { AuthStorageService } from '../services/auth-storage.service';
    import { ApiModule } from './api.module';

    @Injectable({
      providedIn: ApiModule
    })
    export class AuthService {
      constructor(
        private authStorageService: AuthStorageService,
        private http: HttpClient,
        private router: Router
      ) {}

      check(token: string) {
        return this.http.post('api/auth/check', { token: token });
      }

      login(credentials: Credentials) {
        return this.http
          .post('api/auth', credentials)
          .toPromise()
          .then((resp: LoginResponse) => {
            this.authStorageService.set(resp.username, resp.token, resp.permissions);
          });
      }

      logout(callback: Function = null) {
        return this.http.post('api/auth/logout', null).subscribe((resp: any) => {
          this.router.navigate(['/logout'], { skipLocationChange: true });
          this.authStorageService.remove();
          if (callback) {
            callback();
          }
          window.location.replace(resp.redirect_url);
        });
      }
    }