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 Session from "supertokens-node/recipe/session";
SuperTokens.init({    appInfo: {        apiDomain: "...",        appName: "...",        websiteDomain: "..."    },    supertokens: {        connectionURI: "...",    },    recipeList: [        Session.init({            override: {                apis: (originalImplementation) => {                    return {                        ...originalImplementation,
                        // here we are only overriding the function that signs out a user                        signOutPOST: async function (input) {
                            if (originalImplementation.signOutPOST === undefined) {                                throw Error("Should never come here")                            }                            // TODO: some custom logic
                            // or call the default behaviour as show below                            return await originalImplementation.signOutPOST(input);                        },                        // ...                        // TODO: override more apis                    }                }            }        })    ]});
  • originalImplementation is the object that contains apis that have the original implementation for this recipe. It can be used in your apis as a way to use the SuperTokens' default behaviour.
  • In the above code snippet, we override the signOutPOST api of this 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 out 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 signOutPOST 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 signOutPOST code we copied earlier and modify it.

import SuperTokens from "supertokens-node";import Session from "supertokens-node/recipe/session";
SuperTokens.init({    appInfo: {        apiDomain: "...",        appName: "...",        websiteDomain: "..."    },    supertokens: {        connectionURI: "...",    },    recipeList: [        Session.init({            override: {                apis: (originalImplementation) => {                    return {                        ...originalImplementation,
                        signOutPOST: async function ({ options, userContext }) {
                            let session;                            try {                                session = await options.recipeImplementation.getSession({ req: options.req, res: options.res, userContext });                            } catch (err) {                                if (Session.Error.isErrorFromSuperTokens(err) && err.type === Session.Error.UNAUTHORISED) {                                    // The session is expired / does not exist anyway. So we return OK                                    return {                                        status: "OK",                                    };                                }                                throw err;                            }
                            if (session === undefined) {                                throw new Error("Session is undefined. Should not come here.");                            }
                            await session.revokeSession();
                            return {                                status: "OK",                            };                        },                    }                }            }        })    ]});

You can now freely modify any step in the API.