How to authenticate a user using other column than password in Laravel 8?

133 Views Asked by At

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.']);

}
1

There are 1 best solutions below

0
John Lobo On BEST ANSWER

In User model implement \Illuminate\Contracts\Auth\Authenticatable and add below method to override password field

public function getAuthPassword()
{
  return $this->password_new;
}

and auth attempt method

Auth::attempt(["email"=>"","password"=>"");

User model look like this

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements \Illuminate\Contracts\Auth\Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;


    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];


    public function getAuthPassword()
    {
        return $this->password_new;
    }

}

Ref: https://github.com/laravel/framework/pull/30161

https://github.com/laravel/framework/issues/30160