Authenticate user by other model fields in Laravel 5.x API and Passport.
<?php
class User extends Authenticatable implements Transformable
{
use TransformableTrait;
use Notifiable;
use HasApiTokens;
protected $fillable = [
"registry",
"cpf",
"username",
"password",
];
/**
* Implement those methods below. This methods can be found at: ../vendor/laravel/passport/src/Bridge/UserRepository.php
*/
public function findForPassport($registry)
{
// Find the user by the parameter received, in this case is registry field
$user = $this->where('registry', '=', $registry)->first();
if(!$user)
return null;
// check if user status is: 1 - enabled
if($user->status != 1) {
return null;
}
return $user;
}
/** Validate password by any field of model user, in this case is cpf field **/
public function validateForPassportPasswordGrant($password)
{
return $this->where('cpf', $password)->first();
}
}