WordPress automatic login after reset password

Solution:1

I finally managed it by myself with below code. Hope this code helps the others!

add_action( 'validate_password_reset', 'rsm_redirect_after_rest', 10, 2 );
function rsm_redirect_after_rest($errors, $user) {
    if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
        reset_password( $user, $_POST['pass1'] );

        list( $rp_path ) = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) );
        $rp_cookie = 'wp-resetpass-' . COOKIEHASH;
        setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );

        wp_set_current_user( $user->ID );
        wp_set_auth_cookie( $user->ID );
        do_action( 'wp_login', $user->user_login, $user );//`[Codex Ref.][1]
        wp_redirect( home_url() );
        exit;
    }
}

Solution:2

In WordPress there are before and after hooks available, like post save hook.

In my case i found some useful hooks

1) password_reset action hook

<?php

add_action( 'password_reset', 'before_password_update', 10, 2 );

function before_password_update( $user, $new_pass ) {
//write code before password reset.
}
?>

This hook runs after the user submits a new password and before the new password is set. Mean you have entered the new password but password not updated in db.
2) after_password_reset hook

<?php

function action_after_password_update( $wp_password_change_notification ) {
//write code after password reset.
};

add_action( 'after_password_reset', 'action_after_password_update', 10, 1 );

?>

This hook runs after the user submits a new password and after the new password is set.

3) If you are using the WooCommerce in this case you have to use the following hook

woocommerce_customer_reset_password

<?php

function action_woocommerce_reset_password( $user ) {
$login=$_POST['reset_login'];
$pass=$_POST['password_1'];
$creds = array(
'user_login' => $login,
'user_password' => $pass,
'remember' => true
);

wp_signon( $creds, false );
};
add_action( 'woocommerce_customer_reset_password', 'action_woocommerce_reset_password', 10, 1 );

?>

WooCommerce overrides the core WordPress login so, if you are using WooCommerce you have use the exact above code for auto login after password reset. In action_woocommerce_reset_password function wp_signon core function isused for login. if we pass the required parameter as written in the code, wp_signon function will do login in wordpress.
The above piece of code will do all the thing as soon as you entered the new password.