Set Cookie details using react
export const setUserDetails = (userDetails) => {
const { token } = userDetails;
Cookies.set('session-token', token, {
expires: 1 / 3.7, // expire in 1 hour
// secure: true,
domain: process.env.REACT_APP_HOST_NAME,
path: '/',
sameSite: 'lax',
});
};
Get Cookie details using react
export const getCurRefreshToken = () => {
return Cookies.get('refreshToken', {
domain: process.env.REACT_APP_HOST_NAME,
});
};
Remove cookie data using react
export const removeUserDetails = () => {
const options = {
path: '/',
domain: process.env.REACT_APP_HOST_NAME,
};
Cookies.remove('session-token', options);
};
GET USER WISE DETAILS
export const getUserDetails = () => {
try {
const token = Cookies.get('session-token', {
domain: process.env.REACT_APP_HOST_NAME,
});
const { Role, ...rest } = JwtDecode(token);
rest.roles =
Role[0] === 'ADMIN'
? 'Admin'
: Role[0] === 'CONTENT'
? 'OTHER'
: false;
return token
? {
...rest,
Role,
token: token,
}
: false;
} catch (e) {
return false;
}
};
Dependancy
import Cookies from 'js-cookie';
import JwtDecode from 'jwt-decode';
0 Comments