flash message laravel for incorrect password
public function login(Request $request) { //Error messages $messages = [ "email.required" => "Email is required", "email.email" => "Email is not valid", "email.exists" => "Email doesn't exists", "password.required" => "Password is required", "password.min" => "Password must be at least 6 characters" ]; // validate the form data $validator = Validator::make($request->all(), [ 'email' => 'required|email|exists:users,email', 'password' => 'required|min:6' ], $messages); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } else { // attempt to log if (Auth::attempt(['approve' => '1', 'email' => $request->email, 'password' => $request->password ], $request->remember)) { // if successful -> redirect forward return redirect()->intended(route('user.overview')); } // if unsuccessful -> redirect back return redirect()->back()->withInput($request->only('email', 'remember'))->withErrors([ 'approve' => 'Wrong password or this account not approved yet.', ]); } }
Here is what the above code is Doing:
1. We are using the Validator facade to validate the form data.
2. We are using the Auth facade to authenticate the user.
3. If the validation fails, we redirect the user back to the login form with the errors and the old input.
4. If the validation passes, we attempt to log the user in.
5. If the login attempt is successful, we redirect the user to the intended URL.
6. If the login attempt is unsuccessful, we redirect the user back to the login form with an error message.
### Logout
Now, let’s create a logout method in the AuthController.