How to send a custom response
important
This feature is only applicable for users who build their own frontend since our frontend requires on a specific output from the APIs as defined here.
Let's take an example of sending a custom response for the /auth/signup/email/exists GET
API (does email exist).
We need to first override the function for that API (emailExistsGET
) and then use the response object in the input
param to send a custom response.
The function signature expects an return type that has a certain shape, therefore, we must still return a valid response object from the function, but that will be ignored since you have already sent a response to the client.
- NodeJS
- GoLang
- Python
import EmailPassword from "supertokens-node/recipe/emailpassword";
EmailPassword.init({ override: { apis: (originalImplementation) => { return { ...originalImplementation, emailExistsGET: async function (input) {
// we can send a custom response like this: input.options.res.setStatusCode(200); // or any other status code input.options.res.sendJSONResponse({ message: "my custom response", //... })
// this return doesn't matter. But we must do it // cause the function signature expects a response. return { status: "OK", exists: false }; } } } }})
import ( "encoding/json"
"github.com/supertokens/supertokens-golang/recipe/emailpassword" "github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels" "github.com/supertokens/supertokens-golang/supertokens")
func main() { emailpassword.Init(&epmodels.TypeInput{ Override: &epmodels.OverrideStruct{ APIs: func(originalImplementation epmodels.APIInterface) epmodels.APIInterface {
(*originalImplementation.EmailExistsGET) = func(email string, options epmodels.APIOptions, userContext supertokens.UserContext) (epmodels.EmailExistsGETResponse, error) {
// we create our custom response. options.Res.Header().Set("Content-Type", "application/json; charset=utf-8") options.Res.WriteHeader(200) responseJson := map[string]interface{}{ "message": "My custom response", // ... }
bytes, _ := json.Marshal(responseJson) options.Res.Write(bytes)
// this return doesn't matter. But we must do it // cause the function signature expects a response. return epmodels.EmailExistsGETResponse{ OK: &struct{ Exists bool }{ Exists: false, }, }, nil }
return originalImplementation }, }, })}
from supertokens_python.recipe.emailpassword.interfaces import APIOptions, EmailExistsGetOkResult, APIInterfacefrom supertokens_python.recipe import emailpasswordfrom typing import Dict, Any
def override_emailpassword_apis(original_implementation: APIInterface):
async def email_exists_get(email: str, api_options: APIOptions, user_context: Dict[str, Any]):
# send custom response like this api_options.response.set_status_code(200) json_dict = {'message': 'Custom response'} api_options.response.set_json_content(json_dict)
# this return doesn't matter. But we must do it # cause the function signature expects a response. return EmailExistsGetOkResult(False)
original_implementation.email_exists_get = email_exists_get return original_implementation
emailpassword.init(override=emailpassword.InputOverrideConfig(apis=override_emailpassword_apis))