|
1 | | -import {Injectable} from '@angular/core'; |
2 | | -import 'rxjs/add/operator/map'; |
| 1 | +import {HttpClient, HttpErrorResponse} from '@angular/common/http'; |
| 2 | +import {Inject, Injectable} from '@angular/core'; |
| 3 | +import {Observable, of} from 'rxjs'; |
| 4 | +import {catchError, map} from 'rxjs/operators'; |
3 | 5 | import {TokenService} from './token.service'; |
4 | | -import {HttpClient} from '@angular/common/http'; |
5 | 6 | import {environment} from '../environments/environment'; |
6 | | -import {User} from '../_models'; |
| 7 | +import {User} from '../_models/User'; |
| 8 | +import {AlertService} from './alert.service'; |
| 9 | +import {ActivatedRouteSnapshot} from '@angular/router'; |
| 10 | +import {Union, UnionMap, UNIONS_TOKEN} from '../_constants/unions'; |
| 11 | +import {DOCUMENT} from '@angular/common'; |
7 | 12 |
|
8 | | -@Injectable() |
| 13 | +@Injectable({providedIn: 'root'}) |
9 | 14 | export class AuthenticationService { |
10 | 15 | constructor(private http: HttpClient, |
11 | | - private tokenService: TokenService) { |
| 16 | + private alertService: AlertService, |
| 17 | + private tokenService: TokenService, |
| 18 | + @Inject(UNIONS_TOKEN) private unions: UnionMap, |
| 19 | + @Inject(DOCUMENT) private document: Document, |
| 20 | + ) { |
12 | 21 | } |
13 | 22 |
|
14 | | - login(user: User) { |
15 | | - return this.http.post(environment.backendUrl + 'token/new', user, {responseType: 'text'}); |
| 23 | + login(user: User): Observable<string | null> { |
| 24 | + return this.http.post<{ token: string }>(environment.backendUrl + 'token/new', user) |
| 25 | + .pipe( |
| 26 | + map(res => { |
| 27 | + if ('token' in res) { |
| 28 | + return res.token; |
| 29 | + } |
| 30 | + return null; |
| 31 | + }), |
| 32 | + catchError((error: HttpErrorResponse) => { |
| 33 | + console.log(error); |
| 34 | + if (error.status === 401) { |
| 35 | + this.alertService.error(error.error); |
| 36 | + return of(null); |
| 37 | + } |
| 38 | + }) |
| 39 | + ); |
16 | 40 | } |
17 | 41 |
|
18 | 42 | logout() { |
19 | 43 | this.tokenService.removeToken(); |
20 | 44 | } |
21 | 45 |
|
22 | | - async isAuthorized(): Promise<boolean> { |
23 | | - if (!this.tokenService.tokenExists()) { |
24 | | - return Promise.resolve(this.tokenService.tokenExists()); |
25 | | - } |
| 46 | + redirectToUnionIfNeeded(route: ActivatedRouteSnapshot): boolean { |
| 47 | + const returnUnion = route.queryParamMap.get('returnUnion'); |
26 | 48 |
|
27 | | - return this.http.post(environment.backendUrl + 'checkToken', this.tokenService.getToken()) |
28 | | - .toPromise() |
29 | | - .then(() => { |
| 49 | + if (typeof returnUnion === 'string') { |
| 50 | + if (returnUnion in this.unions) { |
| 51 | + this.redirectToUnion(this.unions[returnUnion]); |
30 | 52 | return true; |
31 | | - }).catch((err) => { |
32 | | - if (err.status !== 401) { |
33 | | - console.error(err); |
34 | | - } |
35 | | - this.tokenService.removeToken(); |
36 | | - return false; |
37 | | - }); |
| 53 | + } else { |
| 54 | + this.alertService.error(`Unknown union: ${returnUnion}`); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + redirectToUnion({url}: Union): void { |
| 62 | + const token = this.tokenService.getToken(); |
| 63 | + const tokenizedUrl = `${url}#token=${token}`; |
| 64 | + |
| 65 | + this.document.location.href = tokenizedUrl; |
38 | 66 | } |
39 | 67 | } |
0 commit comments