ข้ามไปยังเนื้อหาหลัก

Pre-register

The Pre-register Action decides whether a new user may register. It runs right before Logto creates the user, so a denied registration leaves no user behind. A typical use is checking the email address with an external validation service, such as ZeroBounce, before the account exists.

บันทึก:

In self-hosted Logto OSS, the Pre-register Action requires v1.47.0 or later.

When it runs​

The Action runs during Register interactions in the Experience API, after Logto's own checks have passed: the registration setting, the email allowlist and blocklist, CAPTCHA, and the required profile fields. If the user still has to fill in a required field, the Action runs only after they complete it, so the event always carries the final profile.

It covers every way a user can register through the sign-in experience:

  • Email or phone verification code
  • Username and password
  • First sign-in with a social connector
  • First sign-in with enterprise SSO
  • Magic link (one-time token)

It does not run when:

  • An existing user signs in.
  • A Post first-factor verification Action returns createUser during a password sign-in.
  • An administrator creates a user in Console or through the Management API.
  • A user registers through the deprecated Interaction API (/api/interaction).

Event payload​

The event has this shape:

type PreRegisterEvent = {
key: 'action.preRegister';
interactionEvent: 'Register';
verificationTypes: string[];
user: PreRegisterUser;
applicationId?: string;
request: {
ip?: string;
userAgent?: string;
};
};

type PreRegisterUser = {
username?: string | null;
primaryEmail?: string | null;
primaryPhone?: string | null;
name?: string | null;
avatar?: string | null;
profile?: Record<string, unknown>;
customData?: Record<string, unknown>;
// Present when the user registers with a social connector.
socialIdentity?: {
target: string;
userInfo: {
id: string;
email?: string;
phone?: string;
name?: string;
avatar?: string;
};
};
// Present when the user registers with enterprise SSO.
enterpriseSsoIdentity?: {
ssoConnectorId: string;
issuer: string;
identityId: string;
};
};
FieldDescription
verificationTypesThe verifications the user completed in this interaction, such as EmailVerificationCode, PhoneVerificationCode, NewPasswordIdentity, Social, EnterpriseSso, or OneTimeToken.
userThe profile Logto is about to create. It has no id because the user does not exist yet.
applicationIdThe application the user is registering through, when known.
requestThe IP address and user agent of the registration request.

The event never contains the user's password or its hash. Social and enterprise SSO identities leave out the raw data returned by the identity provider.

บันทึก:

The event contains personal data of someone who is not yet your user, such as their email address and IP address. If your script sends it to an external service, make sure your privacy policy covers that processing.

Result​

Return one of these results:

type PreRegisterResult =
| { action: 'allow' }
| {
action: 'deny';
// Plain text shown to the end user, up to 256 characters.
message?: string;
};
Script resultWhat Logto does
undefined, null, {}, or { action: 'allow' }Creates the user and continues the registration.
{ action: 'deny', message? }Stops the registration with action.registration_denied. No user is created.
Any other valueStops the registration with action.general, even under the allow error policy.

What the user sees when you deny​

Logto's sign-in experience shows the message in a dialog. Without a message, it shows a localized default, "Your registration was declined." Write the message for the end user, and do not include internal details such as the validation provider's response.

If you build your own sign-in UI on the Experience API, the POST /api/experience/identification request that would create the user fails with HTTP 422:

{
"code": "action.registration_denied",
"message": "Your registration was declined.",
"data": {
"message": "Please sign up with a valid email address."
}
}

message is the localized default, and data.message is the message returned by the script, when there is one.

Email validation example​

This example checks the email address with the ZeroBounce validation API and denies addresses it reports as undeliverable or risky. Store the API key in the Action's environment variables as ZEROBOUNCE_API_KEY.

const runAction = async ({ event, environmentVariables = {} }) => {
const email = event.user.primaryEmail;

// Let invited users and registrations without an email address through.
if (!email || event.verificationTypes.includes('OneTimeToken')) {
return;
}

const query = new URLSearchParams({
api_key: environmentVariables.ZEROBOUNCE_API_KEY,
email,
ip_address: event.request.ip ?? '',
});
const response = await fetch(`https://api.zerobounce.net/v2/validate?${query}`);

if (!response.ok) {
throw new Error(`ZeroBounce returned ${response.status}`);
}

const result = await response.json();

if (result.error) {
throw new Error('ZeroBounce validation failed');
}

if (['invalid', 'spamtrap', 'abuse', 'do_not_mail'].includes(result.status)) {
return { action: 'deny', message: 'Please sign up with a valid email address.' };
}
};

Statuses such as catch-all and unknown are allowed here, because ZeroBounce cannot confirm them either way. Adjust the list to your own tolerance.

Failure behavior​

The On script error setting decides what happens when the script throws, its request fails, or it runs out of time:

  • block (default): The registration fails with action.general, and no user is created.
  • allow: The registration continues as if the Action had allowed it.

An external validation service adds its response time to every registration, and an outage or slow response becomes a script error. Choose allow if you prefer to let registrations through while the service is unavailable, and block if no account may be created without a passing check.

Monitor executions​

Each run creates an Action.PreRegister audit log event. To show which sign-up was evaluated, it keeps the interaction event, verification types, application ID, the user's username, primaryEmail, and primaryPhone, and the decision. It does not keep the rest of the profile, the request context, or the deny message returned by the script.