Commit 55ecae35 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

Merge branch '694-token-isn-t-expired-on-the-second-tab' into 'master'

Token isn`t expired on the second tab

Closes #694

See merge request genesys-pgr/genesys-ui!676
parents f91f1498 8644c6e2
import * as React from 'react';
import * as cookies from 'es-cookie';
import {connect} from 'react-redux';
import { Redirect } from 'react-router-dom';
import { bindActionCreators } from 'redux';
import { updateAuth } from 'user/actions/public';
interface IAuthorizedRouteComponentProps extends React.ClassAttributes<any> {
children: any;
login: any;
authorities: string[];
updateAuth: (token: string) => any;
}
class AuthorizedRouteComponent extends React.Component<IAuthorizedRouteComponentProps, any> {
......@@ -15,6 +19,7 @@ class AuthorizedRouteComponent extends React.Component<IAuthorizedRouteComponent
this.state = {
show: false,
tokenToUpdate: null,
};
}
......@@ -24,9 +29,27 @@ class AuthorizedRouteComponent extends React.Component<IAuthorizedRouteComponent
const show = userRoles.map((e) => authorities.indexOf(e) > -1).reduce((p, c) => p || c);
try {
if (typeof window !== 'undefined') {
const cookieToken = cookies.get('access_token');
if (cookieToken !== login.access_token) {
return { show: false, tokenToUpdate: cookieToken };
}
}
} catch (e) {
console.log('check token error: ', e);
}
return show ? {show: true} : null;
}
public componentWillUnmount() {
const { tokenToUpdate } = this.state;
if (tokenToUpdate) {
this.props.updateAuth(tokenToUpdate);
}
}
public render() {
const {children, ...rest} = this.props;
return this.state.show ? React.cloneElement(children, rest) : <Redirect push to="/login"/>;
......@@ -38,4 +61,8 @@ const mapStateToProps = (state) => ({
login: state.login,
});
export default connect(mapStateToProps)(AuthorizedRouteComponent);
const mapDispatchToProps = (dispatch) => bindActionCreators({
updateAuth,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(AuthorizedRouteComponent);
......@@ -39,6 +39,11 @@ export const loginUser = (payload) => {
};
};
export const updateAuth = (cookieToken) => (dispatch) => {
const parsedTokenData = jwt.decode(cookieToken);
return dispatch(loginUser({ access_token: cookieToken, ...parsedTokenData }));
};
export const loginRequest = (username, password) => (dispatch, getState) => {
return dispatch(apiLogin(username, password))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment