Skip to main content

If you are using our backend SDK that is lesser than the following versions, please visit the older documentation link here.

Which UI do you use?
Custom UI
Pre built UI

Pre API Hook

This function is called before any API call is being made to your backend from our frontend SDK. You can use this to change the request body / url / header or any other request property.

import Passwordless from "supertokens-auth-react/recipe/passwordless";
import ThirdParty from "supertokens-auth-react/recipe/thirdparty";

Passwordless.init({
contactMethod: "EMAIL", // This example will work with any contactMethod.
preAPIHook: async (context) => {
let url = context.url;

// is the fetch config object that contains the header, body etc..
let requestInit = context.requestInit;

let action = context.action;
if (action === "EMAIL_EXISTS") {

} else if (action === "PASSWORDLESS_CONSUME_CODE") {

} else if (action === "PASSWORDLESS_CREATE_CODE") {

} else if (action === "PASSWORDLESS_RESEND_CODE") {

} else if (action === "PHONE_NUMBER_EXISTS") {

}

// events such as sign out are in the
// session recipe pre API hook (See the info box below)
return {
requestInit, url
};
}
})

ThirdParty.init({
preAPIHook: async (context) => {
let url = context.url;
let requestInit = context.requestInit;

let action = context.action;
if (action === "GET_AUTHORISATION_URL") {

} else if (action === "THIRD_PARTY_SIGN_IN_UP") {
// Note: this could either be sign in or sign up.
// we don't know that at the time of the API call
// since all we have is the authorisation code from
// the social provider
}

// events such as sign out are in the
// session recipe pre API hook (See the info box below)

return {
requestInit, url
};

}
})

Reading custom request information in the backend#

Visit this page to learn how to read custom headers etc from the request on the backend.

Looking for older versions of the documentation?
Which UI do you use?
Custom UI
Pre built UI