Post signin / signup callbacks
#
1) On the frontendThis method allows you to fire events immediately after a successful sign in. For example to send analytics events post sign in.
- ReactJS
- Angular
- Vue
- Plain JavaScript
- React Native
Note
You can use the
You can refer to this example app as a reference for using the
supertokens-web-js
SDK which exposes several helper functions that query the APIs exposed by the SuperTokens backend SDK.You can refer to this example app as a reference for using the
supertokens-web-js
SDK.Note
To use SuperTokens with React Native you need to use the
To add login functionality, you need to build your own UI and call the APIs exposed by the backend SDKs. You can find the API spec here
supertokens-react-native
SDK. The SDK provides session management features.To add login functionality, you need to build your own UI and call the APIs exposed by the backend SDKs. You can find the API spec here
What type of UI are you using?
Prebuilt UICustom UI
What type of UI are you using?
Prebuilt UICustom UI
import SuperTokens from "supertokens-auth-react";import Passwordless from "supertokens-auth-react/recipe/passwordless";import Session from "supertokens-auth-react/recipe/session";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, recipeList: [ Passwordless.init({ contactMethod: "EMAIL_OR_PHONE",
onHandleEvent: async (context) => { if (context.action === "SESSION_ALREADY_EXISTS") { // TODO: } else if (context.action === "PASSWORDLESS_RESTART_FLOW") { // TODO: } else if (context.action === "PASSWORDLESS_CODE_SENT") { // TODO: } else { let {id, email, phoneNumber} = context.user; if (context.action === "SUCCESS") { if (context.isNewUser) { // TODO: Sign up } else { // TODO: Sign in } } } } }), Session.init() ]});
info
Please refer to this page to learn more about the onHandleEvent
hook.
#
2) On the backendFor this, you'll have to override the consumeCodePOST
API in the init
function call.
- NodeJS
- GoLang
- Python
import SuperTokens from "supertokens-node";import Passwordless from "supertokens-node/recipe/passwordless";import Session from "supertokens-node/recipe/session";
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 flowType
override: { apis: (originalImplementation) => { return { ...originalImplementation, consumeCodePOST: async (input) => {
if (originalImplementation.consumeCodePOST === undefined) { throw Error("Should never come here"); } // First we call the original implementation of consumeCodePOST. let response = await originalImplementation.consumeCodePOST(input);
// Post sign up response, we check if it was successful if (response.status === "OK") { let { id, email, phoneNumber } = response.user;
if (response.createdNewUser) { // TODO: post sign up logic } else { // TODO: post sign in logic } } return response; } } } } }), Session.init({ /* ... */ }) ]});
import ( "fmt"
"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 {
// create a copy of the original function originalConsumeCodePOST := *originalImplementation.ConsumeCodePOST
// override the sign in up API (*originalImplementation.ConsumeCodePOST) = func(userInput *plessmodels.UserInputCodeWithDeviceID, linkCode *string, preAuthSessionID string, options plessmodels.APIOptions, userContext supertokens.UserContext) (plessmodels.ConsumeCodePOSTResponse, error) {
// First we call the original implementation of ConsumeCodeUpPOST. response, err := originalConsumeCodePOST(userInput, linkCode, preAuthSessionID, options, userContext) if err != nil { return plessmodels.ConsumeCodePOSTResponse{}, err }
if response.OK != nil { // sign in was successful
// user object contains the ID and email or phone number user := response.OK.User fmt.Println(user)
if response.OK.CreatedNewUser { // TODO: Post sign up logic } else { // TODO: Post sign in logic }
} return response, nil }
return originalImplementation }, }, }), }, })}
from supertokens_python import init, InputAppInfofrom supertokens_python.recipe import passwordlessfrom supertokens_python.recipe.passwordless.interfaces import APIInterface, APIOptions, ConsumeCodePostOkResultfrom typing import Union, Dict, Any
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], api_options: APIOptions, user_context: Dict[str, Any]): # First we call the original implementation of consume_code_post. response = await original_consume_code_post(pre_auth_session_id, user_input_code, device_id, link_code, api_options, user_context)
# Post sign up response, we check if it was successful if isinstance(response, ConsumeCodePostOkResult): _ = response.user.user_id __ = response.user.email ___ = response.user.phone_number
if response.created_new_user: pass # TODO: Post sign up logic else: pass # TODO: Post sign in logic
return response 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 ), ) ])
Using the code above, if createdNewUser
is true
, you can (for example):
- Add the user's ID and their info to your own database (in addition to it being stored in SuperTokens).
- Send analytics events about a sign up.
- Send a welcome email to the user.