Home > Software engineering >  laravel how to use auth:attempt without password (replace by other column)
laravel how to use auth:attempt without password (replace by other column)

Time:12-02

I have column nik, password, and pin in users table, and I want to use auth::attempt with 2 case

  1. if column pin is null then auth::attempt use parameter nik and password Auth::attempt(['empnik' => $empnik, 'password' => "12345678"]);
  2. if column pin is not null then auth::attempt use parameter nik and pin (note: pin is encrypted by hash like password) Auth::attempt(['empnik' => $empnik, 'pin' => "123456"]);.

in second case the return is fail because maybe pin not check using hash like password, even though my pin is encrypted by hash. So how to Auth::Attempt validate column pin using hash? is it possible?

this my user model

protected $fillable = [
        'empnik','empname', 'email', 'password', 'pin'
    ];


    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'pin'
    ];

CodePudding user response:

You can change the password used on the User.php model, with the following method.

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

    return $this->password;
}

Now that needs to be hashed and there is an not optimal easy approach or the correct version.


The easy not optimal approach

Simply hash it when you check it.

if ($this->pin) {
    return Hash::make($this->pin);
}

Or Update all the pins to be hashed and when saving or updating it use the hash function.

$user->pin = Hash::make($request->pin);
  • Related