https://supertokens.com/ logo
override order
p

Pogant

04/20/2023, 9:55 AM
Hey, SuperTokens team. I have modified original implementation of sign up to add some metadata. The problem is the initial session does not have it. Only after a relog the data is visible. Where is the best spot to add extra code to handle this case?
r

rp

04/20/2023, 9:56 AM
hey @Pogant you may want to override the function in
override.functions
instead of
override.apis
p

Pogant

04/20/2023, 9:58 AM
Should I consider moving all custom logic like assign role/add metadata to functions instead of apis?
r

rp

04/20/2023, 9:58 AM
yes
p

Pogant

04/20/2023, 9:59 AM
Got ya, thx.
s

simonasaservice

04/24/2023, 1:28 AM
Hey there, I think I'm experiencing a similar problem with my next.js implementation, and I have not got it to work yet. @Pogant did it work for you?
Here's my backendconfig
r

rp

04/24/2023, 6:01 AM
hey @simonasaservice you can always use the
response.session.mergeIntoAccessTokenPayload
funciton to add stuff to the session after calling
await originalImplementation.emailPasswordSignUpPOST
s

simonasaservice

04/24/2023, 1:56 PM
Thanks for the tip @rp! This was successful. Instead of using
UserMetadata.updateUserMetadata(user.id, metadata)
I simply used
response.session.mergeIntoAccessTokenPayload(metadata)
As you can see from the Dashboard screenshot, this worked: it added the correct metadata object
json
{
    "first_name": "Jason",
    "last_name": "Bourne",
    "profile_pic": "https://www.gravatar.com/avatar/8abfbbf8094b5b9cfecfff114e509710"
}
And it recognized the
first_name
and
last_name
values in the dashboard.

https://cdn.discordapp.com/attachments/1098547546256846890/1100057726903668736/image.png

However, I am having difficulty calling these values on the front end: the session is not returning the values of the metadata.
javascript
import SessionReact from "supertokens-auth-react/recipe/session";
import { useSessionContext } from "supertokens-auth-react/recipe/session";
import SuperTokensReact from "supertokens-auth-react";
import { SessionContextUpdate } from "supertokens-auth-react/lib/build/recipe/session/types";


export function SiteHeader() {
  const session = useSessionContext();

  if (session.loading) {
    return null;
    }
    const { doesSessionExist, userId, accessTokenPayload } = session as SessionContextUpdate;

    if (!doesSessionExist) {
      return null;
    }
    
    const { first_name, last_name } = accessTokenPayload;
    console.log(accessTokenPayload); // returns {}
    console.log(first_name);  // returns undefined
    console.log(last_name);  // returns undefined
};
r

rp

04/24/2023, 2:37 PM
Can I see how you have called the mergeIntoAccessTokenPayload function?
response.session.mergeIntoAccessTokenPayload(metadata);
Which seems to work great
r

rp

04/24/2023, 2:47 PM
you need to add
await
in front of
response.session.mergeIntoAccessTokenPayload(metadata);
s

simonasaservice

04/24/2023, 2:49 PM
Thanks RP, good point. But it does work well right now to add the metadata: my problem is in retrieving the metadata once it has been added
r

rp

04/24/2023, 2:49 PM
yea.. you need to add
await
in front
the response is being sent before
mergeIntoAccessTokenPayload
has finished modifying the response headers.
that's why you see the frontend not being updated.
s

simonasaservice

04/24/2023, 2:53 PM
Thanks. I've added the await keyword so now my backendconfig reads
await response.session.mergeIntoAccessTokenPayload(metadata);
And the metadata is added correctly. However, on the front end, with the code here, it is still undefined for first_name and last_name https://discord.com/channels/603466164219281420/1098547546256846890/1100058427163693091
r

rp

04/24/2023, 3:09 PM
did you relogin?
if yes, can you send the response headers from the sign up API?
s

simonasaservice

