Create an auth interceptor in angular

Create an auth interceptor to inject jwt header authorization token in all requests

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import { Injectable } from "@angular/core";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
    constructor() { }

    intercept(req: HttpRequest, next: HttpHandler): Observable> {
        //get token
        let token = '';
        if (localStorage.getItem('token')) {
            token = localStorage.getItem('token');
        }

        let req: any = {
            headers: req.headers
                .set('Authorization', token)
        };
        let copiedReq = req.clone({ headers: req.headers });
        return next.handle(copiedReq);
    }
}
import { HTTP_INTERCEPTORS } from "@angular/common/http";
//...
@NgModule({
    //...
    providers: [
    //...
        { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
    //...
    ]
    //...
})
export class AppModule {}