rename jwt to user in authentication context, in prepartion for filtering display by user level

This commit is contained in:
Rick Watson 2019-05-31 20:55:06 +01:00
parent d5efbe4b18
commit 22d922c699
4 changed files with 26 additions and 18 deletions

View File

@ -13,7 +13,7 @@ export class AuthenticatedRoute extends React.Component {
const { raiseNotification, authenticationContext, component: Component, ...rest } = this.props;
const { location } = this.props;
const renderComponent = (props) => {
if (authenticationContext.jwt) {
if (authenticationContext.isAuthenticated()) {
return (
<Component {...props} />
);

View File

@ -27,14 +27,13 @@ class AuthenticationWrapper extends React.Component {
constructor(props) {
super(props);
this.refresh = this.refresh.bind(this);
this.signIn = this.signIn.bind(this);
this.signOut = this.signOut.bind(this);
this.state = {
context: {
refresh: this.refresh,
signIn: this.signIn,
signOut: this.signOut
signOut: this.signOut,
isAuthenticated: this.isAuthenticated,
isAdmin: this.isAdmin
},
initialized: false
};
@ -72,44 +71,53 @@ class AuthenticationWrapper extends React.Component {
);
}
refresh() {
refresh = () => {
var accessToken = localStorage.getItem(ACCESS_TOKEN);
if (accessToken) {
authorizedFetch(VERIFY_AUTHORIZATION_ENDPOINT)
.then(response => {
const jwt = response.status === 200 ? jwtDecode(accessToken) : undefined;
this.setState({ initialized: true, context: { ...this.state.context, jwt } });
const user = response.status === 200 ? jwtDecode(accessToken) : undefined;
this.setState({ initialized: true, context: { ...this.state.context, user } });
}).catch(error => {
this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } });
this.setState({ initialized: true, context: { ...this.state.context, user: undefined } });
this.props.raiseNotification("Error verifying authorization: " + error.message);
});
} else {
this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } });
this.setState({ initialized: true, context: { ...this.state.context, user: undefined } });
}
}
signIn(accessToken) {
signIn = (accessToken) => {
try {
this.setState({ context: { ...this.state.context, jwt: jwtDecode(accessToken) } });
this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } });
localStorage.setItem(ACCESS_TOKEN, accessToken);
} catch (err) {
this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } });
this.setState({ initialized: true, context: { ...this.state.context, user: undefined } });
this.props.raiseNotification("Failed to parse JWT " + err.message);
}
}
signOut() {
signOut = () => {
localStorage.removeItem(ACCESS_TOKEN);
this.setState({
context: {
...this.state.context,
jwt: undefined
user: undefined
}
});
this.props.raiseNotification("You have signed out.");
history.push('/');
}
isAuthenticated = () => {
return this.state.context.user;
}
isAdmin = () => {
const { context } = this.state;
return context.user && context.user.admin;
}
}
export default withStyles(styles)(withNotifier(AuthenticationWrapper))

View File

@ -8,9 +8,9 @@ import * as Authentication from './Authentication';
class UnauthenticatedRoute extends React.Component {
render() {
const { component:Component, ...rest } = this.props;
const { authenticationContext, component:Component, ...rest } = this.props;
const renderComponent = (props) => {
if (this.props.authenticationContext.jwt) {
if (authenticationContext.isAuthenticated()) {
return (<Redirect to={Authentication.fetchLoginRedirect()} />);
}
return (<Component {...props} />);

View File

@ -189,7 +189,7 @@ class MenuAppBar extends React.Component {
<AccountCircleIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary={"Signed in as: " + authenticationContext.jwt.username} secondary={ authenticationContext.jwt.admin ? "Admin User" : undefined} />
<ListItemText primary={"Signed in as: " + authenticationContext.user.username} secondary={ authenticationContext.isAdmin() ? "Admin User" : undefined} />
</ListItem>
</List>
</CardContent>