Sudip Banerjee
Sudip Banerjee

Reputation: 41

Laravel 5.2 TokenGuard implementation

How to use token guard to create API? I tried it to implement it and I am getting error

call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\TokenGuard' does not have a method 'attempt'

Upvotes: 3

Views: 2861

Answers (1)

Holmes Conan
Holmes Conan

Reputation: 1241

After dig into the source code of laravel, I found that the token guard is useless for now. All auth are passed to auth middleware, from there, you can see that it called Auth::guard($name)->guest() to check whether the user is logged in. The \Auth::guard will get the proper guard that you specified in route. Let's say here is the TokenGuard. In \Illuminate\Auth\TokenGuard, check the user function to see how TokenGuard get a user. First, it will get the input parameter named api_token. Then it will let the provider which may be eloquent as the default configuration to search a value in the database. If any value is found, a new user instance is created. If there is not a input value named api_token, then some other choices will be tried:

  1. bearerToken, which the Authorization HTTP header value that starts with: bearer.
  2. password, which passed through HTTP header: PHP_AUTH_PW.

which key to match in the model is specified by the protected property storageKey.

So the token guard is used to implemented third-party API access token, not a temporary access token that is stored in the session.

Upvotes: 4

Related Questions