Skip to content

Instantly share code, notes, and snippets.

@teomanofficial
Last active July 29, 2021 10:08
Show Gist options
  • Save teomanofficial/ddc3e040d232b3bef18b30524899f8fc to your computer and use it in GitHub Desktop.
Save teomanofficial/ddc3e040d232b3bef18b30524899f8fc to your computer and use it in GitHub Desktop.
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
export function appendToken(req: HttpRequest<any>, token: string) {
return req.clone({
headers: req.headers.set('Authorization', `Bearer ${token}`)
});
}
@Injectable()
export class AppendJwtToken implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = localStorage.getItem('access_token');
const updatedReq = token ? appendToken(req, token) : req;
return next.handle(updatedReq);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment