Ad

Logout And Redirect User Back To Login Blade With A Message On Change Password In Laravel

I'm trying to logout and redirect my user back to login blade once the user's password is changed successfully.

This is my controller so far (only the function is included),

public function store(Request $request)
    {
        $request->validate([
            'current_password' => ['required', new MatchOldPassword],
            'new_password' => ['required', 'string', 'min:12', 'confirmed','regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\[email protected]$!%*?&]{12,}$/'],
            'new_confirm_password' => ['same:new_password'],
        ]);

        User::find(auth()->user()->id)->update(['password'=> Hash::make($request->new_password)]);

        //dd('Password change successfully.');

    }

Here on success how can I logout and redirect my user back to login blade with a success message (Pass word has been changed. Please log in again)

Ad

Answer

I have two solution to this problem. Simply add with function to create a flash message you wanted.


Solution 1: Go to the AuthenticatesUsers.php and inside the logout function change the redirected route from / to /login. (Assuming your using make:auth)

public function logout(Request $request)
{
    //more codes here

    return $request->wantsJson()
        ? new Response('', 204)
        : redirect('/login')->with('success', 'Password change successfully.');
}


Solution 2: Make a customed one. Create a controller function and add these following codes below:

public function logout()
{
    \Auth::logout();
    return redirect('/login')->with('success', 'Password change successfully.');
}
Ad
source: stackoverflow.com
Ad