import { TokenApiModel } from '../model/token.api.model';
import { Router } from '@angular/router';
import { NgToastService } from 'ng-angular-popup';
import { AuthService } from '../services/authService';
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpInterceptor,
HttpErrorResponse,
HttpEvent
} from '@angular/common/http';
import { catchError, Observable, switchMap, throwError } from 'rxjs';
import { token } from 'aws-sdk/clients/sns';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private auth: AuthService, private toast: NgToastService, private router: Router) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const myToken = this.auth.getToken()
console.log("token is ok...")
// this.start.load();
if(myToken){
request = request.clone({
setHeaders: {Authorization: "Bearer "+ myToken}
})
}
return next.handle(request).pipe(
catchError((err)=>{
if(err instanceof HttpErrorResponse){
if(err.status === 401){
//this.toast.warning({detail:"Warning", summary:"Token is expired, Please Login again"});
//this.router.navigate(['login'])
//handle
return this.handleUnAuthorizedError(request,next);
}
}
return throwError(()=> err)
})
);
}