Skip to content

Instantly share code, notes, and snippets.

@J-Swift
Created September 26, 2019 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save J-Swift/5d6fa31205595207c509387853d1050c to your computer and use it in GitHub Desktop.
Save J-Swift/5d6fa31205595207c509387853d1050c to your computer and use it in GitHub Desktop.
ngUpgrade compatible Angular2 shim for ng-token-auth
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;
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