Post signin / signup callbacks
#
1) On the frontendThis method allows you to fire events immediately after a successful sign in / up. For example to send analytics events post sign in / up.
- 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 ThirdParty from "supertokens-auth-react/recipe/thirdparty";
import Session from "supertokens-auth-react/recipe/session";
SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
recipeList: [
ThirdParty.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 signInUpPOST
API in the init
function call.
- NodeJS
- GoLang
- Python
import SuperTokens from "supertokens-node";
import ThirdParty from "supertokens-node/recipe/thirdparty";
import Session from "supertokens-node/recipe/session";
SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
supertokens: {
connectionURI: "...",
},
recipeList: [
ThirdParty.init({
signInAndUpFeature: {
providers: [/* ... */]
},
override: {
apis: (originalImplementation) => {
return {
...originalImplementation,
signInUpPOST: async function (input) {
if (originalImplementation.signInUpPOST === undefined) {
throw Error("Should never come here");
}
// First we call the original implementation of signInUpPOST.
let response = await originalImplementation.signInUpPOST(input);
// Post sign up response, we check if it was successful
if (response.status === "OK") {
let { id, email } = response.user;
// This is the response from the OAuth 2 provider that contains their tokens or user info.
let thirdPartyAuthCodeResponse = response.authCodeResponse;
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/thirdparty"
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
supertokens.Init(supertokens.TypeInput{
RecipeList: []supertokens.Recipe{
thirdparty.Init(&tpmodels.TypeInput{
Override: &tpmodels.OverrideStruct{
APIs: func(originalImplementation tpmodels.APIInterface) tpmodels.APIInterface {
// create a copy of the originalImplementation
originalSignInUpPOST := *originalImplementation.SignInUpPOST
// override the sign in up API
(*originalImplementation.SignInUpPOST) = func(provider tpmodels.TypeProvider, code string, authCodeResponse interface{}, redirectURI string, options tpmodels.APIOptions, userContext supertokens.UserContext) (tpmodels.SignInUpPOSTResponse, error) {
// First we call the original implementation of SignInUpPOST.
response, err := originalSignInUpPOST(provider, code, authCodeResponse, redirectURI, options, userContext)
if err != nil {
return tpmodels.SignInUpPOSTResponse{}, err
}
if response.OK != nil {
// sign in / up was successful
// user object contains the ID, email and other third party info
user := response.OK.User
fmt.Println(user)
// This is the response from the OAuth 2 provider that contains their tokens or user info.
thirdPartyAuthCodeResponse := response.OK.AuthCodeResponse
fmt.Println(thirdPartyAuthCodeResponse)
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, InputAppInfo
from supertokens_python.recipe import thirdparty
from supertokens_python.recipe.thirdparty.interfaces import APIInterface, APIOptions, SignInUpPostOkResult
from typing import Union, Dict, Any
from supertokens_python.recipe.thirdparty.provider import Provider
def override_thirdparty_apis(original_implementation: APIInterface):
original_sign_in_up_post = original_implementation.sign_in_up_post
async def sign_in_up_post(provider: Provider, code: str, redirect_uri: str, client_id: Union[str, None], auth_code_response: Union[Dict[str, Any], None], api_options: APIOptions,
user_context: Dict[str, Any]):
# First we call the original implementation of signInPOST.
response = await original_sign_in_up_post(provider, code, redirect_uri, client_id, auth_code_response, api_options, user_context)
# Post sign up response, we check if it was successful
if isinstance(response, SignInUpPostOkResult):
_ = response.user.user_id
__ = response.user.email
# This is the response from the OAuth 2 provider that contains their tokens or user info.
___ = response.auth_code_response
if response.created_new_user:
pass # TODO: Post sign up logic
else:
pass # TODO: Post sign in logic
return response
original_implementation.sign_in_up_post = sign_in_up_post
return original_implementation
init(
app_info=InputAppInfo(api_domain="...", app_name="...", website_domain="..."),
framework='...',
recipe_list=[
thirdparty.init(
override=thirdparty.InputOverrideConfig(
apis=override_thirdparty_apis
),
sign_in_and_up_feature=thirdparty.SignInAndUpFeature(providers=[])
)
]
)
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.