Skip to content

Instantly share code, notes, and snippets.

@splincode
Last active May 17, 2020 07:39
Show Gist options
  • Save splincode/ccdae12f1b9fc85b1f5e2db3aa7ce80c to your computer and use it in GitHub Desktop.
Save splincode/ccdae12f1b9fc85b1f5e2db3aa7ce80c to your computer and use it in GitHub Desktop.
@Injectable()
export class TokenInterceptor {
constructor(private store: Store) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token: string | null = this.store.selectSnapshot(AuthState.token);
if (token) {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`,
},
});
}
return next.handle(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment