0

I want to be able to display error message if a certain query returns false when attempting to log in user.

for example, as in my case i want to show a message to a user that his account has been deactivated if the deactivated column is set to 'yes'

Here is my code login method, i tried adding a second error, but both error returns if login fails, regardless of the failed query

    public function login(Request $request)
    {

        $message = array(
            'required.email'    =>  'Email is required',
            'required.password' =>  'Password is required',
        );
        $this->validate($request, [
            'email' =>  'required|email',
            'password'  =>  'required|min:8',
        ]);

        $email = $request->email;
        $pass = $request->password;

        if (Auth::attempt([
            'email' => $email,
            'password' => $pass,
            'verified' => 'yes'
        ], $request->get('remember'))) {
            return redirect()->intended(route('user.account.index'));
        } else {
            return redirect()->back()->withErrors(
                [
                    'email' => 'These credentials do not match our records or this user has been deactivated!.',
                    'verified' => 'This user account has been deactivated! Contact support!'
                ]
            );
        }
    }

And here is my login view

<form action="{{ route('login') }}" method="post">
             @csrf

             <span>Login Your Account</span>
             <br><br>
             @if (Session::has('message'))
             <small class="alert alert-success">{{ Session::get('message') }}</small>
             <br><br>
             @endif
             @if (Session::has('status'))
             <small class="alert alert-info" role="alert">
                 {{ Session::get('status') }}
             </small>
             <br><br>
             @endif
             @error('verified')
             <small class="alert alert-danger" role="alert">
                 {{ $message }}
             </small>
             <br><br>
             @enderror
             <div class="form-group">
                 <input type="email" class="form-control" placeholder="Email Address" name="email">
                 @error('email')
                 <br>
                 <small class="alert alert-danger" role="alert">
                     {{ $message }}
                 </small>
                 @enderror
             </div>
             <div class="form-group">
                 <input type="password" class="form-control" placeholder="Password" name="password">
                 @error('password')
                 <br>
                 <small class="alert alert-danger" role="alert">
                     {{ $message }}
                 </small>
                 @enderror
             </div>
             <div class="text-right">
                 <a href="{{ route('password.request') }}">Forgot Password?</a>
             </div>
             <div class="custom-control custom-checkbox">
                 <input type="checkbox" class="custom-control-input" id="form-checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}>
                 <label class="custom-control-label" for="form-checkbox">{{ __('Remember Me') }}</label>
             </div>
             <button type="submit" class="btn btn-danger">Login to account</button>
         </form>
Morri
  • 37
  • 2
  • 9

1 Answers1

1

I will suggest using session flash messages instead of redirect witherror method. It has the advantage of being able to run custom messages in multiple situations. You can implement it like this: https://www.itsolutionstuff.com/post/laravel-5-implement-flash-messages-with-exampleexample.html

I also recommend checking this out to figure out how to customize your messages based on conditions. In Laravel, the best way to pass different types of flash messages in the session

Mash tan
  • 159
  • 11