Post sign in 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 EmailPassword from "supertokens-auth-react/recipe/emailpassword";import Session from "supertokens-auth-react/recipe/session";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, recipeList: [ EmailPassword.init({ onHandleEvent: async (context) => { if (context.action === "SESSION_ALREADY_EXISTS") { // TODO: } else { 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 signInPOST
API in the init
function call.
- NodeJS
- GoLang
- Python
import SuperTokens from "supertokens-node";import EmailPassword from "supertokens-node/recipe/emailpassword";import Session from "supertokens-node/recipe/session";
SuperTokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, supertokens: { connectionURI: "...", }, recipeList: [ EmailPassword.init({ override: { apis: (originalImplementation) => { return { ...originalImplementation, signInPOST: async function (input) {
if (originalImplementation.signInPOST === undefined) { throw Error("Should never come here"); }
// First we call the original implementation of signInPOST. let response = await originalImplementation.signInPOST(input);
// Post sign up response, we check if it was successful if (response.status === "OK") { let { id, email } = response.user;
// These are the input form fields values that the user used while signing in let formFields = input.formFields // TODO: post sign in logic } return response; } } } } }), Session.init({ /* ... */ }) ]});
import ( "fmt"
"github.com/supertokens/supertokens-golang/recipe/emailpassword" "github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels" "github.com/supertokens/supertokens-golang/supertokens")
func main() { supertokens.Init(supertokens.TypeInput{ RecipeList: []supertokens.Recipe{ emailpassword.Init(&epmodels.TypeInput{ Override: &epmodels.OverrideStruct{ APIs: func(originalImplementation epmodels.APIInterface) epmodels.APIInterface { // create a copy of the originalImplementation func originalSignInPOST := *originalImplementation.SignInPOST
// override the sign in up API (*originalImplementation.SignInPOST) = func(formFields []epmodels.TypeFormField, options epmodels.APIOptions, userContext supertokens.UserContext) (epmodels.SignInPOSTResponse, error) {
// First we call the original implementation of SignInPOST. response, err := originalSignInPOST(formFields, options, userContext) if err != nil { return epmodels.SignInPOSTResponse{}, err }
if response.OK != nil { // sign in was successful
// user object contains the ID and email user := response.OK.User
// TODO: Post sign in logic. // You can even read the formFields from the input // params to get the user filled fields. fmt.Println(user)
} return response, nil }
return originalImplementation }, }, }), }, })}
from supertokens_python import init, InputAppInfofrom supertokens_python.recipe import emailpasswordfrom supertokens_python.recipe.emailpassword.interfaces import APIInterface, APIOptions, SignInPostOkResultfrom supertokens_python.recipe.emailpassword.types import FormFieldfrom typing import List, Dict, Any
def override_email_password_apis(original_implementation: APIInterface): original_sign_in_post = original_implementation.sign_in_post
async def sign_in_post(form_fields: List[FormField], api_options: APIOptions, user_context: Dict[str, Any]): # First we call the original implementation of signInPOST. response = await original_sign_in_post(form_fields, api_options, user_context) # Post sign in response, we check if it was successful if isinstance(response, SignInPostOkResult): _ = response.user.user_id __ = response.user.email # TODO: post sign in logic pass return response original_implementation.sign_in_post = sign_in_post return original_implementation
init( app_info=InputAppInfo(api_domain="...", app_name="...", website_domain="..."), framework='...', recipe_list=[ emailpassword.init( override=emailpassword.InputOverrideConfig( apis=override_email_password_apis ) ) ])