diff --git a/assets/js/common.js b/assets/js/common.js index 6422e60..aa4a07a 100644 --- a/assets/js/common.js +++ b/assets/js/common.js @@ -12,7 +12,6 @@ }, complete: function(xhr) { var response = xhr.responseJSON; - if (response === null || response.hasOwnProperty('data')) { window.location.reload(true); } @@ -32,13 +31,13 @@ }); } - // Google callback - window.onGoogleAuth = function(user) { + // Google callback - UPDATED for new Google Identity Services + window.onGoogleAuth = function(credentialResponse) { + // New API returns JWT token directly in credential property hivepress.authUser({ 'authenticator': 'google', - 'id_token': user.getAuthResponse().id_token, + 'id_token': credentialResponse.credential, }); - - gapi.auth2.getAuthInstance().disconnect(); } + })(jQuery); diff --git a/assets/js/common.min.js b/assets/js/common.min.js index f151a3f..7a05960 100644 --- a/assets/js/common.min.js +++ b/assets/js/common.min.js @@ -1 +1 @@ -!function(e){"use strict";hivepress.authUser=function(t){e.ajax({url:hivepressAuthenticationData.apiURL+t.authenticator,method:"POST",data:t,beforeSend:function(e){e.setRequestHeader("X-WP-Nonce",hivepressCoreData.apiNonce)},complete:function(e){var t=e.responseJSON;(null===t||t.hasOwnProperty("data"))&&window.location.reload(!0)}})},window.onFacebookAuth=function(){FB.getLoginStatus(function(e){"connected"===e.status&&hivepress.authUser({authenticator:"facebook",access_token:e.authResponse.accessToken})})},window.onGoogleAuth=function(e){hivepress.authUser({authenticator:"google",id_token:e.getAuthResponse().id_token}),gapi.auth2.getAuthInstance().disconnect()}}(jQuery); +!function(e){"use strict";hivepress.authUser=function(t){e.ajax({url:hivepressAuthenticationData.apiURL+t.authenticator,method:"POST",data:t,beforeSend:function(e){e.setRequestHeader("X-WP-Nonce",hivepressCoreData.apiNonce)},complete:function(e){var t=e.responseJSON;(null===t||t.hasOwnProperty("data"))&&window.location.reload(!0)}})},window.onFacebookAuth=function(){FB.getLoginStatus(function(e){"connected"===e.status&&hivepress.authUser({authenticator:"facebook",access_token:e.authResponse.accessToken})})},window.onGoogleAuth=function(e){hivepress.authUser({authenticator:"google",id_token:e.credential})}}(jQuery); diff --git a/hivepress-authentication.php b/hivepress-authentication.php index 3e7f0b4..db10a3c 100644 --- a/hivepress-authentication.php +++ b/hivepress-authentication.php @@ -2,7 +2,7 @@ /** * Plugin Name: HivePress Authentication * Description: Allow users to sign in via third-party services. - * Version: 1.1.4 + * Version: 1.2.0 * Author: HivePress * Author URI: https://hivepress.io/ * Text Domain: hivepress-authentication diff --git a/includes/components/class-google-authentication.php b/includes/components/class-google-authentication.php index ef3e769..f9b1705 100644 --- a/includes/components/class-google-authentication.php +++ b/includes/components/class-google-authentication.php @@ -4,51 +4,40 @@ * * @package HivePress\Components */ - namespace HivePress\Components; - use HivePress\Helpers as hp; - // Exit if accessed directly. defined( 'ABSPATH' ) || exit; - /** * Google authentication component class. * * @class Google_Authentication */ final class Google_Authentication extends Component { - /** * Class constructor. * * @param array $args Component arguments. */ public function __construct( $args = [] ) { - // Check Google status. if ( ! in_array( 'google', (array) get_option( 'hp_user_auth_methods' ), true ) || ! get_option( 'hp_google_client_id' ) ) { return; } - // Set response. add_filter( 'hivepress/v1/authenticators/google/response', [ $this, 'set_response' ], 10, 2 ); - if ( ! is_user_logged_in() && ! is_admin() ) { - // Enqueue scripts. add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); - // Render header. add_action( 'wp_head', [ $this, 'render_header' ] ); - // Render button. add_filter( 'hivepress/v1/forms/user_authenticate/header', [ $this, 'render_button' ] ); + // Render footer initialization. + add_action( 'wp_footer', [ $this, 'render_footer' ] ); } - parent::__construct( $args ); } - /** * Sets response. * @@ -57,7 +46,6 @@ public function __construct( $args = [] ) { * @return mixed */ public function set_response( $response, $request ) { - // Get response. $response = json_decode( wp_remote_retrieve_body( @@ -71,45 +59,38 @@ public function set_response( $response, $request ) { ), true ); - if ( $response && ! isset( $response['error'] ) ) { - // Check client ID. if ( get_option( 'hp_google_client_id' ) !== $response['aud'] ) { return [ 'error' => 'invalid_client' ]; } - // Check email status. if ( 'true' !== $response['email_verified'] ) { return [ 'error' => 'unverified_email' ]; } - // Set user details. $response['id'] = $response['sub']; $response['first_name'] = $response['given_name']; $response['last_name'] = $response['family_name']; } - return $response; } - /** * Enqueues scripts. */ public function enqueue_scripts() { - wp_enqueue_script( 'google-platform', 'https://apis.google.com/js/platform.js', [], null, true ); - - wp_script_add_data( 'google-platform', 'async', true ); - wp_script_add_data( 'google-platform', 'defer', true ); + // UPDATED: Load new Google Identity Services library instead of old platform.js + wp_enqueue_script( 'google-identity-services', 'https://accounts.google.com/gsi/client', [], null, true ); + wp_script_add_data( 'google-identity-services', 'async', true ); + wp_script_add_data( 'google-identity-services', 'defer', true ); } - /** * Renders header. */ public function render_header() { + // Keep the meta tag for backwards compatibility, though new API doesn't require it echo ''; } - /** * Renders button. * @@ -117,6 +98,53 @@ public function render_header() { * @return string */ public function render_button( $output ) { - return $output . '


'; + // UPDATED: Use a simple div container where Google will render the button + return $output . '


'; + } + /** + * Renders footer with Google Sign-In initialization. + * ADDED: New method to initialize the new Google Identity Services API + */ + public function render_footer() { + $client_id = esc_js( get_option( 'hp_google_client_id' ) ); + ?> + +