11import { HttpClient , HttpErrorResponse } from '@angular/common/http' ;
22import { Inject , Injectable } from '@angular/core' ;
3- import { Observable , of } from 'rxjs' ;
3+ import { Observable , throwError } from 'rxjs' ;
44import { catchError , map } from 'rxjs/operators' ;
55import { TokenService } from './token.service' ;
66import { environment } from '../environments/environment' ;
77import { User } from '../_models/User' ;
8- import { AlertService } from './alert.service' ;
9- import { ActivatedRouteSnapshot } from '@angular/router' ;
8+ import { ParamMap } from '@angular/router' ;
109import { Union , UnionMap , UNIONS_TOKEN } from '../_constants/unions' ;
1110import { DOCUMENT } from '@angular/common' ;
1211
1312@Injectable ( { providedIn : 'root' } )
1413export class AuthenticationService {
1514 constructor ( private http : HttpClient ,
16- private alertService : AlertService ,
1715 private tokenService : TokenService ,
1816 @Inject ( UNIONS_TOKEN ) private unions : UnionMap ,
1917 @Inject ( DOCUMENT ) private document : Document ,
2018 ) {
2119 }
2220
23- login ( user : User ) : Observable < string | null > {
21+ login ( user : User ) : Observable < Login | null > {
2422 return this . http . post < { token : string } > ( environment . backendUrl + 'token/new' , user )
2523 . pipe (
2624 map ( res => {
2725 if ( 'token' in res ) {
28- return res . token ;
26+ return { token : res . token } ;
2927 }
30- return null ;
28+ throw new Error ( `Valid token is not returned. Got: ${ res } ` ) ;
3129 } ) ,
32- catchError ( ( error : HttpErrorResponse ) => {
33- console . log ( error ) ;
34- if ( error . status === 401 ) {
35- this . alertService . error ( error . error ) ;
36- return of ( null ) ;
30+ catchError ( ( error : Error | HttpErrorResponse ) => {
31+ if ( 'status' in error ) {
32+ if ( error . status === 401 ) {
33+ return throwError ( 'Invalid username or password' ) ;
34+ }
35+ return throwError ( error . message || error . error ) ;
3736 }
37+ return throwError ( error ) ;
3838 } )
3939 ) ;
4040 }
@@ -43,25 +43,32 @@ export class AuthenticationService {
4343 this . tokenService . removeToken ( ) ;
4444 }
4545
46- redirectToUnionIfNeeded ( route : ActivatedRouteSnapshot ) : boolean {
47- const returnUnion = route . queryParamMap . get ( 'returnUnion' ) ;
46+ shouldRedirect ( queryParamMap : ParamMap ) : boolean {
47+ const returnUnion = this . getReturnUnion ( queryParamMap ) ;
4848
4949 if ( typeof returnUnion === 'string' ) {
5050 if ( returnUnion in this . unions ) {
51- this . redirectToUnion ( this . unions [ returnUnion ] ) ;
5251 return true ;
5352 } else {
54- this . alertService . error ( `Unknown union: ${ returnUnion } ` ) ;
53+ throw new Error ( `Unknown union: ${ returnUnion } ` ) ;
5554 }
5655 }
5756
5857 return false ;
5958 }
6059
61- redirectToUnion ( { url} : Union ) : void {
60+ redirect ( queryParamMap : ParamMap ) : void {
61+ const returnUnion = this . getReturnUnion ( queryParamMap ) ;
62+ this . redirectToUnion ( this . unions [ returnUnion ] ) ;
63+ }
64+
65+ redirectToUnion ( { url } : Union ) : void {
6266 const token = this . tokenService . getToken ( ) ;
63- const tokenizedUrl = `${ url } #token=${ token } ` ;
6467
65- this . document . location . href = tokenizedUrl ;
68+ this . document . location . href = `${ url } #token=${ token } ` ;
69+ }
70+
71+ getReturnUnion ( queryParamMap : ParamMap ) : string | null {
72+ return queryParamMap . get ( 'returnUnion' ) ;
6673 }
6774}
0 commit comments