import {Injectable, OnInit} from '@angular/core';
import {HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';

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

    token: string;

    ngOnInit() {
    }

    intercept(req: HttpRequest<any>, next: HttpHandler) {
        // 这里的拦截了所有的请求,并且加上了token
        // 每次都获取最新的token
        this.token = window.localStorage.getItem('token') || '';
        const authReq = req.clone({headers: req.headers.set('AccountToken', this.token)});
        return next.handle(authReq);

    }
}
