Search Results :

×

Authenticate users with Cognito using WordPress Custom Login Code

Authenticate users with Cognito using WordPress Custom Login Code


Problem Statement

Handle WordPress Login and User registration in WordPress Cognito User Pool through your custom code.

There may be situations where the custom forms plugin has limitations and you need your own code to design the login and registration forms, handle the form submit event, and manage the backend flow to authenticate users via Cognito credentials. In these cases, having the ability to write custom code can be useful. By creating your own code, you have full control over the form design and authentication functionality.

How to authenticate users with Cognito using WordPress Custom Login Code?

When faced with such situations, the WordPress Cognito Integration becomes a valuable solution for incorporating WordPress login with Cognito Credentials and user registration into a Cognito Pool within your backend code. To use the WordPress Cognito Integrator, make sure to first install the WordPress OAuth Client plugin on your website. Know more about WordPress OAuth Client plugin.

To achieve this, simply invoke the appropriate login and registration functions provided by our plugin within your code, passing the necessary parameters. The WordPress Cognito Integration will take care of the remaining tasks.

Here are the provided code snippets that will guide you in implementing the code on your side, effectively handing over control to the WordPress Cognito Integration and providing the essential information.

1. Login Functionality

Fetch the entered email/username and password, sanitize and escape them, and then pass them as arguments to our function. mo_ci_custom_login_form()

Function Definition:

$username Username or Email of the user depending upon whether your Cognito pool is configured to username & password login OR email & password Login.
$password Plain text password entered by the user.
$return_user If true, the function would return the user ID, and you would need to explicitly set the login cookie for the user. If false, the Cognito Integrator itself would log in the user.
$result WP_User Object or Error string.

Code Snippet:

<?php
    // Your Code
    // Fetch the input values after the form is submitted
    // Code to sanitize the input values

    $return_user = false; // you want the user login session created by the miniOrange plugin and further handling

    $result = mo_ci_custom_login_form($username, $password, $return_user);

    if(!empty($result)){
        // Your custom code for displaying the error messages
    }
  ?>

2. Registration Flow

Fetch the information entered in the registration form, sanitize and escape the information, and store the sanitized and escaped information in an array following the specified format:

$user_data = array(
    "user_pass" => $password_entered,
    "user_login" => $username_entered,
    "user_email" => $email,
    "display_name" => $display_name, // you can pass username or email as well
    "nickname" => $nickname, // you can pass username or email as well
    "first_name" => $firstname,
    "last_name" => $lastname,
    "user_registered" => $registered_date,
    "role" => $wp_role,
    "meta_input" => $meta_data,
  )
  

Any other extra information other than the basic WP user profile attributes would be inside $meta_data. It would be an associative array with the array key as the WordPress meta key and value as the entered value. Below is the format for $meta_data

$meta_data = array(
    "<- your-wp_meta_key_for_city_name ->" => $city_name,
    "<- your-wp_meta_key_for_acc_no ->" => $account_number,
    // ...
  );
  

Pass the complete $user_data array as an argument to the function mo_ci_custom_registeration_form()

Function Definition:

function mo_ci_custom_registeration_form ($user_data, $return_user=false){
    return $result;
  }
  
$username Username or Email of the user depending upon whether your Cognito pool is configured to username & password login OR email & password Login.
$password Plain text password entered by the user.
$return_user If true then the function would return the user object and you would need to explicitly set the login cookie for the user. If false then the Cognito Integrator itself would login the user.
$result WP_User Object or Error string.

Code Snippet:

<?php
    // Your Code
    // Fetch the input values after the form is submitted
    // Code to sanitize the input values

    $return_user = false; // you want the user login session created by the miniOrange plugin and further handling

    $result = mo_ci_custom_registeration_form($user_data, $return_user);

    if(!empty($result)){
        // Your custom code for displaying the error messages
    }
  ?>

Additional Cognito Use Cases Supported

Seamless User Management with Cognito User Pool Integration
Learn More
Sync User Memberships to Cognito Pool with WP Cognito Integration
Learn More
Access Site with Cognito Credentials & Custom Forms
Learn More
Register & verify users via Cognito OTP on WooCommerce checkout
Learn More
Sync existing users from WordPress to Cognito user Pool
Learn More
AWS Cognito SSO and User Sync with WP and Woocommerce
AWS Cognito Single Sign-On (SSO) usecase
Learn More

Additional Resources



Try a Free Demo








 Thank you for your response. We will get back to you soon.

Something went wrong. Please submit your query again

Discover how miniOrange can help you
Sign up for a Demo to receive


  Overview of Plugin's Premium features

  Test Add-ons & 3rd-party integrations

  24*7 support with mail and Zoom call

  Get a guided demo from a Developer

Need Help?

Mail us on oauthsupport@xecurify.com for quick guidance(via email/meeting) on your requirement and our team will help you to select the best suitable solution/plan as per your requirement.

Hello there!

Need Help? We are right here!

support
Contact miniOrange Support
success

Thanks for your inquiry.

If you dont hear from us within 24 hours, please feel free to send a follow up email to info@xecurify.com