Support the ongoing development of Laravel.io →
posted 7 years ago

I have followed the quickstart here for Tymon JWT and I can login okay (that route isn't protected) but when I try to do anything that is protected by middleware I get unauthenticated error. So I log in with a user and get a token back, but then when I try to use that token to do anything else I get unathenticated.

composer.json:

    "require": {
        "php": ">=7.0.0",
        "barryvdh/laravel-cors": "^0.9.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.5.*",
        "tymon/jwt-auth": "dev-develop"
    },

routes/api.php:


    Route::group([
	    'middleware' => 'api',
	    'prefix' => 'auth'
    ], function($router) {
	    Route::post('login', 'AuthController@login');
	    Route::post('logout', 'AuthController@logout');
	    Route::post('refresh', 'AuthController@refresh');
	    Route::get('me', 'AuthController@me');
	    Route::get('test', 'AuthController@test');
    });

config/auth.php:


    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

    'guards' => [
	'web' => [
    	    'driver' => 'session',
	    'provider' => 'users',
	],	
        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
    ],

routes:

GET|HEAD / Closure web

POST api/auth/login App\Http\Controllers\AuthController@login api

POST api/auth/logout App\Http\Controllers\AuthController@logout api,auth:api

GET|HEAD api/auth/me App\Http\Controllers\AuthController@me api,auth:api

POST api/auth/refresh App\Http\Controllers\AuthController@refresh api,auth:api

AuthController.php:


    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }

    public function login()
    {
        $credentials = request(['email', 'password']);

        if (! $token = auth()->attempt($credentials)) { 
	  return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->respondWithToken($token);
    }

    public function me()
    {
        return response()->json(auth()->user());
    }

    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
	    'morale' => 'keep goin, you will get there',
            'token_type' => 'bearer',
            'expires_in' => auth()->factory()->getTTL() * 60
        ]);
    }

So yeah. I have no idea what the problem is. User gets token but can't use it. Been beating my head against this for four days now. Have a working laravel rest api but it was version 4 something and I thought it might be nice to upgrade it. Starting to think I should just stick with what works :/

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.