Closed gurinder-sason closed 2 years ago
Ok. you can use a class with this method findForPassport
'providers' => [
'users' => [
'driver' => 'database',
'model' => YourClass::class,
]
public class YourClass implements AuthenticatableContract, AuthorizableContract {
use HasApiTokens, Authenticatable, Authorizable;
public $password;
public $username;
...
..
public function findForPassport($username) {
your select
}
}
I've updated comment
@paolopiccinini Will it cause any trouble if I have a different set of columns in my user table and have not updated User model ?
By default Passport uses the model defined in auth.php. The dafault behavior is to check the users against email and password. If you want to customize this you have to implement 2 methods in your model: findForPassport($username) (that has to return one user) and validateForPassportPasswordGrant($password). So if you have different columns in your model you can use this methods
@paolopiccinini That's very interesting, I have used standard email and password columns since I had some problems implementing different ones. Under the hood, user login functionality normally expects the email column so I just used the default without really using emails. 😳
So if one implements these methods inside defined User model:
findForPassport($username)
validateForPassportPasswordGrant($password).
one is good to go?
you can also use one of them or both. it depends how you want to validate the users. If for example you does not have an md5 password on the db, you should use validateForPassportPasswordGrant($password) and check the password against the codification you have on the db. On the other side if you want to check the username against the name column of the database you should use findForPassport($username). If you need both you can use both.
See Laravel\Passport\Bridge\UserRepository->getUserEntityByUserCredentials
Remeber that findForPassport must return an instance of the class using the hasHapiToken trait.
Hi @dusterio
i was following steps given by you
i stuck at place where it says i need to use trait in User model, the project on which i am working is already build and this project is using only controllers no model file is used, tables are directly used in controller. Now Please let me know how i can do this step. Second my user table has some different name, is it will affect passport integration.
Thanks