Created
September 26, 2019 19:57
-
-
Save J-Swift/5d6fa31205595207c509387853d1050c to your computer and use it in GitHub Desktop.
ngUpgrade compatible Angular2 shim for ng-token-auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface INgTokenAuthService<TModelType> { | |
validateUser(): ng.IPromise<TModelType>; | |
submitLogin(form: {}): ng.IPromise<TModelType>; | |
signOut(): ng.IPromise<void>; | |
apiUrl(config?: string): string; | |
retrieveData(key: string): {}; | |
} | |
export interface INgTokenAuthServiceProvider extends ng.IServiceProvider { | |
configure(settings: {}): {[key: string]: {}}; | |
} | |
declare const _default = 'ng-token-auth'; | |
export default _default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Inject } from '@angular/core'; | |
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
import { INgTokenAuthService } from './ng-token-auth'; | |
@Injectable() | |
export class TokenAuthInterceptor implements HttpInterceptor { | |
constructor( | |
// tslint:disable-next-line:no-any | |
@Inject('$auth') private readonly $auth: INgTokenAuthService<any>, | |
) { } | |
// tslint:disable-next-line:no-any | |
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
if (request.url.match(this.$auth.apiUrl())) { | |
const ref = this.$auth.retrieveData('auth_headers'); | |
request = request.clone({ | |
setHeaders: ref, | |
}); | |
} | |
return next.handle(request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment