Struggling to get understand how to get and set us...
# support-questions
b
Struggling to get understand how to get and set user metadata. I'm currently using Netlify for backend. Do I need add these code snippets as Netlify functions? https://supertokens.com/docs/passwordless/common-customizations/usermetadata/get-data
r
hey @BirdMan - yes you do need to add them into your existing netlify functions - either in your APIs or in supertokens.init as customisations.
b
thanks, I got it sorted out! So do I need to do another API call to get usermetadata in the front end? This is what I have now:
Copy code
const sessionExists = await Session.doesSessionExist();
if (sessionExists) {
  const user_id = await Session.getUserId();
  const userDetails = await getUserDetails();
}
getUserDetails
calls the netlify function. Is there a cleaner way to do this?
r
You could add the stuff you want to the session's access token payload and access that directly on the frontend
b
nice, that works! Thanks for responding so quickly here
Is there a way to also get the user's phoneNumber in the payload?
r
Yea. Override the createNewSession function on the backend
b
thanks, where can I get the user phone number from? It's not in metadata and cant' see an endpoint for it in passwordless recipe for node
r
there are functions exposed by the passwordless recipe like getUserById
b
perfect!
@rp if I'm refreshing the session using
Session.attemptRefreshingSession();
- what's the best way to get updated metadata? Metadata in payload is still outdated.
Copy code
export const refreshSession = async function() {
  await Session.getAccessToken();
  let sessionExists = await Session.doesSessionExist();
  if (sessionExists) {
    let user_id = await Session.getUserId();
    let accessTokenPayload = await Session.getAccessTokenPayloadSecurely();
    const { metadata } = await getUserDetails();
    user.set({
      id: user_id,
      phone_number: accessTokenPayload.phone_number,
      metadata: metadata
    });
  }
};
Currently have this, but if you refresh page it won't use the updated metadata
r
I’m not quite sure what’s going on here. Metadata update should happen only in the backend.
b
@rp I have a front end component where users can change metadata (their team name). I can change this with a post request to a netlify function, but I'm still getting the existing metadata even though I can see it's updated correctly inside user dashboard
r
The metadata is not added to the session automatically
You need to also update the session manually after adding to the metadata
Which you can do using session.mergeIntoAccessTokenPayload on the backend
b
ah yeah thanks, I can see here. Is there an example of this netlify function that doesn't use typescript?
r
There isn’t an example for that.
b
think I have it figured out thanks
Copy code
export const refreshSession = async function() {
  const { metadata } = await getUserDetails();
  await Session.attemptRefreshingSession();
  let sessionExists = await Session.doesSessionExist();
  if (sessionExists) {
    let user_id = await Session.getUserId();
    let accessTokenPayload = await Session.getAccessTokenPayloadSecurely();
    user.set({
      id: user_id,
      phone_number: accessTokenPayload.phone_number,
      metadata: accessTokenPayload.metadata
    });
  }
};
so getUserDetails is a netlify function that gets updated metadata and merges into access token. Then I am refreshing session and updating my front end store
@rp thanks so much for this, think we should be able to roll out the new auth this week. So we don't have to manage refreshing sessions at all? (Except for in this instance) - the 100 day inactivity will reset each time someone accesses the application?
r
yes
b
@rp Thanks so much for your help and quick responsiveness here. Just finished migrating 3000+ users from Auth0. https://www.saturdayquiztime.com.au/
2 Views