Skip to content

Illuminate\Auth\TokenGuard Auth:user() is null #11782

@sebestenyb

Description

@sebestenyb

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());
    }
}
  1. /home-index works as I expect, returns the logged in user, or null, if I'm not logged in
  2. /home-web works as I expect, returns the logged in user, or redirect me to the login page
  3. /home-api doesn't work as I expect:
    1. while it redirects me to the login page if I don't pass a ?api_token=xxxxx,
    2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions