https://supertokens.com/ logo
Title
j

John Oliver

01/13/2023, 6:48 AM
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

rp

01/13/2023, 6:49 AM
hey!
if not the supertokens id, which ID are you talking about?
which recipe are you using?
j

John Oliver

01/13/2023, 6:50 AM
ThirdPartyEmailPassword
j

John Oliver

01/13/2023, 7:31 AM
Are signup and signin methods implemented in the same function? The both use the same methods?
r

rp

01/13/2023, 8:07 AM
for social login yea. Not for email password
j

John Oliver

01/13/2023, 8:32 AM
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

rp

01/13/2023, 8:33 AM
you also need to set the status code
for example, like this:
input.options.res.setStatusCode(200); // or any other status code
                    input.options.res.sendJSONResponse({
                        message: "my custom response",
                        //...
                    })
j

John Oliver

01/15/2023, 4:03 PM
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

rp

01/15/2023, 6:06 PM
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

John Oliver

01/16/2023, 4:26 AM
I added console log before the 200 status code and I got nothing in the console.
This is the response I am getting:
{
  "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:
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

rp

01/16/2023, 5:27 AM
hey @John Oliver im really confused about what you want to do here. Maybe re explain please.
j

John Oliver

01/16/2023, 5:29 AM
Does supertokens creates its tables in the database whenever user signs up or signs in?
r

rp

01/16/2023, 5:29 AM
it adds a row to the table. Yes
j

John Oliver

01/16/2023, 5:30 AM
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

rp

01/16/2023, 5:31 AM
yes
j

John Oliver

01/16/2023, 5:32 AM
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

rp

01/16/2023, 5:36 AM
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

John Oliver

01/16/2023, 5:39 AM
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

jscyo

01/16/2023, 5:48 AM
Hey @John Oliver , 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

John Oliver

01/16/2023, 5:52 AM
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

jscyo

01/16/2023, 5:59 AM
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

John Oliver

01/16/2023, 6:01 AM
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

jscyo

01/16/2023, 6:16 AM
Give me a second I will send a snippet to you
j

John Oliver

01/16/2023, 6:20 AM
ok
j

jscyo

01/16/2023, 6:26 AM
Hey @John Oliver 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

John Oliver

01/16/2023, 7:14 AM
I have added a comment on that please look at that.
r

rp

01/16/2023, 7:18 AM
hey @John Oliver 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

John Oliver

01/16/2023, 7:21 AM
Take the money then. But please help me.
r

rp

01/16/2023, 7:22 AM
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

lancekey

01/17/2023, 5:22 PM
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

rp

01/17/2023, 6:06 PM
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

lancekey

01/17/2023, 6:45 PM
ok great sounds like createUserIdMapping is what I'm looking for then