-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): catch fin de session (#2485)
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Auth } from '@andes/auth'; | ||
import { Plex } from '@andes/plex'; | ||
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Router } from '@angular/router'; | ||
import { Observable, of, throwError } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
|
||
|
||
@Injectable() | ||
export class TokenExpiredInterceptor implements HttpInterceptor { | ||
constructor( | ||
private plex: Plex, | ||
private router: Router, | ||
private auth: Auth | ||
) { } | ||
|
||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
return next.handle(req).pipe( | ||
catchError((err) => { | ||
if (err.status === 401 && err.error === 'Unauthorized') { | ||
// Falta un feature de plex para mejorar la UI | ||
this.plex.info('danger','', 'La sesión a finalizado!').then((a) => { | ||
this.auth.logout(); | ||
this.router.navigate(['login']).then(() => { | ||
window.location.reload(); | ||
}); | ||
}); | ||
return of(null); | ||
} | ||
return throwError(err); | ||
}) | ||
); | ||
} | ||
} |