Skip to main content

How to use

Method 1: Pre / Post API logic change:#

If you would like to change something pre or post our API logic, then use this method.

SuperTokens.init({    appInfo: {...},    supertokens: {...},    recipeList: [        EmailPassword.init({            override: {                apis: (originalImplementation) => {                    return {                        ...originalImplementation,
                        // here we only override the sign up API logic                         signUpPOST: async function(input) {
                            if (originalImplementation.signUpPOST === undefined){                                throw Error("Should never come here")                            }                            // TODO: some custom logic
                            // or call the default behaviour as show below                            return await originalImplementation.signUpPOST(input);                        },                        // ...                        // TODO: override more apis                    }                },                emailVerificationFeature: {                    apis: (originalImplementationEmailVerification) => {                        return {                            ...originalImplementationEmailVerification,                            verifyEmailPOST: async function(input) {                                                                if (originalImplementationEmailVerification.verifyEmailPOST === undefined){                                    throw Error("Should never come here")                                }                                // TODO: some custom logic
                                // or call the default behaviour as show below                                return await originalImplementationEmailVerification.verifyEmailPOST(input);                            },                            // ...                            // TODO: override more apis                        }                    }                }            }        })    ]});
  • originalImplementation and originalImplementationEmailVerification are objects that contain apis that have the original implementation for this and the email verification recipe. They can be used in your custom apis as a way to use the SuperTokens' default behaviour.
  • In the above code snippet, we override the signUpPOST api of this recipe. This api will be called when the user clicks the sign up button on the frontend.
  • Likewise, we override the verifyEmailPOST api from the email verification recipe.

Method 2: Change API logic by copy / paste#

If you need to change our implementation of the API logic you should use this method.

In this example we will be overriding the sign in API.

Step 1: Copy the API code from the SDK.#

You first need to find the implementation for the API you would like to change in the backend SDK.

  • All the APIs you can override can be found here, copy the name of the function you would like to override.
  • You can find the API code for this recipe here.
  • In this example we want to override the signInPOST API, so we can search for a function with that name in the page mentioned above and copy the code.

Step 2: Add the API code to the override config.#

Now in the override config instead of calling the original implementation like we did in Method 1 we can paste the signInPOST code we copied earlier and modify it.

SuperTokens.init({    appInfo: {...},    supertokens: {...},    recipeList: [        EmailPassword.init({            override: {                apis: (originalImplementation) => {                    return {                        ...originalImplementation,
                        signInPOST: async function({formFields, options}) {
                            let email = formFields.filter((f) => f.id === "email")[0].value;                            let password = formFields.filter((f) => f.id === "password")[0].value;
                            let response = await options.recipeImplementation.signIn({ email, password });                            if (response.status === "WRONG_CREDENTIALS_ERROR") {                                return response;                            }                            let user = response.user;
                            await Session.createNewSession(options.res, user.id, {}, {});                            return {                                status: "OK",                                user,                            };                        },                                            }                },            }        })    ]});

You can now freely modify any step in the API. For example, if you want to modify the API so that a new session is created only if a user is not logged into another device, you can add the logic for that right before calling the Session.createNewSession function.