You were correct, my action was not catching the axios error response correctly. This is the updated code:
const axios = require("axios")
/**
* Handler that will be called during the execution of a PreUserRegistration flow.
*
* @param {Event} event - Details about the context and user that is attempting to register.
* @param {PreUserRegistrationAPI} api - Interface whose methods can be used to change the behavior of the signup.
*/
exports.onExecutePreUserRegistration = async (event, api) => {
const newBaseUser = {
email: event.user.email,
name: event.user.username,
picture_uri: event.user.picture
};
try {
await axios.post(`${event.secrets.ELVIE_API_URL}/api/users/create-user`, newBaseUser, {
headers: {
"Content-Type": "application/json",
"X-API-Key": event.secrets.ELVIE_API_KEY
}
});
} catch (error) {
if (error.response.status === 403) {
api.access.deny("forbidden_api_access", "Forbidden access to the Elvie API. Please check if the API Key header is being sent.");
}
else if (error.response.status === 401) {
api.access.deny("unauthorized_api_access", "Unauthorized access to the Elvie API. Please check if the API Key header is valid.");
}
else if (error.response.status === 400) {
api.access.deny("user_already_exists", "An user with the specified email already exists");
}
else if (error.response.status !== 200) {
api.access.deny("api_internal_error", "Elvie API internal error");
}
}
};
Also, I disabled this setting to have a full support for readable error messages during signup:
Tenant Settings → Advanced → Use a generic response in public signup API error message