Skip to content

Instantly share code, notes, and snippets.

@pankajparkar
Last active March 29, 2020 15:03
Show Gist options
  • Save pankajparkar/3b7ae96f96f3471bf96e940297c68a0f to your computer and use it in GitHub Desktop.
Save pankajparkar/3b7ae96f96f3471bf96e940297c68a0f to your computer and use it in GitHub Desktop.
token.interceptor.ts
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth/auth.service';
import { Observable } from 'rxjs';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
// AuthService is holding
constructor(public auth: AuthService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = window.localStorage.get('token');
request = request.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
return next.handle(request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment