Previously it worked before I edit it for firebase, but for now I want to see local auth first after I edit it for firebase. it can register but not login, it resulted in The email field is required. it is what resulted in either normal user login or admin guard login.
web.php
Auth::routes();
Route::get('/admin',[LoginController::class,'showAdminLoginForm'])->name('admin.login-view');
Route::post('/admin',[LoginController::class,'adminLogin'])->name('admin.login');
LoginController.php
use AppHttpControllersController;
use AppProvidersRouteServiceProvider;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateHttpRequest;
use IlluminateValidationValidationException;
use Session;
use Auth;
use AppModelsUser;
use AppModelsAdmin;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $auth;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
//$this->firebaseAuth = $firebaseAuth;
$this->middleware('guest')->except('logout');
$this->middleware('guest:admin')->except('logout');
//$this->auth =app("firebase.auth");
}
protected function validateLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|string',
'password' => 'required|string',
]);
public function showAdminLoginForm()
{
return view('auth.login', ['url' => route('admin.login-view'), 'title'=>'Admin']);
}
public function adminLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('admin')->attempt($request->only(['email','password']), $request->get('remember'))){
return redirect()->intended('/admin/dashboard');
}
return back()->withInput($request->only('email', 'remember'));
}
}
login.blade.php
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
@csrf
<div class="form-group row">
<label for="login" class="col-sm-4 col-form-label text-md-right">
{{ __('Username or Email') }}
</label>
<div class="col-md-6">
<input id="login" type="text"
class="form-control{{ $errors->has('username') || $errors->has('email') ? ' is-invalid' : '' }}"
name="login" value="{{ old('username') ?: old('email') }}" required autofocus>
@if ($errors->has('username') || $errors->has('email'))
<span class="invalid-feedback">
<strong>{{ $errors->first('username') ?: $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
@if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
@endif
</div>
</div>
</form>
</div>
So what went wrong that I get The email field is required? Before I edit it for firebase it working perfectly in local.
2
Answers
In your controller, you have defined validation for
email
field is required but in your form in blade file you have named field aslogin
. That’s why in controller, email value is not found.Change the field name from
name="login"
toname="email"
.its because your blade try to submit the login parameter instead of email parameter
you can fix it by :