1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import * as _ from 'lodash';
6 import { Observable } from 'rxjs';
7 import { tap } from 'rxjs/operators';
9 import { Credentials } from '../models/credentials';
10 import { LoginResponse } from '../models/login-response';
11 import { AuthStorageService } from '../services/auth-storage.service';
16 export class AuthService {
18 private authStorageService: AuthStorageService,
19 private http: HttpClient,
20 private router: Router,
21 private route: ActivatedRoute
24 check(token: string) {
25 return this.http.post('api/auth/check', { token: token });
28 login(credentials: Credentials): Observable<LoginResponse> {
29 return this.http.post('api/auth', credentials).pipe(
30 tap((resp: LoginResponse) => {
31 this.authStorageService.set(
35 resp.pwdExpirationDate,
36 resp.pwdUpdateRequired
42 logout(callback: Function = null) {
43 return this.http.post('api/auth/logout', null).subscribe((resp: any) => {
44 this.authStorageService.remove();
45 const url = _.get(this.route.snapshot.queryParams, 'returnUrl', '/login');
46 this.router.navigate([url], { skipLocationChange: true });
50 window.location.replace(resp.redirect_url);