Skip to content

Commit

Permalink
feat(auth): catch fin de session (#2485)
Browse files Browse the repository at this point in the history
  • Loading branch information
liquid36 authored Oct 6, 2021
1 parent 5e6ba61 commit 0e5d14a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Plex, PlexModule } from '@andes/plex';
import { AuthContext, Server, ServerErrorHandler, SharedModule } from '@andes/shared';
/** moment pipes - desde agular 5 hay que importar el locale a demanda */
import { registerLocaleData } from '@angular/common';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import localeEs from '@angular/common/locales/es';
import { ErrorHandler, LOCALE_ID, NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
Expand Down Expand Up @@ -193,6 +193,7 @@ import { SugerenciasService } from './services/sendmailsugerencias.service';
import { SIISAService } from './services/siisa.service';
import { ConfiguracionPrestacionService } from './services/term/configuracionPrestacion.service';
import { TipoEstablecimientoService } from './services/tipoEstablecimiento.service';
import { TokenExpiredInterceptor } from './services/token-expired.interceptor';
import { ReglaService } from './services/top/reglas.service';
import { AgendaService } from './services/turnos/agenda.service';
import { ConfigPrestacionService } from './services/turnos/configPrestacion.service';
Expand Down Expand Up @@ -409,7 +410,14 @@ registerLocaleData(localeEs, 'es');
useValue: {
siteKey: environment.SITE_KEY,
} as RecaptchaSettings,
},
{
provide: HTTP_INTERCEPTORS,
useClass: TokenExpiredInterceptor,
multi: true,
}


]
})

Expand Down
35 changes: 35 additions & 0 deletions src/app/services/token-expired.interceptor.ts
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);
})
);
}
}

0 comments on commit 0e5d14a

Please sign in to comment.