|
|
|
@ -9,7 +9,7 @@ import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse |
|
|
|
import { Injectable} from '@angular/core'; |
|
|
|
import { Observable } from 'rxjs'; |
|
|
|
|
|
|
|
import { AuthService } from './../services/auth.service'; |
|
|
|
import { AuthService, Profile } from './../services/auth.service'; |
|
|
|
import { ApiUrlConfig } from 'framework'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
@ -25,31 +25,37 @@ export class AuthInterceptor implements HttpInterceptor { |
|
|
|
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
|
|
|
if (req.url.indexOf(this.baseUrl) === 0 && !req.headers.has('NoAuth')) { |
|
|
|
return this.authService.userChanges.first().switchMap(user => { |
|
|
|
const token = user ? user.authToken : ''; |
|
|
|
|
|
|
|
const authReq = req.clone({ |
|
|
|
headers: req.headers |
|
|
|
.set('Authorization', token) |
|
|
|
.set('Accept-Language', '*') |
|
|
|
.set('Pragma', 'no-cache') |
|
|
|
}); |
|
|
|
|
|
|
|
return next.handle(authReq) |
|
|
|
.catch((error: HttpErrorResponse) => { |
|
|
|
if (error.status === 404 && (!user || user.isExpired)) { |
|
|
|
this.authService.logoutRedirect(); |
|
|
|
|
|
|
|
return Observable.empty<Response>(); |
|
|
|
} else if (error.status === 401 || error.status === 403) { |
|
|
|
this.authService.logoutRedirect(); |
|
|
|
|
|
|
|
return Observable.empty<Response>(); |
|
|
|
} |
|
|
|
return Observable.throw(error); |
|
|
|
}); |
|
|
|
return this.makeRequest(req, next, user, true); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
return next.handle(req); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private makeRequest(req: HttpRequest<any>, next: HttpHandler, user: Profile, renew = false): Observable<HttpEvent<any>> { |
|
|
|
const token = user ? user.authToken : ''; |
|
|
|
|
|
|
|
const authReq = req.clone({ |
|
|
|
headers: req.headers |
|
|
|
.set('Authorization', token) |
|
|
|
.set('Accept-Language', '*') |
|
|
|
.set('Pragma', 'no-cache') |
|
|
|
}); |
|
|
|
|
|
|
|
return next.handle(authReq) |
|
|
|
.catch((error: HttpErrorResponse) => { |
|
|
|
if (error.status === 401 && renew) { |
|
|
|
return this.authService.loginSilent().switchMap(u => this.makeRequest(req, next, u)); |
|
|
|
} else if (error.status === 404 && (!user || user.isExpired)) { |
|
|
|
this.authService.logoutRedirect(); |
|
|
|
|
|
|
|
return Observable.empty<Response>(); |
|
|
|
}else if (error.status === 401 || error.status === 403) { |
|
|
|
this.authService.logoutRedirect(); |
|
|
|
|
|
|
|
return Observable.empty<Response>(); |
|
|
|
} |
|
|
|
return Observable.throw(error); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |