I want to authenticate a user in my Laravel 8 app using email and password_new instead of email and password. I tried the following but kept getting the error Undefined index: password.
Here is the view:
<form action="{{ route('login') }}" method="post" autocomplete="off">
@csrf
<div class="card">
<div class="card-body">
<div class="form-group">
<label for="" class="col-form-label">Email</label>
<input type="text" name="email" id="" class="form-control">
</div>
<div class="form-group">
<label for="" class="col-form-label">Password</label>
<input type="password" name="password" id="" class="form-control">
</div>
<div class="form-group">
<input type="checkbox" name="remember" id=""> Remember me
</div>
</div>
<div class="card-footer d-grid">
<button class="btn btn-primary">Login</button>
</div>
</div>
</form>
This is what I have done in the controller:
public function store(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required',
]);
if (Auth::attempt(['email' => $request->email, 'password_new' => $request->password], $request->remember)){
$request->session()->regenerate();
return redirect()->intended(RouteServiceProvider::HOME);
}
return back()->withErrors(['error' => 'Login gagal. Silakan coba lagi.']);
}
In
Usermodel implement\Illuminate\Contracts\Auth\Authenticatableand add below method to override password fieldand auth attempt method
User model look like this
Ref: https://github.com/laravel/framework/pull/30161
https://github.com/laravel/framework/issues/30160