laravel authentication with eloquent driver on roles

后端 未结 1 1937
失恋的感觉
失恋的感觉 2020-12-21 22:41

I am trying to authenticate users in my Laravel application.

I am encountering the following problem:

  • using driver database in auth
相关标签:
1条回答
  • 2020-12-21 22:52

    The problem is your implementation of getAuthIdentifier(). This method should actually return the primary key of your table and not the username that's used for logging in.

    So yours should look like this:

    public function getAuthIdentifier(){
        return $this->id;
    }
    

    Or actually, I recommend you clean up your model a bit more since all of the getSomeAuthStuff methods are implemented in the two traits.

    Use the default model on github as a base and add all your custom code (roles methods, rules etc)

    Background info

    The value returned from getAuthIdentifier() will be stored in the session.
    When using check() afterwards, retrieveById will be called on the UserProvider. And the EloquentUserProvider does this:

    public function retrieveById($identifier)
    {
        return $this->createModel()->newQuery()->find($identifier);
    }
    

    It uses find() which searches for the model by it's primary key (usually id)

    0 讨论(0)
提交回复
热议问题