Skip to main content

Post signin / signup callbacks

1) On the frontend#

This method allows you to fire events immediately after a successful sign in / up. For example to send analytics events post sign in / up.

import SuperTokens from "supertokens-auth-react";
import ThirdPartyEmailPassword from "supertokens-auth-react/recipe/thirdpartyemailpassword";
import Session from "supertokens-auth-react/recipe/session";

SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
recipeList: [
ThirdPartyEmailPassword.init({
onHandleEvent: async (context) => {
if (context.action === "SESSION_ALREADY_EXISTS") {
// TODO:
} else if (context.action === "SUCCESS") {
let { id, email } = context.user;
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 backend#

For this, you'll have to override the following APIs in the init function call, on the backend:

  • emailPasswordSignUpPOST: Sign up with email & password
  • emailPasswordSignInPOST: Sign in with email & password
  • thirdPartySignInUpPOST: Sign in or up with third party
import SuperTokens from "supertokens-node";
import ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";
import Session from "supertokens-node/recipe/session";

SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
supertokens: {
connectionURI: "...",
},
recipeList: [
ThirdPartyEmailPassword.init({
override: {
apis: (originalImplementation) => {
return {
...originalImplementation,

// override the email password sign up API
emailPasswordSignUpPOST: async function(input) {
if (originalImplementation.emailPasswordSignUpPOST === undefined) {
throw Error("Should never come here");
}

// TODO: some pre sign up logic

let response = await originalImplementation.emailPasswordSignUpPOST(input);

if (response.status === "OK") {
// TODO: some post sign up logic
}

return response;
},

// override the email password sign in API
emailPasswordSignInPOST: async function(input) {
if (originalImplementation.emailPasswordSignInPOST === undefined) {
throw Error("Should never come here");
}

// TODO: some pre sign in logic

let response = await originalImplementation.emailPasswordSignInPOST(input);

if (response.status === "OK") {
// TODO: some post sign in logic
}

return response;
},

// override the thirdparty sign in / up API
thirdPartySignInUpPOST: async function(input) {
if (originalImplementation.thirdPartySignInUpPOST === undefined) {
throw Error("Should never come here");
}

// TODO: Some pre sign in / up logic

let response = await originalImplementation.thirdPartySignInUpPOST(input);

if (response.status === "OK") {
if (response.createdNewUser) {
// TODO: some post sign up logic
} else {
// TODO: some post sign in logic
}
}

return response;
}
}
}
}
}),
Session.init({ /* ... */ })
]
});

Using the code above, if createdNewUser is true or in emailPasswordSignUpPOST, 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.
Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react