rtCamp / login-with-google

Minimal plugin which allows WordPress user to login with google.
https://wordpress.org/plugins/login-with-google/
GNU General Public License v2.0
57 stars 17 forks source link

Google Login to remember the login for next session, just like "Remember Me" option in standard login form. #139

Open inf3cti0n95 opened 1 year ago

inf3cti0n95 commented 1 year ago

I want the plugin to remember the login, just like "Remember Me" option in standard login form.

username0136 commented 1 year ago

+1

aviral-mittal commented 1 year ago

We are considering adding this feature in next release - however, not final yet.

Thanks for pointing this out.

rothschild86 commented 2 months ago

+1 pretty useless for my users without this...

perhaps could borrow some tactics from this plugin? https://wordpress.org/plugins/wp-persistent-login/

rothschild86 commented 2 months ago

Here's tested working code on persisting the login - as seen in https://wordpress.org/plugins/wp-persistent-login/ wp-persistent-login.php:348

essentially, it finds the relevant session, updates it, and then creates a new cookie

add_action( 'wp_login', function($user_login, $user){

//calls into persistent login plugin https://wordpress.org/plugins/wp-persistent-login/

if(!class_exists('WP_Persistent_Login_Manage_Sessions')){return;}

$user_id = $user->ID;

update_user_meta( $user_id, 'persistent_login_remember_me', 'true');

// get the users latest session from the database
$sessions = get_user_meta( $user_id, 'session_tokens', true );
if( is_array($sessions) ) {

    // fetch the login time column from the array
    $login_times = array_column($sessions, 'login');

    // sort the sessions by login times (newest first)
    array_multisort( $login_times, SORT_DESC, $sessions );

    // get the key (verifier) of the first session
    $session_verifier = array_key_first($sessions);

    //remove the session from the database
    $wp_login_manage_sessions = new WP_Persistent_Login_Manage_Sessions( $user_id );
    $wp_login_manage_sessions->persistent_login_update_session( $session_verifier, null );

    // set a new cookie with remember me checked
    wp_set_auth_cookie( $user_id, true );
}

}, 10, 2 );