rename jwt to user in authentication context, in prepartion for filtering display by user level
This commit is contained in:
parent
d5efbe4b18
commit
22d922c699
@ -13,7 +13,7 @@ export class AuthenticatedRoute extends React.Component {
|
|||||||
const { raiseNotification, authenticationContext, component: Component, ...rest } = this.props;
|
const { raiseNotification, authenticationContext, component: Component, ...rest } = this.props;
|
||||||
const { location } = this.props;
|
const { location } = this.props;
|
||||||
const renderComponent = (props) => {
|
const renderComponent = (props) => {
|
||||||
if (authenticationContext.jwt) {
|
if (authenticationContext.isAuthenticated()) {
|
||||||
return (
|
return (
|
||||||
<Component {...props} />
|
<Component {...props} />
|
||||||
);
|
);
|
||||||
|
@ -27,14 +27,13 @@ class AuthenticationWrapper extends React.Component {
|
|||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.refresh = this.refresh.bind(this);
|
|
||||||
this.signIn = this.signIn.bind(this);
|
|
||||||
this.signOut = this.signOut.bind(this);
|
|
||||||
this.state = {
|
this.state = {
|
||||||
context: {
|
context: {
|
||||||
refresh: this.refresh,
|
refresh: this.refresh,
|
||||||
signIn: this.signIn,
|
signIn: this.signIn,
|
||||||
signOut: this.signOut
|
signOut: this.signOut,
|
||||||
|
isAuthenticated: this.isAuthenticated,
|
||||||
|
isAdmin: this.isAdmin
|
||||||
},
|
},
|
||||||
initialized: false
|
initialized: false
|
||||||
};
|
};
|
||||||
@ -72,44 +71,53 @@ class AuthenticationWrapper extends React.Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
refresh() {
|
refresh = () => {
|
||||||
var accessToken = localStorage.getItem(ACCESS_TOKEN);
|
var accessToken = localStorage.getItem(ACCESS_TOKEN);
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
authorizedFetch(VERIFY_AUTHORIZATION_ENDPOINT)
|
authorizedFetch(VERIFY_AUTHORIZATION_ENDPOINT)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
const jwt = response.status === 200 ? jwtDecode(accessToken) : undefined;
|
const user = response.status === 200 ? jwtDecode(accessToken) : undefined;
|
||||||
this.setState({ initialized: true, context: { ...this.state.context, jwt } });
|
this.setState({ initialized: true, context: { ...this.state.context, user } });
|
||||||
}).catch(error => {
|
}).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);
|
this.props.raiseNotification("Error verifying authorization: " + error.message);
|
||||||
});
|
});
|
||||||
} else {
|
} 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 {
|
try {
|
||||||
this.setState({ context: { ...this.state.context, jwt: jwtDecode(accessToken) } });
|
this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } });
|
||||||
localStorage.setItem(ACCESS_TOKEN, accessToken);
|
localStorage.setItem(ACCESS_TOKEN, accessToken);
|
||||||
} catch (err) {
|
} 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);
|
this.props.raiseNotification("Failed to parse JWT " + err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
signOut() {
|
signOut = () => {
|
||||||
localStorage.removeItem(ACCESS_TOKEN);
|
localStorage.removeItem(ACCESS_TOKEN);
|
||||||
this.setState({
|
this.setState({
|
||||||
context: {
|
context: {
|
||||||
...this.state.context,
|
...this.state.context,
|
||||||
jwt: undefined
|
user: undefined
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.props.raiseNotification("You have signed out.");
|
this.props.raiseNotification("You have signed out.");
|
||||||
history.push('/');
|
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))
|
export default withStyles(styles)(withNotifier(AuthenticationWrapper))
|
||||||
|
@ -8,9 +8,9 @@ import * as Authentication from './Authentication';
|
|||||||
|
|
||||||
class UnauthenticatedRoute extends React.Component {
|
class UnauthenticatedRoute extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
const { component:Component, ...rest } = this.props;
|
const { authenticationContext, component:Component, ...rest } = this.props;
|
||||||
const renderComponent = (props) => {
|
const renderComponent = (props) => {
|
||||||
if (this.props.authenticationContext.jwt) {
|
if (authenticationContext.isAuthenticated()) {
|
||||||
return (<Redirect to={Authentication.fetchLoginRedirect()} />);
|
return (<Redirect to={Authentication.fetchLoginRedirect()} />);
|
||||||
}
|
}
|
||||||
return (<Component {...props} />);
|
return (<Component {...props} />);
|
||||||
|
@ -189,7 +189,7 @@ class MenuAppBar extends React.Component {
|
|||||||
<AccountCircleIcon />
|
<AccountCircleIcon />
|
||||||
</Avatar>
|
</Avatar>
|
||||||
</ListItemAvatar>
|
</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>
|
</ListItem>
|
||||||
</List>
|
</List>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
|
Loading…
Reference in New Issue
Block a user