How can get email from current user logged in react-admin?
I am working in admin website using react-admin framework.
https://marmelab.com/react-admin/Readme.html
I need to show current user email that is logged. How can I get this functionality?
Regards
UPDATE:
There is a dedicated hook to retrieve this kind of data called useGetIdentity
ORIGINAL:
I have implemented a custom verb in the authProvier
similarly to the Dwadelfri's answer. It gets pretty handy because you can access it through the built in hook useAuthProvider
inside authProvider.js
import decodeJwt from 'jwt-decode';
const getCurrentUser = () => {
// the place where you saved user's data on login
const user = JSON.parse(localStorage.getItem("user"));
const decodedToken = decodeJwt(user.token);
return {
...user,
...decodedToken,
};
}
export default {
login: loginHandler,
logout: logoutHandler,
checkAuth: checkAuthHandler,
checkError: checkErrorHandler,
getPermissions: getPermissionsHandler,
//custom verbs
signUp: signUpHandler,
getCurrentUser: getCurrentUser,
};
Then the code looks pretty neat when you decide to call it:
const authProvider = useAuthProvider();
const user = authProvider.getCurrentUser();