Skip to content
Advertisement

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-zd@$!%*?&]{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)

Advertisement

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.');
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement