Last active
July 25, 2018 19:21
-
-
Save EduardoSP6/78aeece88d035efb1b71c0fb48521143 to your computer and use it in GitHub Desktop.
Authenticate user by other fields through Laravel Passport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment