blogs

Installing Google Library and Setting Up Project:

  1. Install Google API PHP Client Library:
    • Use Composer to install the Google API PHP Client library. Run the following command in your project directory:
      composer require google/apiclient:^2.0
      
    • This will download and install the necessary files for interacting with Google APIs.
  2. Set Up Google Cloud Platform Project:
    • Go to the Google Cloud Console.
    • Create a new project or select an existing one.
    • Navigate to the “APIs & Services” > “Library” section.
    • Enable the required APIs for your project, such as the Google Sign-In API.
  3. Create OAuth 2.0 Credentials:
    • In the Cloud Console, navigate to the “APIs & Services” > “Credentials” section.
    • Click on “Create credentials” and select “OAuth client ID”.
    • Choose the application type (e.g., Web application).
    • Add authorized redirect URIs where Google will redirect users after authentication. Ensure these URIs use HTTPS.
    • Click “Create” to generate client ID and client secret.

Copy Client ID, Secret ID, Redirect URI:

  1. Copy Client ID and Secret ID:
    • After creating OAuth 2.0 credentials, you’ll receive a client ID and client secret.
    • Copy the client ID and client secret values provided by Google.
  2. Copy Redirect URI:
    • Ensure to copy the redirect URI(s) you configured in the OAuth client ID setup.
    • These redirect URIs are where Google will redirect users after they authenticate. Remember that these URIs must use HTTPS for security reasons.

Google Signup Credentials Menu Page

Adding Menu Page to Save Keys:

-This PHP function adds a menu page titled “Social Login” under the WordPress admin menu. It provides options to manage social login settings and is accessible to users with the “manage_options” capability.

/** Add menu page : Google signup credentials */ 
function add_social_login_menu() {
    add_menu_page(
        'Social Login Settings',
        'Social Login',
        'manage_options',
        'social-login-settings',
        'social_login_page_content',
        'dashicons-admin-users',
        30
    );
}
add_action('admin_menu', 'add_social_login_menu');


/** Callback function for Google signup credentials */ 
function social_login_page_content() {
	$client_id = get_option('social_login_client_id');
	$secret_id = get_option('social_login_secret_id');
	$redirect_uri = get_option('social_login_redirect_uri');
	?>
	
	<form id="social-login-form">
	<label for="client-id">Client ID:</label>
	<input type="text" id="client-id" name="client_id" value="<?php echo esc_attr($client_id); ?>"  style="width: 100%;"><br>
	
	<label for="secret-id">Secret ID:</label>
	<input type="text" id="secret-id" name="secret_id" value="<?php echo esc_attr($secret_id); ?>"  style="width: 100%;"><br>
	
	<label for="redirect-uri">Redirect URI:</label>
	<input type="text" id="redirect-uri" name="redirect_uri" value="<?php echo esc_attr($redirect_uri); ?>"  style="width: 100%;"><br>
	
	<button type="submit" id="save-settings" class="button-primary">Save Settings</button>
	</form>
	<div id="status-message"></div>   
    <?php
}
add_action('wp_ajax_save_social_login_settings', 'save_social_login_settings');
function save_social_login_settings() {
    // Sanitize and save settings
    $client_id = $_POST['client_id'];
    $secret_id = $_POST['secret_id'];
    $redirect_uri = $_POST['redirect_uri'];
    
    // Save settings to database or any other action
    update_option('social_login_client_id', $client_id);
    update_option('social_login_secret_id', $secret_id);
    update_option('social_login_redirect_uri', $redirect_uri);
    
    wp_send_json_success('Settings saved successfully');
}
jQuery('#social-login-form').on('submit', function(e) {
    e.preventDefault();
    var client_id = jQuery('#client-id').val();
    var secret_id = jQuery('#secret-id').val();
    var redirect_uri = jQuery('#redirect-uri').val();
    
    jQuery.ajax({
        url: ajax_object.ajax_url,
        type: 'POST',
        data: {
            action: 'save_social_login_settings',
            client_id: client_id,
            secret_id: secret_id,
            redirect_uri: redirect_uri,
        },
        success: function(response) {
            jQuery('#status-message').html('<div class="updated"><p>Settings saved successfully!</p></div>');
            setTimeout(function () {
                jQuery('#status-message').empty();
            }, 2000);               
        },
        error: function(xhr, status, error) {
            jQuery('#status-message').html('<div class="error"><p>' + xhr.responseText + '</p></div>');
        }
    });
});

Copy Client ID, Secret ID, Redirect URI and Save in adminarea

Display Google Sign-In Button in Frontend

Ajax Handler for Google Authentication and How to Make user Google register, or Re-Login if already exist and send credential’s Mail notification

/**
 * Ajax handle for google authentication
 */
function google_login_ajax_handler() {
    // Load Google API PHP Client library
    require_once get_template_directory() . '/vendor/autoload.php';

    // Retrieve Google API credentials from WordPress options
    $client_id = get_option('social_login_client_id');
    $secret_id = get_option('social_login_secret_id');
    $redirect_uri = get_option('social_login_redirect_uri');

    // Initialize Google Client
    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($secret_id);
    $client->setRedirectUri($redirect_uri);
    $client->addScope("email");
    $client->addScope("profile");

    // Handle Google Sign-In callback
    if (isset($_POST['code'])) {
        $token = $client->fetchAccessTokenWithAuthCode($_POST['code']);
        if (!isset($token['error'])) {
            $client->setAccessToken($token['access_token']);
            $google_oauth = new Google_Service_Oauth2($client);
            $google_account_info = $google_oauth->userinfo->get();

            // Check if the user exists in WordPress
            $user_email = $google_account_info->getEmail();
            $first_name = $google_account_info->getGivenName();
            $last_name = $google_account_info->getFamilyName();
            $user = get_user_by('email', $user_email);
            $profile_url = home_url('/profile');
                    
            if (!empty($user->ID)) {
                try {
                    $error = '';
                    if (wp_set_current_user($user->ID)) {
                        // Set authentication cookie
                        if (wp_set_auth_cookie($user->ID)) {
                            // Trigger wp_login action
                            if (do_action('wp_login', $user->user_login)) {
                                
                                // Check if the profile URL is valid
                                if ($profile_url) {
                                    wp_send_json_success(array('redirect' => $profile_url));
                                } else {
                                    $error .= 'Invalid profile URL.';                                   
                                }
                            } else {
                                // Error triggering wp_login action
                                $error = 'Error triggering wp_login action.';                             
                            }
                        } else {
                            $error .= 'Error setting authentication cookie.';                           
                        }
                        wp_send_json_error(array('error' => 'Error triggering wp_login action.','redirect' => $profile_url,'user'=>'exist'));
                    } else {
                        $error .= 'Error setting current user.';
                        // Error setting current user
                        wp_send_json_error(array('error' => 'Error setting current user.'));
                    }
                   
                } catch (Exception $e) {
                    wp_send_json_error(array('error' => $e->getMessage()));
                }
                
            } else {
               // Create user data array
               $password = wp_generate_password();               
               // Create user
               $user_id = wp_create_user($user_email, $password, $user_email);
               if (is_wp_error($user_id)) {
                   // Error creating user
                   $error_message = $user_id->get_error_message();
                   wp_send_json_error(array('error' => $error_message));
               } else {
                    // $user_id = $user->ID;
                    update_user_meta($user_id, 'first_name', $first_name);
                    update_user_meta($user_id, 'last_name', $last_name);

                    // Send notification email to user
                    $subject = 'Welcome to our website';
                    $message = 'Dear ' . $first_name . ',<br><br>';
                    $message .= 'Thank you for signing up on our website. Your account has been created successfully.<br>';
                    $message .= 'Your username: ' . $user_email . '<br>';
                    $message .= 'Your password: ' . $password . '<br><br>';
                    $message .= 'Please keep this information secure.<br><br>';
                    $message .= 'Best regards,<br>';

                    // Send the email
                    $headers[] = 'Content-Type: text/html; charset=UTF-8';
                    wp_mail($user_email, $subject, $message, $headers);

                    // User created successfully, sign in the user
                   $creds = array(
                       'user_login' => $user_email,
                       'user_password' => $password,
                   );
                   $signon_user = wp_signon($creds);
                   if (is_wp_error($signon_user)) {
                       // Error signing in the user
                       $error_message = $signon_user->get_error_message();
                       wp_send_json_error(array('error' => $error_message));
                   } else {
                       // Sign in successful, send success response
                       $redirect_url = home_url('/profile');
                       wp_send_json_success(array('redirect' => $redirect_url));
                   }
               }
            }
        }
    } else {
        // Code parameter not found in the request
        wp_send_json_error(array('error' => 'No code parameter found.'));
    }
}

// Hook the AJAX handler function
add_action('wp_ajax_google_login_ajax', 'google_login_ajax_handler');
add_action('wp_ajax_nopriv_google_login_ajax', 'google_login_ajax_handler');

User Existence Check and Sign-In:

New User Creation and Sign-In: