1

I'm doing multi authentication in Laravel in user login form the error validation is working but in my company login form the error validation is not working . please help me I'm just new in Laravel and I'm just a student. Sorry for my English

this is my code in login

public function login(Request $request)
{
  $this->validate($request, [
    'email' => 'required|email',
    'password' => 'required'
  ]);

  if (Auth::guard('company')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
    return redirect()->intended(route('company'));
  }
  return redirect()->back()->withInput($request->only('email','remember'));
}

and this is my form where error must show

  <div class="form-group row">
                        <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>

                            @if ($errors->has('email'))
                                <span class="invalid-feedback">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>
5
  • Can I ask, what is the purpose of doing it this way and why are you not just using laravels out the box auth scaffolding? I feel like you're making alot of unrequired stress for yourself, only to achieve what laravel already gives you. Commented May 13, 2018 at 4:59
  • im using the auth scaffolding . but in my case i have 3 users. Company,Admin and the Users . in user form i've use what laravel already have the auth scaffolding but in company and admin i used custom login Commented May 13, 2018 at 5:04
  • Well, it's up to you how you want to go about things, but I feel like you're making a simple task extremely complex. If I was in your position i'd just make a new Model called UserRole and then define a relationship between User and UserRole, you can then just add a column to your users table 'role_id' and make a small trait like UserPermission and then just define some dead simple logic so you can call stuff like if($user->isAdmin()), if($user->isUser()), if($user->isCompany()) Commented May 13, 2018 at 13:04
  • okay nice idea. ill rid of that. but can you tell me why error is not showing? Commented May 14, 2018 at 4:00
  • You're not sending any errors to the view. back()->withErrors(); Commented May 14, 2018 at 6:22

1 Answer 1

1

instead of:

return redirect()->back()->withInput($request->only('email','remember'));

use this:

    $errors = new MessageBag(['password' => ['Email and/or password invalid.']]);
    return Redirect::back()->withErrors($errors)->withInput(Input::except('password'));

also add this to the top of your controller

use Redirect;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Facades\Input;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.