logout ut through interceptor

This commit is contained in:
annampak 2017-10-27 11:40:44 +03:00
parent dbab592360
commit ee18d0843c
3 changed files with 36 additions and 30 deletions

View File

@ -69,20 +69,20 @@ export class DmpComponent implements OnInit{
this.dmpResource.query(params).then(dmps => this.dmps = dmps); this.dmpResource.query(params).then(dmps => this.dmps = dmps);
} }
); );
}, }
(err: HttpErrorResponse) => { // (err: HttpErrorResponse) => {
if (err.error instanceof Error) { // if (err.error instanceof Error) {
// A client-side or network error occurred. Handle it accordingly. // // A client-side or network error occurred. Handle it accordingly.
console.log('An error occurred:', err.error.message); // console.log('An error occurred:', err.error.message);
} else { // } else {
// The backend returned an unsuccessful response code. // // The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong, // // The response body may contain clues as to what went wrong,
if(err.status == 401){ // if(err.status == 401){
this.isignOutBtn.nativeElement.click(); // this.isignOutBtn.nativeElement.click();
} // }
console.log(`Backend returned code ${err.status}, body was: ${err.error}`); // console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
} // }
} // }
); );
this.serverService.getAllProjects().subscribe( this.serverService.getAllProjects().subscribe(

View File

@ -83,20 +83,20 @@ export class ProjectsComponent implements OnInit{
this.projectResource.query(params).then(projects => this.projects = projects); this.projectResource.query(params).then(projects => this.projects = projects);
} }
); );
}, }
(err: HttpErrorResponse) => { // (err: HttpErrorResponse) => {
if (err.error instanceof Error) { // if (err.error instanceof Error) {
// A client-side or network error occurred. Handle it accordingly. // // A client-side or network error occurred. Handle it accordingly.
console.log('An error occurred:', err.error.message); // console.log('An error occurred:', err.error.message);
} else { // } else {
// The backend returned an unsuccessful response code. // // The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong, // // The response body may contain clues as to what went wrong,
if(err.status == 401){ // if(err.status == 401){
this.isignOutBtn.nativeElement.click(); // this.isignOutBtn.nativeElement.click();
} // }
console.log(`Backend returned code ${err.status}, body was: ${err.error}`); // console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
} // }
} // }
); );
} }

View File

@ -1,12 +1,13 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse} from '@angular/common/http'; import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse} from '@angular/common/http';
import { TokenService, TokenProvider } from './token.service';
@Injectable() @Injectable()
export class GlobalInterceptor implements HttpInterceptor { export class GlobalInterceptor implements HttpInterceptor {
constructor() { constructor(private tokenService: TokenService) {
} }
@ -21,7 +22,12 @@ export class GlobalInterceptor implements HttpInterceptor {
}, (err: any) => { }, (err: any) => {
if (err instanceof HttpErrorResponse) { if (err instanceof HttpErrorResponse) {
if (err.status === 401) { 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);
} }
} }
}); });