`UserContext` vs `UserMetadata` which one to choos...
# support-questions
s
UserContext
vs
UserMetadata
which one to choose for user profile data such as timezone, address, etc.?
n
Hi, So if you want any information to be stored for a given user you should use user UserMetaData
s
What are the use cases for
UserContext
?
n
The documentation probably explains it best, but it’s a way to make the override feature more powerful.
s
I assume that the only fields stored during signup are email and password. If I have to add common fields such as "name", I should override
signupPost
and from there, I should call
UserMetadata.updateUserMetadata(userId, { name: "John Doe" });
n
Yep thats correct, the signin/up functions (both frontend and backend) support custom fields and then you would use the
UserMetaData
to store the information
s
Follow up question I want this information(name, timezone, etc.) on all the pages so that I can show it in the menu bar. When I'm storing this info, I'm making usermetadata request on every page. Is there any more effective way to do this? My first instict goes to either storing this info in the token payload or the session info, how can I do that?
@nkshah2
n
Right on the backend you can add the information to the access token payload
You can then access it on the frontend using
getAccessTokenPayloadSecurely
s
how to add info to token payload?
n
Theres a couple ways to do that, what recipe are you using?
s
email password for now
later I'd add thirdpartyemail for social login as well
n
Ah Id recommend using thirdpartyemailpassword recipe instead then and just enable third party later. But to add to access token payload you can follow this doc: https://supertokens.com/docs/thirdpartyemailpassword/common-customizations/sessions/update-jwt-payload
If you want to add info to the access token on session creation you can follow this one instead: https://supertokens.com/docs/thirdpartyemailpassword/common-customizations/sessions/new-session
s
Ok. I assume this token/session info update is not saved in db so in order to persist this info we should first updater userMetaData and then get the userMetaData and update session/token info at the time of login. Is this right?
n
If you add it to the access token payload then it is persisted (so that the payload remains the same when the access token is refreshed)
So depending on how you want to use it, adding it to the access token payload is enough
But because there is a limit to cookie size, if you want to store a lot of information we recommend user meta data instead
s
and when we sign out and login again, I should add the info again to the token?
n
Yep
user meta data gets stored against the specific user so it is more suited to information that needs to be stored forever
You can do that and then add the specific info you need on your frontend to the access token payload as well
s
Thank you. Let me do that
I guess the right place to do this would be signin override?
n
Youd need this in sign up as well
s
This is what I have done now. I am calling following fn in
signInPost
and
signUpPOST
override
Copy code
async function storeUserMetaDataInTokenPayload(session, user) {
  let userMetaData = await UserMetadata.getUserMetadata(user.id);
  let currAccessTokenPayload = session.getAccessTokenPayload();
  console.debug("currAccessTokenPayload: " + JSON.stringify(currAccessTokenPayload));
  let accessTokenPayloadWithMetadata = Object.assign({}, userMetaData ? userMetaData.metadata : null, currAccessTokenPayload, userInfo);
  console.debug("accessTokenPayloadWithMetadata: " + JSON.stringify(accessTokenPayloadWithMetadata));
  await session.updateAccessTokenPayload(accessTokenPayloadWithMetadata);
}
And then I have a middleware for session verification on the backend where on successful session verification, I do this
req.user = session.getAccessTokenPayload()
. So now, I can access all user info via
req.user
on backend and in the token payload on the frontend.
Do you have any feedback for this?
n
Yeah that looks good