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.

import SuperTokens from "supertokens-node";import ThirdParty from "supertokens-node/recipe/thirdparty";
SuperTokens.init({    appInfo: {        apiDomain: "...",        appName: "...",        websiteDomain: "..."    },    supertokens: {        connectionURI: "...",    },    recipeList: [        ThirdParty.init({            signInAndUpFeature: {                providers: [/* ... */]            },            override: {                apis: (originalImplementation) => {                    return {                        ...originalImplementation,
                        // here we only override the sign in / up API logic                         signInUpPOST: async function (input) {
                            if (originalImplementation.signInUpPOST === undefined) {                                throw Error('Should never come here')                            }
                            // TODO: some custom logic
                            // or call the default behaviour as show below                            return await originalImplementation.signInUpPOST(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 signInUpPOST api of this recipe. This api will be used to handle the signInUp API route when a user either signs up or signs in.
  • 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 and up 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 signInUpPOST 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 in the override config.#

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

import axios from "axios";import qs from "qs";import SuperTokens from "supertokens-node";import ThirdParty from "supertokens-node/recipe/thirdparty";import Session from "supertokens-node/recipe/session";
SuperTokens.init({    appInfo: {        apiDomain: "...",        appName: "...",        websiteDomain: "..."    },    supertokens: {        connectionURI: "...",    },    recipeList: [        ThirdParty.init({            signInAndUpFeature: {                providers: [/* ... */]            },            override: {                apis: (originalImplementation) => {                    return {                        ...originalImplementation,
                        signInUpPOST: async function ({ provider, code, redirectURI, options, userContext }) {
                            let userInfo;                            let accessTokenAPIResponse: any;                            let providerInfo = await provider.get(redirectURI, code, userContext);
                            accessTokenAPIResponse = await axios({                                method: "post",                                url: providerInfo.accessTokenAPI.url,                                data: qs.stringify(providerInfo.accessTokenAPI.params),                                headers: {                                    "content-type": "application/x-www-form-urlencoded",                                    accept: "application/json", // few providers like github don't send back json response by default                                },                            });                            userInfo = await providerInfo.getProfileInfo(accessTokenAPIResponse.data, userContext);
                            let emailInfo = userInfo.email;                            if (emailInfo === undefined) {                                return {                                    status: "NO_EMAIL_GIVEN_BY_PROVIDER",                                };                            }                            let response = await options.recipeImplementation.signInUp({                                thirdPartyId: provider.id,                                thirdPartyUserId: userInfo.id,                                email: emailInfo,                                userContext                            });
                            // we set the email as verified if already verified by the OAuth provider.                            // This block was added because of https://github.com/supertokens/supertokens-core/issues/295                            if (emailInfo.isVerified) {                                const tokenResponse = await options.emailVerificationRecipeImplementation.createEmailVerificationToken({                                    userId: response.user.id,                                    email: response.user.email,                                    userContext                                });
                                if (tokenResponse.status === "OK") {                                    await options.emailVerificationRecipeImplementation.verifyEmailUsingToken({                                        token: tokenResponse.token,                                        userContext                                    });                                }                            }
                            let session = await Session.createNewSession(options.res, response.user.id, {}, {});                            return {                                status: "OK",                                createdNewUser: response.createdNewUser,                                user: response.user,                                authCodeResponse: accessTokenAPIResponse.data,                                session                            };                        },
                    }                },            }        })    ]});

You can now freely modify any step in the API.