dusterio / lumen-passport

Making Laravel Passport work with Lumen
MIT License
654 stars 141 forks source link

no model class available issue in luman #16

Closed gurinder-sason closed 2 years ago

gurinder-sason commented 7 years ago

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

paolopiccinini commented 7 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 
    }
} 
paolopiccinini commented 7 years ago

I've updated comment

himanshusinghs commented 7 years ago

@paolopiccinini Will it cause any trouble if I have a different set of columns in my user table and have not updated User model ?

paolopiccinini commented 7 years ago

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

DCdeBrabander commented 7 years ago

@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?

paolopiccinini commented 7 years ago

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.