How to modify the response object after singin/log...
# support-questions-legacy
j
How to modify the response object after singin/login request because the default response is email, timejoined and superTokenId. I want the id of the user (not the supertoken id) as well. How can I change this behavior?
r
hey!
if not the supertokens id, which ID are you talking about?
which recipe are you using?
j
ThirdPartyEmailPassword
j
Are signup and signin methods implemented in the same function? The both use the same methods?
r
for social login yea. Not for email password
j
Copy code
override: {
            apis: (originalImplementation: any) => {
              return {
                ...originalImplementation,
                emailPasswordEmailExistsGET: async function (input) {
                  input.options.res.sendJSONResponse({
                    message: 'my custom response',
                  });
                  return {
                    status: 'OK',
                    exists: false,
                  };
                },
                emailPasswordSignUpPOST: async function (input: any) {
                  if (
                    originalImplementation.emailPasswordSignUpPOST === undefined
                  ) {
                    throw Error('Should never come here');
                  }
                  const formFields: any = input.formFields;
                  const inputObject: any = {};
                  for (let index = 0; index < formFields.length; index++) {
                    const element = formFields[index];
                    inputObject[element.id] = element.value;
                  }
                  const { email, firstName, lastName, phoneNumber } =
                    inputObject;

                  // First we call the original implementation of signUpPOST.
                  const response: any =
                    await originalImplementation.emailPasswordSignUpPOST(input);

                  response['user'] = {
                    ...response['user'],
                    email,
                    firstName,
                    lastName,
                    phoneNumber,
                  };
                  console.log(response);
                  return response;
                },
I have tried this but this is not working for me.
r
you also need to set the status code
for example, like this:
Copy code
input.options.res.setStatusCode(200); // or any other status code
                    input.options.res.sendJSONResponse({
                        message: "my custom response",
                        //...
                    })
j
Copy code
emailPasswordEmailExistsGET: async function (input) {
                  input.options.res.setStatusCode(200);
                  input.options.res.sendJSONResponse({
                    message: 'my custom response',
                  });
                  return {
                    status: 'OK',
                    exists: false,
                  };
                },
This is not working. I am still getting the id that is generated by supertoken, timeJoined and email only.
r
Can you add a console log right before you set the 200 status code? Does that get printed out?
Also, what do you meant by getting the id generated by supertokens? This is a different api that you are overriding. Not the sign in / sign up api. Im confused
j
I added console log before the 200 status code and I got nothing in the console.
This is the response I am getting:
Copy code
{
  "status": "OK",
  "user": {
    "email": "mitulkheni695@gmail.com",
    "id": "37ffadc9-3002-48g4-95bc-5230a44c5b47",
    "timeJoined": 1673798066293
  }
}
look at the id now, It is generated by supertoken library. I have another method in my nest app that saves the user data on signup/signin method in the postgres database. I want to get that id that is auto generated (primary key like 1,2,3 etc) in the response
I have this code in my app:
Copy code
supertokens.init({
  appInfo: this.config.appInfo,
  supertokens: {
    connectionURI: this.config.connectionURI,
    apiKey: this.config.apiKey,
  },
  recipeList: [
    ThirdPartyEmailPassword.init({
      providers: [
        ThirdPartyEmailPassword.Google({
          clientSecret: 'TODO: GOOGLE_CLIENT_SECRET',
          clientId
        }),
      ],
      signUpFeature: {
        formFields: [
          {
            id: 'firstName',
          },
          {
            id: 'lastName',
          },
          {
            id: 'phoneNumber',
          },
        ],
      },
      override: {
        apis: (originalImplementation: any) => {
          return {
            ...originalImplementation,

            emailPasswordSignUpPOST: async function (input: any) {
              if (
                originalImplementation.emailPasswordSignUpPOST === undefined
              ) {
                throw Error
              }
             // retrieving data from input and saving logic goes here...
            },

            emailPasswordEmailExistsGET: async function (input) {
              input.options.res.setStatusCode(200);
              input.options.res.sendJSONResponse({
                message: 'my custom response',
              });
              return {
                status: 'OK',
                exists: false,
              };
            },
          };
        },
      },
    }), 
  ],
});
OK, I have tried it from the frontend. Now, I am getting the log on the signup method. but I am not getting the log on the signin request
r
hey @johnoliver2342 im really confused about what you want to do here. Maybe re explain please.
j
Does supertokens creates its tables in the database whenever user signs up or signs in?
r
it adds a row to the table. Yes
j
OK, apart from auto generated table by supertoken, I have my own implementation of saving users in the database and that is a different table. Do you get that?
r
yes
j
Good, now I want to get the users id (primary key that is auto generated by postgres.) from the tables that I have implemented** not the supertokens generated tables**
Now, Whenever user signs in he will pass the supertoken id in the request. Is that Correct?
r
right. So what you want to do is to do user ID mapping of supertokens and your external user ID in the sign up override in override.functions config.
@jscyo can help here.
j
Yes, because I have another functionalities as well in my app where only authorized user can save the data in the database. I do not want to save that long super-token id in my other tables, I want to save the user id that is generated by postgres database by my own implementation.
j
Hey @johnoliver2342 , you can map the userId generated by the postgres database to your supertokens Id by adding the following
SuperTokens.createUserIdMapping({ superTokensUserId: "SUPERTOKENS_USER_ID", externalUserId: "POSTGRES_USER_ID"});
to the signUp override code.
j
Here, I have the table called users in the postgres database and there are columns like id(primary key - auto generated), user's details and superToken_id (This table is my own implementation of user). Now, How can I do the same that you have mentioned?
j
If you add the function I mentioned above to your
emailPasswordSignUpPOST:
override and pass the auto-generated postgres userId and SuperTokens userId to it, SuperTokens will now use the postgres userId for the user. So all subsequent signIn requests or function calls will return the postgres userId.
j
Ok
Sorry but I am confused like where to place this code in my file. I have tried it but I am getting an error. Can you show with the example. You already have some code.
j
Give me a second I will send a snippet to you
j
ok
j
Hey @johnoliver2342 you can look at this https://gist.github.com/jscyo/8a26e4324b6943e1b240fa5259554437, one clarification to make is that you should use the functional override for emailpassword signup
j
I have added a comment on that please look at that.
r
hey @johnoliver2342 it will be difficult for us to give you step by step support like this for free. We have already shared the code with you for how it can be done. Hope that helps.
j
Take the money then. But please help me.
r
I can schedule a call today to discuss support pricing if you want. You could pick a slot here: https://supertokens.com/call-user
Btw, our support plan starts at $5,000 per year. So if that's something that you can do, only then it makes sense to get on a call.
l
This is something I'm trying to accomplish as well except I was thinking to store my postgresId as metadata on the user in supertokens
what's the difference between createUserIdMapping and using metadata?
r
Create user id mapping maps the supertokens user id an external user id that you provide such that logins yield the external user id as opposed to the supertokens user id
Whereas metadata is just storing any json against a user Id for later retrieval
l
ok great sounds like createUserIdMapping is what I'm looking for then
4 Views