Search code examples
phpauthenticationlaravel-5.6reset-password

Logout user from all browser when password is reset in laravel 5.6


When the user changes their password, they get Logged Out from the browser. However, if they are logged into another browser at the same time they stay logged in on the other browser.

I want to log out the user from all browsers they are logged into when they reset their password.

Here login controller.

function checklogin(Request $request)
{

    $this->validate($request, ['email' => 'required|email', 'password' => 'required|string|min:3']);

    $user_data = array(
        'email' => $request->get('email') ,
        'password' => $request->get('password')
    );

    $remember_me = $request->has('remember') ? true : false;

    if (Auth::attempt($user_data, $remember_me))
    {
        return redirect()->intended('dashboard');
    }
    else
    {
        return back()->with('error', 'Wrong Login Details');
    }
}

send mail function as below

function sendEmail(Request $request)
{

    $this->validate($request, ['email' => 'required|exists:users']);

    $email = $request->email;

    $name = User::where('email', $email)->first();
    $name = $name->name;

    $token = Password::getRepository()->createNewToken();
    $link = url("password/reset?email=$email&token=$token");

    $value = Password_resets::where('email', $email)->first();

    if (isset($value))
    {
        Password_resets::where('email', $email)->update(['email' => $email, 'token' => $token]);
    }
    else
    {
        Password_resets::insert(['email' => $email, 'token' => $token]);
    }

    Mail::to($email)->send(new \App\Mail\ResetPassword($link, $name));

    return redirect()->back()->with('success', 'Please check your Email for Password Reset');
}

password reset function as below

function resetpasswordchange(Request $request)
{

    $passwordtoken = $request->input('passwordtoken');
    $email = $request->input('email');
    $user_password = $request->input('user_password');

    $users['user'] = Password_resets::where('token', $passwordtoken)->where('email', $email)->get();
    if (empty($users['user'][0]))
    {
        $settoken = '0';

    }
    else
    {
        $settoken = $users['user'][0]->token;

    }

    if (($settoken) == $passwordtoken)
    {

        $update = array(
            'password' => bcrypt($user_password) ,
        );

        User::where('email', $email)->update($update);
       /* Auth::logout();
        auth()->logoutOtherDevices(bcrypt($user_password),'password');*/

        return redirect()->route('login')->with('success', 'Password has been Updated.');

    }
    else
    {
        return redirect()->back()->with('error', 'Token & Email Not Match!.');
    }
}

How I can logout the user from all browsers who they are logged already ?


Solution

  • Open App\Http\Kernel and inside the protected $middlewareGroups property uncomment the \Illuminate\Session\Middleware\AuthenticateSession::class middleware. This compares the password hash of the user to see if the session is valid or not.