How to use
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
info
See all the functions that can be overrided here
import SuperTokens from "supertokens-node";
import Passwordless from "supertokens-node/recipe/passwordless";
SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
recipeList: [
Passwordless.init({
contactMethod: "EMAIL", // This example will work with any contactMethod
flowType: "USER_INPUT_CODE_AND_MAGIC_LINK", // This example will work with any flow.
override: {
apis: (originalImplementation) => {
return {
...originalImplementation,
// here we only override the API that is called when a user
// clicks on a magic link or enters an OTP
consumeCodePOST: async function (input) {
if (originalImplementation.consumeCodePOST === undefined) {
throw Error("Should never come here")
}
// TODO: some custom logic
// or call the default behaviour as show below
return await originalImplementation.consumeCodePOST(input);
},
// ...
// TODO: override more apis
};
},
},
})
]
});
originalImplementation
is an object that contains apis that have the original implementation for this 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
consumeCodePOST
api of this recipe. This api will be called when the user clicks on a magic link or enters an OTP.
info
See all the functions that can be overrided here
import (
"github.com/supertokens/supertokens-golang/recipe/passwordless"
"github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
supertokens.Init(supertokens.TypeInput{
RecipeList: []supertokens.Recipe{
passwordless.Init(plessmodels.TypeInput{
Override: &plessmodels.OverrideStruct{
APIs: func(originalImplementation plessmodels.APIInterface) plessmodels.APIInterface {
//First we copy the original impl function
originalConsumeCodePOST := *originalImplementation.ConsumeCodePOST
// Then we override the functions we want to
(*originalImplementation.ConsumeCodePOST) = func(userInput *plessmodels.UserInputCodeWithDeviceID, linkCode *string, preAuthSessionID string, tenantId string, options plessmodels.APIOptions, userContext supertokens.UserContext) (plessmodels.ConsumeCodePOSTResponse, error) {
// TODO: some custom logic
// or call the default behaviour as show below
return originalConsumeCodePOST(userInput, linkCode, preAuthSessionID, tenantId, options, userContext)
}
// TODO: Override more APIs
return originalImplementation
},
},
}),
},
})
}
originalImplementation
is an object that contains apis that have the original implementation for this 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
ConsumeCodePOST
api of this recipe. This api will be called when the user clicks on a magic link or enters an OTP.
info
See all the functions that can be overrided here
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import passwordless
from supertokens_python.recipe.passwordless.interfaces import APIInterface, APIOptions
from typing import Union, Dict, Any, Optional
from supertokens_python.recipe.session.interfaces import SessionContainer
def override_passwordless_apis(original_implementation: APIInterface):
original_consume_code_post = original_implementation.consume_code_post
async def consume_code_post(
pre_auth_session_id: str,
user_input_code: Union[str, None],
device_id: Union[str, None],
link_code: Union[str, None],
session: Optional[SessionContainer],
should_try_linking_with_session_user: Union[bool, None],
tenant_id: str,
api_options: APIOptions,
user_context: Dict[str, Any],
):
# TODO: some custom logic
# or call the default behaviour as show below
return await original_consume_code_post(
pre_auth_session_id,
user_input_code,
device_id,
link_code,
session,
should_try_linking_with_session_user,
tenant_id,
api_options,
user_context,
)
original_implementation.consume_code_post = consume_code_post
return original_implementation
init(
app_info=InputAppInfo(api_domain="...", app_name="...", website_domain="..."),
framework="...",
recipe_list=[
passwordless.init(
contact_config=...,
flow_type="...",
override=passwordless.InputOverrideConfig(apis=override_passwordless_apis),
)
],
)
original_implementation
is an object that contains apis that have the original implementation for this 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
consume_code_post
api of this recipe. This api will be called when the user clicks on a magic link or enters an OTP.