Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-139 : Remember functionality for login with google #144

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion assets/build/css/login.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/build/js/login.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '57f26158d9cbe8f4');
<?php return array('dependencies' => array(), 'version' => '4d9d3a64de60ef78');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elifvish we should start using short array syntax [ 'dependencies' => [], 'version' => '4d9d3a64de60ef78' ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asset file is automatically generated during build.

2 changes: 1 addition & 1 deletion assets/build/js/login.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions assets/src/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const wpGoogleLogin = {
*/
init() {
document.addEventListener( 'DOMContentLoaded', this.onContentLoaded );
const rememberMeCheckbox = document.getElementById( 'remember-google-login' );
rememberMeCheckbox.addEventListener( 'change', this.rememberMe );
},

/**
Expand Down Expand Up @@ -43,8 +45,46 @@ const wpGoogleLogin = {
this.googleLoginButton.classList.remove( 'hidden' );
// HTML is cloned from existing HTML node.
this.form.append( this.googleLoginButton );
},

/**
* Callback function to detect change in state of remember me checkbox.
*
* Update request parameters based on user selection
*
* @return void
*/
rememberMe() {

const loginWithGoogle = document.getElementsByClassName( 'wp_google_login__button' )[0];

if( this.checked === true ) {
window.remember = true;
var params = loginWithGoogle.getAttribute( 'href' );
var state = params.substring( params.indexOf( '&state=' ) + 7, params.indexOf( '&scope' ) );

/* Decodes state value */
var decodeState = JSON.parse( atob( state ) );

/* Add remember parameter to state */
decodeState['remember'] = true;
var newState = btoa( JSON.stringify( decodeState ) );
window.orignalParams = params;
params = params.replace( state, newState );

/* Replace hyperlink to new state variable */
loginWithGoogle.setAttribute( 'href', params );

}

if( this.checked === false && window.remember === true ) {
/* Resets href attribute to orignal state if checked and unchecked again */
loginWithGoogle.setAttribute( 'href', window.orignalParams );
}
}



};

wpGoogleLogin.init();
4 changes: 4 additions & 0 deletions assets/src/scss/login.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ $white_color: #FFFFFF;
vertical-align: middle;
margin-right: 10px;
}

&__remember-container {
margin-top: 15px;
}
}

.wp-block-google-login-login-button {
Expand Down
2 changes: 1 addition & 1 deletion src/Modules/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function render_login_button( $attributes ): string {
* @since 1.2.3
*/
$force_display = $attributes['forceDisplay'] ?? false;
if ( $force_display || ! is_user_logged_in() || apply_filters( 'rtcamp.google_login_button_display', false ) ) {
if ( $force_display || ! is_user_logged_in() || apply_filters( 'rtcamp.google_login_button_display', false ) ) {
$markup = $this->markup(
[
'login_url' => $this->client->authorization_url(),
Expand Down
14 changes: 14 additions & 0 deletions src/Modules/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public function authenticate( $user = null ) {
return $user;
}

if ( ! empty( $decoded_state['remember'] ) && true === $decoded_state['remember'] ) {
add_action( 'wp_login', [ $this, 'remember_me' ], 9, 2 );
}

try {
$this->gh_client->set_access_token( $code );
$user = $this->gh_client->user();
Expand Down Expand Up @@ -230,4 +234,14 @@ public function login_redirect(): void {
exit;
}
}

/**
* Sets cookie to remember user
*
* @param String $user_login user login.
* @param WP_User $user WP User Object.
*/
public function remember_me( string $user_login, WP_User $user ) {
$this->authenticator->set_auth_cookies( $user, true );
}
}
5 changes: 3 additions & 2 deletions src/Utils/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ public function register( stdClass $user ): ?WP_User {
* Set auth cookies for WordPress login.
*
* @param WP_User $user WP User object.
* @param bool $remember to remember user or not.
*
* @return void
*/
public function set_auth_cookies( WP_User $user ) {
public function set_auth_cookies( WP_User $user, bool $remember = false ) {
wp_clear_auth_cookie();
wp_set_current_user( $user->ID, $user->user_login );
wp_set_auth_cookie( $user->ID );
wp_set_auth_cookie( $user->ID, $remember );
}

/**
Expand Down
4 changes: 4 additions & 0 deletions templates/google-login-button.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@
<?php echo esc_html( $button_text ); ?>
</a>
</div>
<div class="wp_google_login__remember-container">
<input type="checkbox" id="remember-google-login"/>
<label for="remember-google-login"><?php esc_html_e( 'Remember Me ( Login with Google )', 'login-with-google' ); ?></label>
</div>
</div>
3 changes: 2 additions & 1 deletion tests/php/Unit/Utils/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ public function testSetAuthCookies() {
$this->wpMockFunction(
'wp_set_auth_cookie',
[
100
100,
false,
],
1
);
Expand Down