From 52d75de86459bb61a3233aa271232debfaf94b83 Mon Sep 17 00:00:00 2001 From: Itay Mizeretz Date: Thu, 22 Feb 2018 15:22:35 +0200 Subject: [PATCH] Fix AuthService --- .../cc/ui/src/services/AuthService.js | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/monkey_island/cc/ui/src/services/AuthService.js b/monkey_island/cc/ui/src/services/AuthService.js index 36e352ed4..25e214088 100644 --- a/monkey_island/cc/ui/src/services/AuthService.js +++ b/monkey_island/cc/ui/src/services/AuthService.js @@ -7,7 +7,7 @@ export default class AuthService { if (this.AUTH_ENABLED) { return this._login(username, password); } else { - return {}; + return {result: true}; } }; @@ -28,7 +28,14 @@ export default class AuthService { }) }).then(response => response.json()) .then(res => { - this._setToken(res['access_token']); + if (res.hasOwnProperty('access_token')) { + this._setToken(res['access_token']); + return {result: true}; + } else { + this._removeToken(); + return {result: false}; + } + }) }; @@ -45,21 +52,26 @@ export default class AuthService { return fetch(url, { headers, ...options + }).then(res => { + if (res.status === 401) { + this._removeToken(); + } + return res; }); - }; loggedIn() { if (!this.AUTH_ENABLED) { return true; } + const token = this._getToken(); - return (token && !this._isTokenExpired(token)); + return ((token !== null) && !this._isTokenExpired(token)); } logout() { if (this.AUTH_ENABLED) { - localStorage.removeItem('jwt'); + this._removeToken(); } } @@ -76,6 +88,10 @@ export default class AuthService { localStorage.setItem('jwt', idToken); } + _removeToken() { + localStorage.removeItem('jwt'); + } + _getToken() { return localStorage.getItem('jwt') }