04/24/2023, 3:34 PM
Hi RP, is this the info you want? it's the response headers from the signup request to http://localhost:3000/api/auth/signup
http
HTTP/1.1 200 OK
access-control-allow-origin: http://localhost:3000
vary: Origin, Accept-Encoding
access-control-allow-credentials: true
cache-control: no-cache, no-store, max-age=0, must-revalidate
front-token: eyJ1aWQiOiJjYmM3YTcxMi1lZTk2LTQ0MzctODI2My1kMjA4NGJmNjg0ZDIiLCJhdGUiOjE2ODIzNTM1NjA3OTksInVwIjp7fX0=
access-control-expose-headers: front-token
set-cookie: sAccessToken=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsInZlcnNpb24iOiIyIn0%3D.eyJzZXNzaW9uSGFuZGxlIjoiNWVmYWEzNjQtNThjNy00YWFmLWEwZWYtYjZkZjBkMDNmNGZhIiwidXNlcklkIjoiY2JjN2E3MTItZWU5Ni00NDM3LTgyNjMtZDIwODRiZjY4NGQyIiwicmVmcmVzaFRva2VuSGFzaDEiOiI0ZTU0MWRjZTY3NTA2ODY0ODQ1MjZkNGQ5ODliY2VhM2E1NGUzNDc3Yzk0NDE1NmUxZDQxNjAzMjA1MmVlZDNhIiwicGFyZW50UmVmcmVzaFRva2VuSGFzaDEiOm51bGwsInVzZXJEYXRhIjp7fSwiYW50aUNzcmZUb2tlbiI6bnVsbCwiZXhwaXJ5VGltZSI6MTY4MjM1MzU2MDc5OSwidGltZUNyZWF0ZWQiOjE2ODIzNDk5NjA3OTksImxtcnQiOjE2ODIzNDk5NjA3OTl9.ThpuChPj2xX%2Byqy9BLbOCSBjiBCF7EZUPKSct99BLBZdg296mzljOgew5o0X7Z8e5MBNt9D20bQMLj3F8z9TJ%2FFkIpuuqNS%2Fm8t%2FfXrV56Q%2BQlvFy9EXc4RMdL62pMtDJ%2BiF4XcNPnxZpJn9njLhW2mY3Dvhc6i2A6zwe7eaJcP4BGE5kaJxa2IiI78Rl2ACjdNAd9ioMKXWmkPA1VhvHEp2iWMYtBW1zbY4GsYgm%2BEA97zhOlBhRD05xlvtKBaqBwOdXXyPxLWiqdTLeR4BoWxcOnIYG32drEb%2FC6mp%2BVtqL4PIabaG1sbEheaw6gRz2DvSRqCpTcdCdPT0L3XzVA%3D%3D; Path=/; Expires=Wed, 31 Mar 2123 15:26:00 GMT; HttpOnly; SameSite=Lax
set-cookie: sRefreshToken=jVPkLmEO%2BPboAbq0%2Fst1GIWTE61%2F632q4bO4QhR5IMafsWbAGPy7KWI5IgRLvC8xjWLzZfFSQpo%2FFoa43VlumypChpImbebTRSCaaBvshkPjZVcMZFbiIctz9Rv%2B%2FBvQehyZqd3WulHXnnZkhpIFHS08BiOq4BW9REJCw7Xc4WMRzaUAmSMmOqAcePiTFfWXegv1QStyxx9u7JiC4bALmuKn67w3mY7LJJxTHTdrVVt102uaUPp3SAlgT2uMoSvkNE0JQSQxvaqVi8W%2BXF13.7e92c57ce66a807a1597815827061969bd3fca640662d8e279383e51caec03ae.V2; Path=/api/auth/session/refresh; Expires=Wed, 02 Aug 2023 15:26:00 GMT; HttpOnly; SameSite=Lax
content-type: application/json; charset=utf-8
etag: "asncqv24r63f"
date: Mon, 24 Apr 2023 15:26:01 GMT
content-encoding: gzip
connection: close
transfer-encoding: chunked
r

rp

04/24/2023, 4:01 PM
can i see the new code with the
await
?
have you rebuilt the app?
s

simonasaservice

04/24/2023, 6:03 PM
Aha! I had to rebuild the app with
npm run build
and now it works. This surprised me: is it not enough to simply stop and start the app?
r

rp

04/25/2023, 6:48 AM
well.. that depends on your setup. Nothing to do with supertokens