mirror of
https://github.com/Telecominfraproject/wlan-cloud-ui.git
synced 2025-11-02 03:37:59 +00:00
added User Context
This commit is contained in:
@@ -2,27 +2,39 @@ import React from 'react';
|
||||
import T from 'prop-types';
|
||||
import { Route, Redirect } from 'react-router-dom';
|
||||
|
||||
import UserProvider from 'contexts/UserProvider';
|
||||
import MasterLayout from 'containers/MasterLayout';
|
||||
import { AUTH_TOKEN } from 'constants/index';
|
||||
|
||||
import { getItem } from 'utils/localStorage';
|
||||
import { parseJwt } from 'utils/jwt';
|
||||
|
||||
const ProtectedRouteWithLayout = ({ component: Component, ...rest }) => (
|
||||
<Route
|
||||
{...rest}
|
||||
render={props =>
|
||||
getItem(AUTH_TOKEN) ? (
|
||||
<MasterLayout>
|
||||
<Component {...props} />
|
||||
</MasterLayout>
|
||||
) : (
|
||||
render={props => {
|
||||
const token = getItem(AUTH_TOKEN);
|
||||
|
||||
if (token) {
|
||||
const jwt = parseJwt(token.access_token);
|
||||
|
||||
return (
|
||||
<UserProvider email={jwt.userName} role={jwt.userRole} customerId={jwt.customerId}>
|
||||
<MasterLayout>
|
||||
<Component {...props} />
|
||||
</MasterLayout>
|
||||
</UserProvider>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Redirect
|
||||
to={{
|
||||
pathname: '/login',
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
|
||||
5
app/contexts/UserContext/index.js
Normal file
5
app/contexts/UserContext/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import React from 'react';
|
||||
|
||||
const UserContext = React.createContext();
|
||||
|
||||
export default UserContext;
|
||||
17
app/contexts/UserProvider/index.js
Normal file
17
app/contexts/UserProvider/index.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import UserContext from 'contexts/UserContext';
|
||||
|
||||
const UserProvider = ({ children, email, role, customerId }) => (
|
||||
<UserContext.Provider value={{ email, role, customerId }}>{children}</UserContext.Provider>
|
||||
);
|
||||
|
||||
UserProvider.propTypes = {
|
||||
children: PropTypes.node.isRequired,
|
||||
email: PropTypes.string.isRequired,
|
||||
role: PropTypes.string.isRequired,
|
||||
customerId: PropTypes.number.isRequired,
|
||||
};
|
||||
|
||||
export default UserProvider;
|
||||
@@ -1,7 +1,6 @@
|
||||
export function parseJwt(token) {
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace('-', '+').replace('_', '/');
|
||||
return JSON.parse(window.atob(base64));
|
||||
const base64Url = token.split('.')[0];
|
||||
return JSON.parse(window.atob(base64Url));
|
||||
}
|
||||
|
||||
export function isTokenExpired(token) {
|
||||
|
||||
Reference in New Issue
Block a user