]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClient } from '@angular/common/http'; |
2 | import { Injectable } from '@angular/core'; | |
b3b6e05e | 3 | import { ActivatedRoute, Router } from '@angular/router'; |
11fdf7f2 | 4 | |
b3b6e05e | 5 | import * as _ from 'lodash'; |
9f95a23c TL |
6 | import { Observable } from 'rxjs'; |
7 | import { tap } from 'rxjs/operators'; | |
8 | ||
11fdf7f2 TL |
9 | import { Credentials } from '../models/credentials'; |
10 | import { LoginResponse } from '../models/login-response'; | |
11 | import { AuthStorageService } from '../services/auth-storage.service'; | |
11fdf7f2 TL |
12 | |
13 | @Injectable({ | |
f67539c2 | 14 | providedIn: 'root' |
11fdf7f2 TL |
15 | }) |
16 | export class AuthService { | |
17 | constructor( | |
18 | private authStorageService: AuthStorageService, | |
19 | private http: HttpClient, | |
b3b6e05e TL |
20 | private router: Router, |
21 | private route: ActivatedRoute | |
11fdf7f2 TL |
22 | ) {} |
23 | ||
24 | check(token: string) { | |
25 | return this.http.post('api/auth/check', { token: token }); | |
26 | } | |
27 | ||
9f95a23c TL |
28 | login(credentials: Credentials): Observable<LoginResponse> { |
29 | return this.http.post('api/auth', credentials).pipe( | |
30 | tap((resp: LoginResponse) => { | |
31 | this.authStorageService.set( | |
32 | resp.username, | |
9f95a23c TL |
33 | resp.permissions, |
34 | resp.sso, | |
35 | resp.pwdExpirationDate, | |
36 | resp.pwdUpdateRequired | |
37 | ); | |
38 | }) | |
39 | ); | |
11fdf7f2 TL |
40 | } |
41 | ||
42 | logout(callback: Function = null) { | |
43 | return this.http.post('api/auth/logout', null).subscribe((resp: any) => { | |
11fdf7f2 | 44 | this.authStorageService.remove(); |
b3b6e05e TL |
45 | const url = _.get(this.route.snapshot.queryParams, 'returnUrl', '/login'); |
46 | this.router.navigate([url], { skipLocationChange: true }); | |
11fdf7f2 TL |
47 | if (callback) { |
48 | callback(); | |
49 | } | |
50 | window.location.replace(resp.redirect_url); | |
51 | }); | |
52 | } | |
53 | } |