mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-03-07 20:10:03 +03:00
34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { Router } from '@angular/router';
|
|
import { Observable, throwError } from 'rxjs';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
@Injectable()
|
|
export class H401Interceptor implements HttpInterceptor {
|
|
|
|
constructor(private router: Router, private snackBar: MatSnackBar) { }
|
|
|
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
return next.handle(request).pipe(catchError(err => {
|
|
if (err.status === 401) {
|
|
localStorage.setItem('jwt_token', null);
|
|
if (this.router.url !== '/login' && !this.router.url.includes('player')) {
|
|
this.router.navigate(['/login']).then(() => {
|
|
this.openSnackBar('Login expired, please login again.');
|
|
});
|
|
}
|
|
}
|
|
|
|
const error = err.error.message || err.statusText;
|
|
return throwError(error);
|
|
}));
|
|
}
|
|
|
|
public openSnackBar(message: string, action: string = '') {
|
|
this.snackBar.open(message, action, {
|
|
duration: 2000,
|
|
});
|
|
}
|
|
} |