# Available JQuery Events

Some portal functions (such as 2FA registration) are performed by Javascript.

We offer a few custom events that let you react to certain high-level Javascript events

## Preventing default behavior

Added in version 2.17.

Event handlers can stop the default LemonLDAP::NG behavior from executing:

    $(document).on( "sslFailure", { }, function( event ) {
                event.preventDefault();
                // Navigate the user to the smartcard help page, etc.
    });

## Second factor management

### mfaAdded

Added in version 2.0.15.

This event is triggered when a TOTP or WebAuthn device is registered

Sample code:

    $(document).on( "mfaAdded", { }, function( event, info ) {
                console.log( "Added MFA of type" + info.type );
                // Your code here

                // If you want to prevent the redirection to the 2FA register page
                // event.preventDefault();
    });

Changed in version 2.0.16: LemonLDAP::NG redirects to the 2FA Register page after your device has been added. You can cancel this redirection by calling `event.preventDefault()` in your listener.

### mfaDeleted

Added in version 2.0.15.

This event is triggered when a TOTP or WebAuthn device is removed

Sample code:

    $(document).on( "mfaDeleted", { }, function( event, info ) {
                console.log( "Removed MFA of type" + info.type );
                // Your code here
    });

### mfaUpdated

Added in version 2.23.0.

This event is triggered when a TOTP or WebAuthn device name is updated

Sample code:

    $(document).on( "mfaUpdated", { }, function( event, info ) {
                console.log( "Updated MFA of type" + info.type + " with a new label " + info.label);
                // Your code here
    });

## UI

### portalLoaded

Added in version 2.0.16.

This even is triggered after the main portal javascript has run

Sample code:

    $(document).on( "portalLoaded", { }, function( event, info ) {
            // Make sure DOM is ready as well
            $( document ).ready(function() {
                    console.log("Portal loaded and DOM ready");
            });
    });

## Authentication

### kerberosAttempt

Added in version 2.17.

This even is triggered immediately before the user tries to authenticate using Kerberos

    $(document).on( "kerberosAttempt", { }, function( event ) {
                console.log( "Kerberos attempt" );
    });

### kerberosFailure

Added in version 2.17.

This even is triggered when the user failed to authenticate using Kerberos

    $(document).on( "kerberosFailure", { }, function( event , info ) {
                console.log( "Kerberos failure", info );
    });

### kerberosSuccess

Added in version 2.17.

This even is triggered when before the user successfully authenticated using Kerberos

    $(document).on( "kerberosSuccess", { }, function( event , info ) {
                console.log( "Kerberos success", info );
    });

### sslAttempt

Added in version 2.17.

This even is triggered immediately before the user tries to authenticate using SSL

    $(document).on( "sslAttempt", { }, function( event ) {
                console.log( "SSL attempt" );
    });

### sslFailure

Added in version 2.17.

This even is triggered when the user failed to authenticate using SSL

    $(document).on( "sslFailure", { }, function( event , info ) {
                console.log( "SSL failure", info );
    });

### sslSuccess

Added in version 2.17.

This even is triggered when before the user successfully authenticated using SSL

    $(document).on( "sslSuccess", { }, function( event , info ) {
                console.log( "SSL success", info );
    });

### webauthnAttempt

Added in version 2.17.

This even is triggered immediately before the user tries to use a WebAuthn device

    $(document).on( "webauthnAttempt", { }, function( event ) {
                console.log( "WebAuthn attempt" );
    });

### webauthnFailure

Added in version 2.17.

This even is triggered when the user failed to use a WebAuthn device

    $(document).on( "webauthnFailure", { }, function( event , info ) {
                console.log( "WebAuthn failure", info );
    });

### webauthnSuccess

Added in version 2.17.

This even is triggered when the user successfully used a WebAuthn device

    $(document).on( "webauthnSuccess", { }, function( event , info ) {
                console.log( "WebAuthn success", info );
    });

### webauthnRegistrationAttempt

Added in version 2.17.

This even is triggered immediately before the user tries to register a WebAuthn device

    $(document).on( "webauthnRegistrationAttempt", { }, function( event ) {
                console.log( "WebAuthn registration attempt" );
    });

### webauthnRegistrationFailure

Added in version 2.17.

This even is triggered when the user failed to register a WebAuthn device

    $(document).on( "webauthnRegistrationFailure", { }, function( event , info ) {
                console.log( "WebAuthn registration failure", info );
    });

### webauthnRegistrationSuccess

Added in version 2.17.

This even is triggered when the user successfully registered a WebAuthn device

    $(document).on( "webauthnRegistrationSuccess", { }, function( event , info ) {
                console.log( "WebAuthn registration success", info );
    });

## Password policy

### checkpassword

Added in version 2.18.

This event is triggered on every keystroke while the user inputs a new password. You can use it to check if it fits certain criteria.

You can fetch the candidate password in `context.password`.

`context.evType` will contain `focusout` if the user has focused out of the password field. You can use it for costly password quality checks to avoid running the check at every keystroke.

`context.setResult` is a function that you can call to update the password form state.

- Its first argument is the `id` of the policy that you added with `addPasswordPolicy` in the server-side code.

- Its second argument is the state of the policy, you can use one of `good`, `bad`, `unknown`, `waiting` or `info`.

The new password form can only be submitted when all policies have been set to a status of either `good` or `info`.

    $(document).on( "checkpassword", function( event , context ) {
        password = context.password;
        evType = context.evType;
        setResult = context.setResult;

        // Do your client-side check here
        setResult("my-custom-ppolicy", "bad");
    });