Skip to main content
Download the JWT Authentication for CoCart plugin to use these hooks.
Each action hook is documented below with its description and usage example.

Token Events

cocart_jwt_token_generated

Made available since v2.1.0
Fires when a new JWT token is generated after successful authentication.
add_action( 'cocart_jwt_token_generated', function( $token, $user ) {
    // Log token generation
    error_log("New token generated for user: {$user->ID}");
}, 10, 2 );

cocart_jwt_auth_token_refreshed

Made available since v2.1.0
Fires when a token is refreshed using a refresh token.
add_action( 'cocart_jwt_auth_token_refreshed', function( $token, $user ) {
    // Track token refresh events
    error_log("Token refreshed for user: {$user->ID}");
}, 10, 2 );

cocart_jwt_auth_token_validated

Made available since v2.1.0
Fires when a token is successfully validated.
add_action( 'cocart_jwt_auth_token_validated', function( $decoded ) {
    // Access validated token data
    $user_id = $decoded->data->user->id;
    error_log("Token validated for user: {$user_id}");
} );

Token Management

cocart_jwt_auth_token_deleted

Made available since v2.1.0
Fires when a token is deleted.
add_action( 'cocart_jwt_auth_token_deleted', function( $user_id ) {
    $user = get_user_by( 'id', $user_id );

    // Cleanup after token deletion
    error_log("Token for {$user->display_name} has been deleted");
}, 10, 2 );

Authentication Events

cocart_jwt_auth_authenticated

Made available since v3.0.0
Fires when a user is authenticated via JWT token.
add_action( 'cocart_jwt_auth_authenticated', function( $user ) {
    // Send admin notification for VIP customers
    if ( in_array( 'vip_customer', $user->roles ) ) {
        wp_mail(
            get_option( 'admin_email' ),
            'VIP Customer Login',
            "VIP customer {$user->display_name} has logged in via API."
        );
    }

    // Track API usage analytics
    $usage_count = get_user_meta( $user->ID, 'api_usage_count', true );
    update_user_meta( $user->ID, 'api_usage_count', intval( $usage_count ) + 1 );
} );
All actions follow WordPress coding standards and can be used with the standard add_action() function. The examples above show practical implementations for each action.
I