From 6e39be553e7c799abc215052a821b1df04a32a18 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 20 Feb 2017 14:53:42 -0500 Subject: [PATCH 1/2] Fix date comparison in checkToken method --- src/containers/GoogleLogin/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/containers/GoogleLogin/index.js b/src/containers/GoogleLogin/index.js index 61b69cf2..45d5beaa 100644 --- a/src/containers/GoogleLogin/index.js +++ b/src/containers/GoogleLogin/index.js @@ -18,7 +18,8 @@ class GoogleLogin extends Component { } static checkToken() { - return localStorage.gat && new Date() < localStorage.ed; + console.info('Checking expiration...', new Date(), localStorage.ed, new Date(parseInt(localStorage.ed, 10))); + return localStorage.gat && new Date() < new Date(parseInt(localStorage.ed, 10)); } static loggedIn() { @@ -123,6 +124,7 @@ class GoogleLogin extends Component { }; refreshToken = () => { + console.info('Refreshing access token'); window.gapi.auth2.getAuthInstance().currentUser.get() .reloadAuthResponse().then((authResponse) => { this.onSuccess(authResponse); -- GitLab From 17c61e98983865357f4fc0bf6e7dc8b53c31b1f0 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 20 Feb 2017 15:31:08 -0500 Subject: [PATCH 2/2] Redirect to homepage if token is refreshing and on login page --- src/containers/GoogleLogin/index.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/containers/GoogleLogin/index.js b/src/containers/GoogleLogin/index.js index 45d5beaa..72e42baf 100644 --- a/src/containers/GoogleLogin/index.js +++ b/src/containers/GoogleLogin/index.js @@ -8,17 +8,16 @@ import request from '../../utils/request'; class GoogleLogin extends Component { static onFailure() { - // TODO handle error + // TODO: handle error // console.error(err); } static onRequest() { - // TODO add spinner + // TODO: add spinner // console.log('loading...'); } static checkToken() { - console.info('Checking expiration...', new Date(), localStorage.ed, new Date(parseInt(localStorage.ed, 10))); return localStorage.gat && new Date() < new Date(parseInt(localStorage.ed, 10)); } @@ -55,7 +54,7 @@ class GoogleLogin extends Component { this.setState({ loggedIn: true }); } }).catch(() => { - // TODO handle invalid token error + // TODO: handle invalid token error // console.error(err); }); } @@ -104,7 +103,7 @@ class GoogleLogin extends Component { }; } - onSuccess = (authResponse, user, goBack) => { + onSuccess = (authResponse, redirect, user) => { localStorage.setItem('git', authResponse.id_token); localStorage.setItem('gat', authResponse.access_token); localStorage.setItem('ed', authResponse.expires_at); @@ -117,17 +116,20 @@ class GoogleLogin extends Component { } this.setState({ loggedIn: true }); - if (goBack) { - // TODO go back to previous page + if (redirect) { + // TODO: go back to previous page this.props.router.push('/'); } }; refreshToken = () => { - console.info('Refreshing access token'); window.gapi.auth2.getAuthInstance().currentUser.get() .reloadAuthResponse().then((authResponse) => { - this.onSuccess(authResponse); + let redirect = false; + if (this.props.router.getCurrentLocation().pathname === '/login') { + redirect = true; + } + this.onSuccess(authResponse, redirect); }); }; @@ -152,7 +154,7 @@ class GoogleLogin extends Component { .then((res) => { const basicProfile = res.getBasicProfile(); const authResponse = res.getAuthResponse(); - this.onSuccess(authResponse, basicProfile, true); + this.onSuccess(authResponse, true, basicProfile); }, err => GoogleLogin.onFailure(err) ); @@ -161,7 +163,7 @@ class GoogleLogin extends Component { }; signOut = () => { - // TODO prevent logout button from being pressed before gapi.auth2.init + // TODO: prevent logout button from being pressed before gapi.auth2.init // has loaded if (!this.state.disabled) { window.gapi.auth2.getAuthInstance().signOut(); -- GitLab