]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClient } from '@angular/common/http'; |
2 | import { Injectable } from '@angular/core'; | |
3 | import { Router } from '@angular/router'; | |
4 | ||
9f95a23c TL |
5 | import { Observable } from 'rxjs'; |
6 | import { tap } from 'rxjs/operators'; | |
7 | ||
11fdf7f2 TL |
8 | import { Credentials } from '../models/credentials'; |
9 | import { LoginResponse } from '../models/login-response'; | |
10 | import { AuthStorageService } from '../services/auth-storage.service'; | |
11 | import { ApiModule } from './api.module'; | |
12 | ||
13 | @Injectable({ | |
14 | providedIn: ApiModule | |
15 | }) | |
16 | export class AuthService { | |
17 | constructor( | |
18 | private authStorageService: AuthStorageService, | |
19 | private http: HttpClient, | |
20 | private router: Router | |
21 | ) {} | |
22 | ||
23 | check(token: string) { | |
24 | return this.http.post('api/auth/check', { token: token }); | |
25 | } | |
26 | ||
9f95a23c TL |
27 | login(credentials: Credentials): Observable<LoginResponse> { |
28 | return this.http.post('api/auth', credentials).pipe( | |
29 | tap((resp: LoginResponse) => { | |
30 | this.authStorageService.set( | |
31 | resp.username, | |
32 | resp.token, | |
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) => { | |
44 | this.router.navigate(['/logout'], { skipLocationChange: true }); | |
45 | this.authStorageService.remove(); | |
46 | if (callback) { | |
47 | callback(); | |
48 | } | |
49 | window.location.replace(resp.redirect_url); | |
50 | }); | |
51 | } | |
52 | } |