Skip to content

Commit 0da8887

Browse files
committed
fix issue with authentication redirect loop
1 parent 3157b7d commit 0da8887

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

interface/src/App.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react';
2+
import { Redirect, Route, Switch } from 'react-router';
23

34
import AppRouting from './AppRouting';
45
import SnackbarNotification from './components/SnackbarNotification';
@@ -33,19 +34,25 @@ const theme = createMuiTheme({
3334
// JSS instance
3435
const jss = create(jssPreset());
3536

36-
class App extends Component {
37-
render() {
38-
return (
39-
<StylesProvider jss={jss}>
40-
<MuiThemeProvider theme={theme}>
41-
<SnackbarNotification>
42-
<CssBaseline />
43-
<AppRouting />
44-
</SnackbarNotification>
45-
</MuiThemeProvider>
46-
</StylesProvider>
47-
)
48-
}
37+
// this redirect forces a call to authenticationContext.refresh() which invalidates the JWT if it is invalid.
38+
const unauthorizedRedirect = () => <Redirect to="/" />;
39+
40+
class App extends Component {
41+
render() {
42+
return (
43+
<StylesProvider jss={jss}>
44+
<MuiThemeProvider theme={theme}>
45+
<SnackbarNotification>
46+
<CssBaseline />
47+
<Switch>
48+
<Route exact path="/unauthorized" component={unauthorizedRedirect} />
49+
<Route component={AppRouting} />
50+
</Switch>
51+
</SnackbarNotification>
52+
</MuiThemeProvider>
53+
</StylesProvider>
54+
);
55+
}
4956
}
5057

5158
export default App

interface/src/authentication/Authentication.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function redirectingAuthorizedFetch(url, params) {
4747
return new Promise(function (resolve, reject) {
4848
authorizedFetch(url, params).then(response => {
4949
if (response.status === 401) {
50-
history.push("/");
50+
history.push("/unauthorized");
5151
} else {
5252
resolve(response);
5353
}

interface/src/authentication/AuthenticationWrapper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ class AuthenticationWrapper extends React.Component {
8989

9090
signIn = (accessToken) => {
9191
try {
92-
this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } });
9392
localStorage.setItem(ACCESS_TOKEN, accessToken);
93+
this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } });
9494
} catch (err) {
9595
this.setState({ initialized: true, context: { ...this.state.context, user: undefined } });
96-
this.props.raiseNotification("Failed to parse JWT " + err.message);
96+
throw new Error("Failed to parse JWT " + err.message);
9797
}
9898
}
9999

interface/src/containers/SignInPage.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ const styles = theme => {
4444
}
4545
}
4646
}
47-
4847

49-
class LoginPage extends Component {
48+
49+
class SignInPage extends Component {
5050

5151
constructor(props) {
5252
super(props);
@@ -82,7 +82,6 @@ class LoginPage extends Component {
8282
}
8383
}).then(json => {
8484
authenticationContext.signIn(json.access_token);
85-
this.setState({ processing: false });
8685
})
8786
.catch(error => {
8887
this.props.raiseNotification(error.message);
@@ -132,4 +131,6 @@ class LoginPage extends Component {
132131

133132
}
134133

135-
export default withAuthenticationContext(withNotifier(withStyles(styles)(LoginPage)));
134+
export default withAuthenticationContext(
135+
withNotifier(withStyles(styles)(SignInPage))
136+
);

0 commit comments

Comments
 (0)