Files
doneit-web/src/app/interceptors/token.interceptors.ts
T

147 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-11-29 12:17:52 +01:00
import { Injectable } from "@angular/core";
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpErrorResponse,
HTTP_INTERCEPTORS,
HttpClient,
} from "@angular/common/http";
import { AuthService } from '../services/auth.service';
import { Observable, throwError, BehaviorSubject, of } from "rxjs";
import { catchError, filter, take, switchMap, tap } from "rxjs/operators";
import { SessionStore } from '../store/session.service';
import { environment } from "src/environments/environment";
2023-12-01 12:19:20 +01:00
import { Router } from "@angular/router";
2024-03-05 09:18:08 +01:00
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
2023-11-29 12:17:52 +01:00
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
private isRefreshing = false;
private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
null
);
2024-06-21 13:45:06 +01:00
private excludedDomains = [ 'Login', environment.apiChatUrl, 'http://localhost:8019']; // Add the domains you want to exclude
2024-02-01 08:25:31 +01:00
2024-03-05 09:18:08 +01:00
constructor(private http: HttpClient, private router: Router,private httpErrorHandle: HttpErrorHandle,) { }
2023-11-29 12:17:52 +01:00
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
2024-02-01 08:25:31 +01:00
if (this.shouldExcludeDomain(request)) {
return next.handle(request);
}
2023-11-29 12:17:52 +01:00
if (SessionStore.user.Authorization) {
request = this.addToken(request, SessionStore.user.Authorization);
}
return next.handle(request).pipe(
2024-06-19 15:00:01 +01:00
2023-11-29 12:17:52 +01:00
catchError((error) => {
2024-06-19 15:00:01 +01:00
console.log('interceptor ',error)
2023-11-29 12:17:52 +01:00
if (error instanceof HttpErrorResponse && error.status === 401) {
return this.handle401Error(request, next);
2024-06-19 15:00:01 +01:00
} else if (error.url.includes('https://gdapi-dev.dyndns.info/stage/api/v2') && error.status === 0){
return this.handle401Error(request, next);
2023-11-29 12:17:52 +01:00
} else {
return throwError(error);
}
})
);
}
2024-02-01 08:25:31 +01:00
private shouldExcludeDomain(request: HttpRequest<any>): boolean {
const url = request.url.toLowerCase();
return this.excludedDomains.some((domain) => url.includes(domain.toLowerCase()));
}
2024-01-18 13:08:10 +01:00
2024-02-01 08:25:31 +01:00
private addToken(request: HttpRequest<any>, token: string) {
2024-01-18 13:08:10 +01:00
2024-02-01 08:25:31 +01:00
return request.clone({
setHeaders: {
//'Access-Control-Allow-Origin': '*',
Authorization: `Bearer ${token}`,
},
});
2024-01-18 13:08:10 +01:00
2023-11-29 12:17:52 +01:00
}
private handle401Error(request: HttpRequest<any>, next: HttpHandler) {
2024-02-01 08:25:31 +01:00
// if not getting the new token yet
2023-11-29 12:17:52 +01:00
if (!this.isRefreshing) {
this.isRefreshing = true;
this.refreshTokenSubject.next(null);
return this.refreshToken().pipe(
switchMap((token: any) => {
this.isRefreshing = false;
2023-12-07 07:01:05 +01:00
this.refreshTokenSubject.next(token.Authorization);
return next.handle(this.addToken(request, token.Authorization));
2023-11-29 12:17:52 +01:00
})
);
} else {
2024-02-01 08:25:31 +01:00
// get new token
2023-11-29 12:17:52 +01:00
return this.refreshTokenSubject.pipe(
filter((token) => token != null),
take(1),
switchMap((jwt) => {
return next.handle(this.addToken(request, jwt));
})
);
}
}
2023-12-01 12:19:20 +01:00
2023-11-29 12:17:52 +01:00
//this method refresh token is declared here temporary beacouse a circular error
refreshToken() {
2023-12-01 12:19:20 +01:00
2023-11-29 12:17:52 +01:00
return this.http
.put<any>(environment.apiURL + "UserAuthentication/RefreshToken", {
refreshToken: SessionStore.user.RefreshToken,
},)
.pipe(
tap((tokens) => {
console.log(tokens)
SessionStore.user.Authorization = tokens.Authorization;
SessionStore.user.RefreshToken = tokens.refreshToken;
2023-12-07 10:51:22 +01:00
SessionStore.save();
2023-11-29 12:17:52 +01:00
}),
catchError((error) => {
2023-12-07 07:01:05 +01:00
console.log(error)
2024-02-01 10:41:41 +01:00
SessionStore.user.Authorization = SessionStore.user.Authorization;
SessionStore.user.RefreshToken = SessionStore.user.RefreshToken;
2023-12-01 12:19:20 +01:00
SessionStore.setInativity(false)
/* SessionStore.setUrlBeforeInactivity(this.router.url); */
if (environment.production) {
window.location.pathname = '/auth'
} else {
2024-03-06 15:20:35 +01:00
const pathBeforeGoOut = window.location.pathname
console.log('Before auth',window.location.pathname)
2024-03-05 09:18:08 +01:00
this.router.navigateByUrl('/auth', { replaceUrl: true }).then(() =>{
2024-03-06 15:20:35 +01:00
if(pathBeforeGoOut != "/auth") {
this.httpErrorHandle.httpsSucessMessagge('sessonExpired')
}
2024-06-19 18:55:36 +01:00
2024-03-05 09:18:08 +01:00
})
2023-12-01 12:19:20 +01:00
}
2023-11-29 12:17:52 +01:00
return of(false);
})
);
}
}
export const tokenInterceptor = {
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
2024-01-17 10:25:16 +01:00
};