From 0da88878d5ae606f9c8dbddf5fb13b059cbd5454 Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Mon, 3 Jun 2019 21:32:54 +0100 Subject: [PATCH] fix issue with authentication redirect loop --- interface/src/App.js | 33 +++++++++++-------- .../src/authentication/Authentication.js | 2 +- .../authentication/AuthenticationWrapper.js | 4 +-- interface/src/containers/SignInPage.js | 9 ++--- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/interface/src/App.js b/interface/src/App.js index 1f5a7e2..231be0e 100644 --- a/interface/src/App.js +++ b/interface/src/App.js @@ -1,4 +1,5 @@ import React, { Component } from 'react'; +import { Redirect, Route, Switch } from 'react-router'; import AppRouting from './AppRouting'; import SnackbarNotification from './components/SnackbarNotification'; @@ -33,19 +34,25 @@ const theme = createMuiTheme({ // JSS instance const jss = create(jssPreset()); -class App extends Component { - render() { - return ( - - - - - - - - - ) - } +// this redirect forces a call to authenticationContext.refresh() which invalidates the JWT if it is invalid. +const unauthorizedRedirect = () => ; + +class App extends Component { + render() { + return ( + + + + + + + + + + + + ); + } } export default App diff --git a/interface/src/authentication/Authentication.js b/interface/src/authentication/Authentication.js index 2de39e7..eb3acdc 100644 --- a/interface/src/authentication/Authentication.js +++ b/interface/src/authentication/Authentication.js @@ -47,7 +47,7 @@ export function redirectingAuthorizedFetch(url, params) { return new Promise(function (resolve, reject) { authorizedFetch(url, params).then(response => { if (response.status === 401) { - history.push("/"); + history.push("/unauthorized"); } else { resolve(response); } diff --git a/interface/src/authentication/AuthenticationWrapper.js b/interface/src/authentication/AuthenticationWrapper.js index 3494fd2..c4429af 100644 --- a/interface/src/authentication/AuthenticationWrapper.js +++ b/interface/src/authentication/AuthenticationWrapper.js @@ -89,11 +89,11 @@ class AuthenticationWrapper extends React.Component { signIn = (accessToken) => { try { - this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } }); localStorage.setItem(ACCESS_TOKEN, accessToken); + this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } }); } catch (err) { this.setState({ initialized: true, context: { ...this.state.context, user: undefined } }); - this.props.raiseNotification("Failed to parse JWT " + err.message); + throw new Error("Failed to parse JWT " + err.message); } } diff --git a/interface/src/containers/SignInPage.js b/interface/src/containers/SignInPage.js index 518a73d..28cfa70 100644 --- a/interface/src/containers/SignInPage.js +++ b/interface/src/containers/SignInPage.js @@ -44,9 +44,9 @@ const styles = theme => { } } } - -class LoginPage extends Component { + +class SignInPage extends Component { constructor(props) { super(props); @@ -82,7 +82,6 @@ class LoginPage extends Component { } }).then(json => { authenticationContext.signIn(json.access_token); - this.setState({ processing: false }); }) .catch(error => { this.props.raiseNotification(error.message); @@ -132,4 +131,6 @@ class LoginPage extends Component { } -export default withAuthenticationContext(withNotifier(withStyles(styles)(LoginPage))); +export default withAuthenticationContext( + withNotifier(withStyles(styles)(SignInPage)) +);