Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

}

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading