Skip to main content

Inserting Users into Supabase when they sign up

In our example app a user can login via magic link. We will need the API such that when a user signs up, their email mapped to their userId is stored in Supabase.

Step 1: Overriding the Passwordless signup function#

// config/backendConfig.ts
import Passwordless from "supertokens-node/recipe/passwordless";import SessionNode from "supertokens-node/recipe/session";import { TypeInput, AppInfo } from "supertokens-node/types";import jwt from "jsonwebtoken";
let appInfo: AppInfo = {    appName: "TODO: add your app name",    apiDomain: "TODO: add your website domain",    websiteDomain: "TODO: add your website domain"}
// take a look at the Creating Supabase Client section to see how to define getSupabaselet getSupabase: any;
let backendConfig = (): TypeInput => {    return {        framework: "express",        supertokens: {            connectionURI: "https://try.supertokens.com",        },        appInfo,        recipeList: [            Passwordless.init({                flowType: "MAGIC_LINK",                contactMethod: "EMAIL",                override: {                    apis: (originalImplementation) => {                        return {                            ...originalImplementation,                            // the consumeCodePOST function gets called when a user clicks a magic link                            consumeCodePOST: async function (input) {                                if (originalImplementation.consumeCodePOST === undefined) {                                    throw Error("Should never come here");                                }
                                let response = await originalImplementation.consumeCodePOST(input);
                                if (response.status === "OK" && response.createdNewUser) {                                                                        // retrieve the accessTokenPayload from the user's session                                    const accessTokenPayload = response.session.getAccessTokenPayload();
                                    // create a supabase client with the supabase_token from the accessTokenPayload                                    const supabase = getSupabase(accessTokenPayload.supabase_token);
                                    // store the user's email mapped to their userId in Supabase                                    const { error } = await supabase                                        .from("users")                                        .insert({ email: response.user.email, user_id: response.user.id });
                                    if (error !== null) {                                        throw error;                                    }                                }
                                return response;                            },                        }                    }                }            }),            SessionNode.init({/*...*/}),        ],        isInServerlessEnv: true,    };};

We will be changing the Passwordless flow by overriding the consumeCodePOST api. When a user signs up we will retrieve the supabase_token from the user's accessTokenPayload(this was added in the previous step where we changed the createNewSession function) and use it to query Supabase to insert the new user's information. // take a look at the Create Supabase Client section to see how to define getSupabase

Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react