Hi,
I'm struggling with implementing the token based authentication scheme.
Using a clean Laravel 5.2 install and a php artisan make:auth I create the following test route:
// routes.php
Route::get('/home-index', ['uses' => 'HomeController@index', 'middleware' => ['web']]);
Route::get('/home-web', ['uses' => 'HomeController@index', 'middleware' => ['web', 'auth:web']]);
Route::get('/home-api', ['uses' => 'HomeController@index', 'middleware' => ['api', 'auth:api']]);
The HomeController looks like
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{
public function index()
{
dd(Auth::user());
}
}
/home-index works as I expect, returns the logged in user, or null, if I'm not logged in
/home-web works as I expect, returns the logged in user, or redirect me to the login page
/home-api doesn't work as I expect:
- while it redirects me to the login page if I don't pass a
?api_token=xxxxx,
- if I'm using
/home-api?api_token=xxxxx it returns null (my users table has a column called api_token, and I'm using an existing token)
The only way I could retrieve the user with the corresponding api_token is to use dd(Auth::guard('api')->user());, which is not ideal.
So what do I miss here? Is there a more consolidated way to retrieve the authenticated user, without specifying the guard I'm using?
Thanks, Sebi
Hi,
I'm struggling with implementing the token based authentication scheme.
Using a clean Laravel 5.2 install and a
php artisan make:authI create the following test route:The
HomeControllerlooks like/home-indexworks as I expect, returns the logged in user, or null, if I'm not logged in/home-webworks as I expect, returns the logged in user, or redirect me to the login page/home-apidoesn't work as I expect:?api_token=xxxxx,/home-api?api_token=xxxxxit returnsnull(my users table has a column calledapi_token, and I'm using an existing token)The only way I could retrieve the user with the corresponding
api_tokenis to usedd(Auth::guard('api')->user());, which is not ideal.So what do I miss here? Is there a more consolidated way to retrieve the authenticated user, without specifying the guard I'm using?
Thanks, Sebi