https://supertokens.com/ logo
#support-questions
Title
# support-questions
b

BirdMan

02/26/2023, 4:38 AM
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

rp

02/26/2023, 5:36 AM
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

BirdMan

02/26/2023, 6:04 AM
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

rp

02/26/2023, 6:06 AM
You could add the stuff you want to the session's access token payload and access that directly on the frontend
b

BirdMan

02/26/2023, 6:22 AM
nice, that works! Thanks for responding so quickly here
Is there a way to also get the user's phoneNumber in the payload?
r

rp

02/26/2023, 6:35 AM
Yea. Override the createNewSession function on the backend
b

BirdMan

02/26/2023, 6:53 AM
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

rp

02/26/2023, 6:54 AM
there are functions exposed by the passwordless recipe like getUserById
b

BirdMan

02/26/2023, 7:02 AM
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

rp

03/02/2023, 4:25 AM
I’m not quite sure what’s going on here. Metadata update should happen only in the backend.
b

BirdMan

03/02/2023, 4:31 AM
@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

rp

03/02/2023, 4:32 AM
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

BirdMan

03/02/2023, 4:43 AM
ah yeah thanks, I can see here. Is there an example of this netlify function that doesn't use typescript?
r

rp

03/02/2023, 4:51 AM
There isn’t an example for that.
b

BirdMan

03/02/2023, 4:53 AM
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

rp

03/02/2023, 5:29 AM
yes
b

BirdMan

03/03/2023, 6:40 AM
@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