From ee18d0843cb472ceff14b5522a44a9b4cc3ac9c5 Mon Sep 17 00:00:00 2001 From: annampak Date: Fri, 27 Oct 2017 11:40:44 +0300 Subject: [PATCH] logout ut through interceptor --- dmp-frontend/src/app/dmps/dmp.component.ts | 28 +++++++++---------- .../src/app/projects/projects.component.ts | 28 +++++++++---------- dmp-frontend/src/app/services/interceptor.ts | 10 +++++-- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/dmp-frontend/src/app/dmps/dmp.component.ts b/dmp-frontend/src/app/dmps/dmp.component.ts index 75b466e34..25ac6b6a2 100644 --- a/dmp-frontend/src/app/dmps/dmp.component.ts +++ b/dmp-frontend/src/app/dmps/dmp.component.ts @@ -69,20 +69,20 @@ export class DmpComponent implements OnInit{ this.dmpResource.query(params).then(dmps => this.dmps = dmps); } ); - }, - (err: HttpErrorResponse) => { - if (err.error instanceof Error) { - // A client-side or network error occurred. Handle it accordingly. - console.log('An error occurred:', err.error.message); - } else { - // The backend returned an unsuccessful response code. - // The response body may contain clues as to what went wrong, - if(err.status == 401){ - this.isignOutBtn.nativeElement.click(); - } - console.log(`Backend returned code ${err.status}, body was: ${err.error}`); - } - } + } + // (err: HttpErrorResponse) => { + // if (err.error instanceof Error) { + // // A client-side or network error occurred. Handle it accordingly. + // console.log('An error occurred:', err.error.message); + // } else { + // // The backend returned an unsuccessful response code. + // // The response body may contain clues as to what went wrong, + // if(err.status == 401){ + // this.isignOutBtn.nativeElement.click(); + // } + // console.log(`Backend returned code ${err.status}, body was: ${err.error}`); + // } + // } ); this.serverService.getAllProjects().subscribe( diff --git a/dmp-frontend/src/app/projects/projects.component.ts b/dmp-frontend/src/app/projects/projects.component.ts index a412d324c..fa6535e36 100644 --- a/dmp-frontend/src/app/projects/projects.component.ts +++ b/dmp-frontend/src/app/projects/projects.component.ts @@ -83,20 +83,20 @@ export class ProjectsComponent implements OnInit{ this.projectResource.query(params).then(projects => this.projects = projects); } ); - }, - (err: HttpErrorResponse) => { - if (err.error instanceof Error) { - // A client-side or network error occurred. Handle it accordingly. - console.log('An error occurred:', err.error.message); - } else { - // The backend returned an unsuccessful response code. - // The response body may contain clues as to what went wrong, - if(err.status == 401){ - this.isignOutBtn.nativeElement.click(); - } - console.log(`Backend returned code ${err.status}, body was: ${err.error}`); - } - } + } + // (err: HttpErrorResponse) => { + // if (err.error instanceof Error) { + // // A client-side or network error occurred. Handle it accordingly. + // console.log('An error occurred:', err.error.message); + // } else { + // // The backend returned an unsuccessful response code. + // // The response body may contain clues as to what went wrong, + // if(err.status == 401){ + // this.isignOutBtn.nativeElement.click(); + // } + // console.log(`Backend returned code ${err.status}, body was: ${err.error}`); + // } + // } ); } diff --git a/dmp-frontend/src/app/services/interceptor.ts b/dmp-frontend/src/app/services/interceptor.ts index 4449ccd78..4c5f5d269 100644 --- a/dmp-frontend/src/app/services/interceptor.ts +++ b/dmp-frontend/src/app/services/interceptor.ts @@ -1,12 +1,13 @@ import {Injectable} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse} from '@angular/common/http'; +import { TokenService, TokenProvider } from './token.service'; @Injectable() export class GlobalInterceptor implements HttpInterceptor { - constructor() { + constructor(private tokenService: TokenService) { } @@ -21,7 +22,12 @@ export class GlobalInterceptor implements HttpInterceptor { }, (err: any) => { if (err instanceof HttpErrorResponse) { if (err.status === 401) { - EDW VALE TO KWDIKA GIA TO LOGOUT KAI SXOLIASE TON OPOU ALLOU TON EXEIS + var auth2 = gapi.auth2.getAuthInstance(); +     auth2.signOut().then(function () { +       console.log('User signed out.'); + localStorage.removeItem('currentUser'); +     }); + this.tokenService.setToken(null); } } });