first
This commit is contained in:
107
src/contexts/ConfigContext.js
Normal file
107
src/contexts/ConfigContext.js
Normal file
@@ -0,0 +1,107 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import { createContext } from 'react';
|
||||
|
||||
// project import
|
||||
import config from 'config';
|
||||
import useLocalStorage from 'hooks/useLocalStorage';
|
||||
|
||||
// initial state
|
||||
const initialState = {
|
||||
...config,
|
||||
onChangeContainer: () => {},
|
||||
onChangeLocalization: () => {},
|
||||
onChangeMode: () => {},
|
||||
onChangePresetColor: () => {},
|
||||
onChangeDirection: () => {},
|
||||
onChangeMiniDrawer: () => {},
|
||||
onChangeMenuOrientation: () => {},
|
||||
onChangeFontFamily: () => {}
|
||||
};
|
||||
|
||||
// ==============================|| CONFIG CONTEXT & PROVIDER ||============================== //
|
||||
|
||||
const ConfigContext = createContext(initialState);
|
||||
|
||||
function ConfigProvider({ children }) {
|
||||
const [config, setConfig] = useLocalStorage('mantis-react-js-config', initialState);
|
||||
|
||||
const onChangeContainer = () => {
|
||||
setConfig({
|
||||
...config,
|
||||
container: !config.container
|
||||
});
|
||||
};
|
||||
|
||||
const onChangeLocalization = (lang) => {
|
||||
setConfig({
|
||||
...config,
|
||||
i18n: lang
|
||||
});
|
||||
};
|
||||
|
||||
const onChangeMode = (mode) => {
|
||||
setConfig({
|
||||
...config,
|
||||
mode
|
||||
});
|
||||
};
|
||||
|
||||
const onChangePresetColor = (theme) => {
|
||||
setConfig({
|
||||
...config,
|
||||
presetColor: theme
|
||||
});
|
||||
};
|
||||
|
||||
const onChangeDirection = (direction) => {
|
||||
setConfig({
|
||||
...config,
|
||||
themeDirection: direction
|
||||
});
|
||||
};
|
||||
|
||||
const onChangeMiniDrawer = (miniDrawer) => {
|
||||
setConfig({
|
||||
...config,
|
||||
miniDrawer
|
||||
});
|
||||
};
|
||||
|
||||
const onChangeMenuOrientation = (layout) => {
|
||||
setConfig({
|
||||
...config,
|
||||
menuOrientation: layout
|
||||
});
|
||||
};
|
||||
|
||||
const onChangeFontFamily = (fontFamily) => {
|
||||
setConfig({
|
||||
...config,
|
||||
fontFamily
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<ConfigContext.Provider
|
||||
value={{
|
||||
...config,
|
||||
onChangeContainer,
|
||||
onChangeLocalization,
|
||||
onChangeMode,
|
||||
onChangePresetColor,
|
||||
onChangeDirection,
|
||||
onChangeMiniDrawer,
|
||||
onChangeMenuOrientation,
|
||||
onChangeFontFamily
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</ConfigContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
ConfigProvider.propTypes = {
|
||||
children: PropTypes.node
|
||||
};
|
||||
|
||||
export { ConfigProvider, ConfigContext };
|
||||
145
src/contexts/JWTContext.js
Normal file
145
src/contexts/JWTContext.js
Normal file
@@ -0,0 +1,145 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import { createContext, useEffect, useReducer } from 'react';
|
||||
|
||||
// third-party
|
||||
import { Chance } from 'chance';
|
||||
import jwtDecode from 'jwt-decode';
|
||||
|
||||
// reducer - state management
|
||||
import { LOGIN, LOGOUT } from 'store/reducers/actions';
|
||||
import authReducer from 'store/reducers/auth';
|
||||
|
||||
// project import
|
||||
import Loader from 'components/Loader';
|
||||
import axios from 'utils/axios';
|
||||
|
||||
const chance = new Chance();
|
||||
|
||||
// constant
|
||||
const initialState = {
|
||||
isLoggedIn: false,
|
||||
isInitialized: false,
|
||||
user: null
|
||||
};
|
||||
|
||||
const verifyToken = (serviceToken) => {
|
||||
if (!serviceToken) {
|
||||
return false;
|
||||
}
|
||||
const decoded = jwtDecode(serviceToken);
|
||||
/**
|
||||
* Property 'exp' does not exist on type '<T = unknown>(token: string, options?: JwtDecodeOptions | undefined) => T'.
|
||||
*/
|
||||
return decoded.exp > Date.now() / 1000;
|
||||
};
|
||||
|
||||
const setSession = (serviceToken) => {
|
||||
if (serviceToken) {
|
||||
localStorage.setItem('serviceToken', serviceToken);
|
||||
axios.defaults.headers.common.Authorization = `Bearer ${serviceToken}`;
|
||||
} else {
|
||||
localStorage.removeItem('serviceToken');
|
||||
delete axios.defaults.headers.common.Authorization;
|
||||
}
|
||||
};
|
||||
|
||||
// ==============================|| JWT CONTEXT & PROVIDER ||============================== //
|
||||
|
||||
const JWTContext = createContext(null);
|
||||
|
||||
export const JWTProvider = ({ children }) => {
|
||||
const [state, dispatch] = useReducer(authReducer, initialState);
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
try {
|
||||
const serviceToken = window.localStorage.getItem('serviceToken');
|
||||
if (serviceToken && verifyToken(serviceToken)) {
|
||||
setSession(serviceToken);
|
||||
const response = await axios.get('/api/account/me');
|
||||
const { user } = response.data;
|
||||
dispatch({
|
||||
type: LOGIN,
|
||||
payload: {
|
||||
isLoggedIn: true,
|
||||
user
|
||||
}
|
||||
});
|
||||
} else {
|
||||
dispatch({
|
||||
type: LOGOUT
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
dispatch({
|
||||
type: LOGOUT
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
init();
|
||||
}, []);
|
||||
|
||||
const login = async (email, password) => {
|
||||
const response = await axios.post('/api/account/login', { email, password });
|
||||
const { serviceToken, user } = response.data;
|
||||
setSession(serviceToken);
|
||||
dispatch({
|
||||
type: LOGIN,
|
||||
payload: {
|
||||
isLoggedIn: true,
|
||||
user
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const register = async (email, password, firstName, lastName) => {
|
||||
// todo: this flow need to be recode as it not verified
|
||||
const id = chance.bb_pin();
|
||||
const response = await axios.post('/api/account/register', {
|
||||
id,
|
||||
email,
|
||||
password,
|
||||
firstName,
|
||||
lastName
|
||||
});
|
||||
let users = response.data;
|
||||
|
||||
if (window.localStorage.getItem('users') !== undefined && window.localStorage.getItem('users') !== null) {
|
||||
const localUsers = window.localStorage.getItem('users');
|
||||
users = [
|
||||
...JSON.parse(localUsers),
|
||||
{
|
||||
id,
|
||||
email,
|
||||
password,
|
||||
name: `${firstName} ${lastName}`
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
window.localStorage.setItem('users', JSON.stringify(users));
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
setSession(null);
|
||||
dispatch({ type: LOGOUT });
|
||||
};
|
||||
|
||||
const resetPassword = async () => {};
|
||||
|
||||
const updateProfile = () => {};
|
||||
|
||||
if (state.isInitialized !== undefined && !state.isInitialized) {
|
||||
return <Loader />;
|
||||
}
|
||||
|
||||
return <JWTContext.Provider value={{ ...state, login, logout, register, resetPassword, updateProfile }}>{children}</JWTContext.Provider>;
|
||||
};
|
||||
|
||||
JWTProvider.propTypes = {
|
||||
children: PropTypes.node
|
||||
};
|
||||
|
||||
export default JWTContext;
|
||||
Reference in New Issue
Block a user