Hi, I was wondering how can I can immediately upd...
# support-questions-legacy
k
Hi, I was wondering how can I can immediately update the token? When ever I create a user, I add a role to the user in the override, but I can't access them roles on the frontend(react-native) until I log out and back in. So how can I make the effect take place immediately?
r
Hey! What are you overriding to do this? Can I see the whole override block?
k
Copy code
javascript
import UserRoles from "supertokens-node/recipe/userroles";

import { User } from "../models";

const signInUpOverride = (originalImplementation: any) => {
  return {
    ...originalImplementation,

    thirdPartySignInUpPOST: async function (input: any) {
      if (originalImplementation.thirdPartySignInUpPOST === undefined) {
        throw Error("Should never come here");
      }

      let response = await originalImplementation.thirdPartySignInUpPOST(input);

      if (response.status === "OK") {
        if (response.createdNewUser) {
          await UserRoles.addRoleToUser(response.user.id, "pending");

         
        } else {
          const user = await User.findOne({
            userId: response.user.id,
          });
          if (user) {
            user.logins = [...user.logins, new Date()];
            await user.save();
          }
        }
      }

      return response;
    },
  };
};

export default signInUpOverride;
Copy code
javascript
Session.init({
      override: {
        functions: (originalImplementation) => {
          return sessionsOverride(originalImplementation);
        },
      },
    }),
r
right. So you need to update the session to have the role as well:
Copy code
ts
import UserRoles from "supertokens-node/recipe/userroles";

import { User } from "../models";

const signInUpOverride = (originalImplementation: any) => {
  return {
    ...originalImplementation,

    thirdPartySignInUpPOST: async function (input: any) {
      if (originalImplementation.thirdPartySignInUpPOST === undefined) {
        throw Error("Should never come here");
      }

      let response = await originalImplementation.thirdPartySignInUpPOST(input);

      if (response.status === "OK") {
        if (response.createdNewUser) {
          await UserRoles.addRoleToUser(response.user.id, "pending");
          await response.session.updateAccessTokenPayload({
            ...response.session.getAccessTokenPayload(),
            role: "pending"
          });
         
        } else {
          const user = await User.findOne({
            userId: response.user.id,
          });
          if (user) {
            user.logins = [...user.logins, new Date()];
            await user.save();
          }
        }
      }

      return response;
    },
  };
};

export default signInUpOverride;
I added:
Copy code
await response.session.updateAccessTokenPayload({
            ...response.session.getAccessTokenPayload(),
            role: "pending"
          });
k
Ah ok, I'll try that now, Thank you!
r
or rather, you want to do this inside the
response.status === "OK"
if statement (at the end of it)
so it updates the session during sign in and sign up
k
Which line should I add this on?
r
At the end of
if (response.status === "OK") {
block
k
Ah ok, thank you!
Neither of them seem to work, and I still need to log out and in again to update it
r
did you rebuild your code? No cache? Can you log somethign before you call
updateAccessTokenPayload
k
Yeah it is rebuilt, I put in some console.logs
r
and the log gets printed out?
Can I see where you have put the updateAccessTokenPayload call?
k
Yeah
Copy code
javascript
import UserRoles from "supertokens-node/recipe/userroles";

import { User } from "../models";

const signInUpOverride = (originalImplementation: any) => {
  return {
    ...originalImplementation,

    thirdPartySignInUpPOST: async function (input: any) {
      if (originalImplementation.thirdPartySignInUpPOST === undefined) {
        throw Error("Should never come here");
      }

      let response = await originalImplementation.thirdPartySignInUpPOST(input);

      if (response.status === "OK") {
        if (response.createdNewUser) {
          await UserRoles.addRoleToUser(response.user.id, "pending");

          console.log("Pre");

          await response.session.updateAccessTokenPayload({
            ...response.session.getAccessTokenPayload(),
            role: "pending",
          });

          console.log("After");
        } else {
          const user = await User.findOne({
            userId: response.user.id,
          });
          if (user) {
            user.logins = [...user.logins, new Date()];
            await user.save();
          }
        }
        response.status === "OK";
      }

      return response;
    },
  };
};

export default signInUpOverride;
r
So you are calling
updateAccessTokenPayload
only when
createdNewUser
is true. You want to call that even when it's false.
k
What is wrong with how I am currently doing it? I also got another method for when I log in each time and it gives me the correct roles?
It works perfectly when I log out after I create the account?
r
so the way you are doing it means that the session will be updated only when the user signs up and not signs in. Cause in sign in, createdNewUser boolean is false.
k
That's ok, I only want to get the roles on the frontend when the user signs up, because I got this override for adding roles to the session too
Copy code
javascript
import UserRoles from "supertokens-node/recipe/userroles";

const sessionsOverride = (originalImplementation: any) => {
  return {
    ...originalImplementation,
    createNewSession: async function (input: {
      userId: any;
      accessTokenPayload: any;
    }) {
      let userId = input.userId;

      let roles = await UserRoles.getRolesForUser(userId);

      input.accessTokenPayload = {
        ...input.accessTokenPayload,
        roles,
      };

      return originalImplementation.createNewSession(input);
    },
  };
};

export default sessionsOverride;
r
ah ok.. then it should work
k
Oh
r
one difference is that during sign up, you add
role
but in
createNewSession
, you add
roles
so might wanna make that consistent.
k
Ah true, But I still have a problem. I'm using react-native. When the user is created it adds the role, but doesn't return the roles it has to the frontend. It returns the roles to the frontend only when the user logs and back in. I want it so the user immediately has roles added to the frontend without having to relog
r
can you prnt out the value of session.getAccessTokenPayload() right after you call the
updateAccessTokenPayload
function?
k
Oh it's undefined
Copy code
javascript
const output = await response.session.updateAccessTokenPayload({
            ...response.session.getAccessTokenPayload(),
            role: "pending",
          });

          console.log(output);
r
do
console.log(response.session.getAccessTokenPayload())
i would suggest that you also see the types for the functions when you call them 🙂
clearly,
updateAccessTokenPayload
returns void.
k
Oh I did something wrong then, sorry 😅
r
no worries 🙂
k
That is the response I got
Copy code
javascript
    const roles: any = (await SuperTokens.getAccessTokenPayloadSecurely())[
      "roles"
    ];

    console.log(roles);
That is how I am getting the roles from the frontend
r
please check how you are using updateAccessTokenPayload.
You are adding a different key there vs in createNewSession
role
vs
roles
im sure you can figure this out now 🙂
k
Ah I think I see it!
Yeah it works, thank you!
r
nice