Is it true, that ``createNewSession`` get's called...
# support-questions
u
Is it true, that ``createNewSession`` get's called before ``emailPasswordSignUpPOST`` upon registration? I wanted to store a user in the db using ``emailPasswordSignUpPOST`` and store the information in the session using ``createNewSession``. Weirdly, this illustrations tells the opposite behaviour I am having: https://supertokens.com/docs/thirdpartyemailpassword/architecture
r
CreateNewSession gets called inside emailPasswordSignUpPOST function
If you want to store data in your db before createNewSession, you should override the signUp recipe function and not the api
The emailPasswordSignUpPOST api function first calls the signUp recipe function and based on the result, it calls the createNewSession recipe function
What do you mean? 😅
u
never mind, I just overlooked it. 👍 Thanks for your help
Sorry for re-opening this, but I hadn't had the time to implement this yet. This is how my override looks:
Copy code
javascript
override: {
          functions: (originalImplementation) => {
            return {
              ...originalImplementation,
              emailPasswordSignUp(input) {
                console.log(`emailPasswordSignUp`);
                console.log(input);

                return originalImplementation.emailPasswordSignUp(input);
              },
            }
          },
Form fields on the server side:
Copy code
javascript
        signUpFeature: {
          formFields: [
            {
              id: "username",
            },
          ],
        },
Form fields on the frontend:
Copy code
javascript
          signUpForm: {
            formFields: [
              {
                id: "username",
                label: "Username",
                placeholder: "Public visible username",
              },
            ],
            termsOfServiceLink: "https://example.com/terms-of-service",
            privacyPolicyLink: "https://example.com/privacy-policy",
          },
There is no information about the form fields in the request:
Copy code
emailPasswordSignUp
{
  email: 'manicraft25@gmail.com',
  password: 'sml12345',
  userContext: {}
}
r
hey!
You need to override the API, not the recipe function.
The API override is like:
Copy code
override: {
  apis: (originalImplementation) => {
    return {
      ...originalImplementation,
      emailPasswordSignUpPOST: async function (input) {
        // Your logic here. The input variable will contain all the form fields
      }
    }
  }
}
u
I am confused. Above you told me, that I should use the ``signUp`` for inserting the user. (click on the response link above to see what I mean)
r
well.. you get access to the request object in the API override. So you can read the form field in that, and save it in the context object (https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context) and then you can access it in the
signUp
recipe function override.
u
Like this?
Copy code
javascript
export const backendConfig = (): TypeInput => {
  return {
    framework: "express",
    supertokens: {
      connectionURI: "<REDACTED>",
      apiKey: process.env.SUPERTOKENS_API_KEY,
    },
    appInfo,
    recipeList: [
      ThirdPartyEmailPasswordNode.init({
        providers: [
          // We have provided you with development keys which you can use for testsing.
          // IMPORTANT: Please replace them with your own OAuth keys for production use.
          ThirdPartyEmailPasswordNode.Google({
            clientId:
              "<REDACTED>",
            clientSecret: "<REDACTED></REDACTED>",
          }),
        ],
        signUpFeature: {
          formFields: [
            {
              id: "username",
            },
          ],
        },
        override: {
          functions: (originalImplementation) => {
            return {
              ...originalImplementation,
              emailPasswordSignUp(input) {
                const usernameFromContext = input.userContext.username;
                console.log("usernameFromContext", usernameFromContext);

                return originalImplementation.emailPasswordSignUp(input);
              },
            };
          },
          apis: (originalImplementation) => {
            return {
              ...originalImplementation,
              emailPasswordSignUpPOST: async function (input) {
                if (
                  originalImplementation.emailPasswordSignUpPOST === undefined
                ) {
                  throw Error("Should never come here");
                }

                // First we call the original implementation
                let response =
                  await originalImplementation.emailPasswordSignUpPOST(input);

                // If sign up was successful
                if (response.status === "OK") {
                  // We can get the form fields from the input like this
                  let formFields = input.formFields;
                  let user = response.user;

                  console.log(formFields);

                  const fieldUsername = formFields.find(
                    (field) => field.id === "username"
                  );

                  input.userContext.username = fieldUsername?.value;
                }

                return response;
              },
            };
          },
        },
      }),
      SessionNode.init({
        override: {
          functions: (originalImplementation) => {
            return {
              ...originalImplementation,
              createNewSession: async function (input) {
                let userId = input.userId;

                console.log(input.userContext);

                const username = "todo";

                // This goes in the access token, and is availble to read on the frontend.
                input.accessTokenPayload = {
                  ...input.accessTokenPayload,
                  username: username,
                };

                // This is stored in the db against the sessionHandle for this session
                input.sessionData = {
                  ...input.sessionData,
                  username: username,
                };

                const session = originalImplementation.createNewSession(input);
                console.log(`user ${userId} created a new session`);

                return session;
              },
            };
          },
        },
      }),
    ],
    isInServerlessEnv: true,
  };
};
Context in ``emailPasswordSignUp`` still seems to be undefined.
Copy code
usernameFromContext undefined
{}
user 92c11c44-308d-4ef9-b504-35f19d43a1ff created a new session
[
  { id: 'email', value: 'manicraft27@gmail.com' },
  { id: 'password', value: 'sml12345' },
  { id: 'username', value: 'Mani27' }
]
I also can't find an example in the documentation, even tho this is a common customisation.
r
You need to modify input.userContext before calling the originalImplementation
The link I had sent above has an example of how to use the context object. Just with a different use case
u
ok thanks, I'll try it 👌
Worked, thank you
6 Views