benbaran / adal-angular4

Angular 4/5/6/7 ADAL Wrapper
MIT License
86 stars 104 forks source link

Issue while Acquire Token #75

Open vipins-optimus opened 6 years ago

vipins-optimus commented 6 years ago

I have Angular 6 project and using adal-angular4(which is upgraded to Angular6) for authentication. After 1 hour, the authentication token gets expired. So i want to refresh the token internally. For the same reason, i have used 'adalService.acquireToken()' method to get the refreshed token every time. The below code is working fine normaly. But after 1 hour, when i trigger any API, we get the => '401 Unauthorized User'. So, its not working as expected.

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpUserEvent<any>> {
        return this.adalService.acquireToken(clientId).flatMap((token) => {
            const authReq = req.clone({
                url: environment.apiUrl + req.url,
                headers: req.headers
                    .set('Content-Type', 'application/json')
                    .set('Authorization', 'Bearer' + token)
            })
            return next.handle(authReq);
        })
        .catch((err) =>  {
            return Observable.throw(err);
        })
        .finally(() => this.loaderService.display(false));
}