fix issue with authentication redirect loop
This commit is contained in:
parent
3157b7d3ef
commit
0da88878d5
@ -1,4 +1,5 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
|
import { Redirect, Route, Switch } from 'react-router';
|
||||||
|
|
||||||
import AppRouting from './AppRouting';
|
import AppRouting from './AppRouting';
|
||||||
import SnackbarNotification from './components/SnackbarNotification';
|
import SnackbarNotification from './components/SnackbarNotification';
|
||||||
@ -33,19 +34,25 @@ const theme = createMuiTheme({
|
|||||||
// JSS instance
|
// JSS instance
|
||||||
const jss = create(jssPreset());
|
const jss = create(jssPreset());
|
||||||
|
|
||||||
class App extends Component {
|
// this redirect forces a call to authenticationContext.refresh() which invalidates the JWT if it is invalid.
|
||||||
render() {
|
const unauthorizedRedirect = () => <Redirect to="/" />;
|
||||||
return (
|
|
||||||
<StylesProvider jss={jss}>
|
class App extends Component {
|
||||||
<MuiThemeProvider theme={theme}>
|
render() {
|
||||||
<SnackbarNotification>
|
return (
|
||||||
<CssBaseline />
|
<StylesProvider jss={jss}>
|
||||||
<AppRouting />
|
<MuiThemeProvider theme={theme}>
|
||||||
</SnackbarNotification>
|
<SnackbarNotification>
|
||||||
</MuiThemeProvider>
|
<CssBaseline />
|
||||||
</StylesProvider>
|
<Switch>
|
||||||
)
|
<Route exact path="/unauthorized" component={unauthorizedRedirect} />
|
||||||
}
|
<Route component={AppRouting} />
|
||||||
|
</Switch>
|
||||||
|
</SnackbarNotification>
|
||||||
|
</MuiThemeProvider>
|
||||||
|
</StylesProvider>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
@ -47,7 +47,7 @@ export function redirectingAuthorizedFetch(url, params) {
|
|||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
authorizedFetch(url, params).then(response => {
|
authorizedFetch(url, params).then(response => {
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
history.push("/");
|
history.push("/unauthorized");
|
||||||
} else {
|
} else {
|
||||||
resolve(response);
|
resolve(response);
|
||||||
}
|
}
|
||||||
|
@ -89,11 +89,11 @@ class AuthenticationWrapper extends React.Component {
|
|||||||
|
|
||||||
signIn = (accessToken) => {
|
signIn = (accessToken) => {
|
||||||
try {
|
try {
|
||||||
this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } });
|
|
||||||
localStorage.setItem(ACCESS_TOKEN, accessToken);
|
localStorage.setItem(ACCESS_TOKEN, accessToken);
|
||||||
|
this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.setState({ initialized: true, context: { ...this.state.context, user: undefined } });
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,9 +44,9 @@ const styles = theme => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class LoginPage extends Component {
|
|
||||||
|
class SignInPage extends Component {
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
@ -82,7 +82,6 @@ class LoginPage extends Component {
|
|||||||
}
|
}
|
||||||
}).then(json => {
|
}).then(json => {
|
||||||
authenticationContext.signIn(json.access_token);
|
authenticationContext.signIn(json.access_token);
|
||||||
this.setState({ processing: false });
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
this.props.raiseNotification(error.message);
|
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))
|
||||||
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user