Support the ongoing development of Laravel.io →
Passport
Last updated by @amitleuva1987 2 years ago.
0

Mostly, this error occurs when a route protected by a middleware is being accessed by an unauthorized/unauthenticated resource. We think you should check if the jwt token is valid by removing the auth:api middleware and replacing it with this:

return response()->json([ 'valid' => auth()->check() ]);

if that doesn't work, you may give us your API route code and we can check what went wrong.

0

I tried below stuff.

// Testcase.php
Passport::actingAsClient(
            Client::factory()->create(),
            ['check-status']
);

$response = $this->get('/test_jwt');
dd($response);
$response->assertStatus(200);

// api.php

Route::get('/test_jwt', function () {
    return response()->json([ 'valid' => auth()->check()]);
});

The value of the 'valid' is false.

 #content: "{"valid":false}"
    #version: "1.1"
    #statusCode: 200
    #statusText: "OK"
    #charset: null
    +original: array:1 [
      "valid" => false
    ]
Last updated 2 years ago.
0

As per your code, it is not checking the auth on that URL

You can call the middleware(auth:api)

it will check if auth is there or not (if the user is not logged in, it will return false)

In route api.php

Route::middleware('auth:api')->get('/test_jwt', function (Request $request) {
    return response()->json([ 'valid' => auth()->check() ]);
});
0

I tried as you said. Guarded the route with 'auth:api'

Route::middleware('auth:api')->group(function () {
        Route::get('/test_jwt', function () {
            return response()->json([ 'valid' => auth()->check()]);
        });
});

and I receive 500 server error. Below is the server error log trace.

testing.ERROR: Route [login] not defined. {"exception":"[object] (Symfony\Component\Routing\Exception\RouteNotFoundException(code: 0): Route [login] not defined. at C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php:444) [stacktrace] #0 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(739): Illuminate\Routing\UrlGenerator->route('login', Array, true) #1 C:\xampp1\htdocs\solution_12\app\Http\Middleware\Authenticate.php(18): route('login') #2 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php(83): App\Http\Middleware\Authenticate->redirectTo(Object(Illuminate\Http\Request)) #3 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php(68): Illuminate\Auth\Middleware\Authenticate->unauthenticated(Object(Illuminate\Http\Request), Array) #4 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php(42): Illuminate\Auth\Middleware\Authenticate->authenticate(Object(Illuminate\Http\Request), Array) #5 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'api') #6 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #7 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Routing\Router.php(723): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #8 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Routing\Router.php(698): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request)) #9 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Routing\Router.php(662): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route)) #10 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Routing\Router.php(651): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) #11 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(167): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) #12 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request)) #13 C:\xampp1\htdocs\solution_12\vendor\barryvdh\laravel-debugbar\src\Middleware\InjectDebugbar.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #14 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Barryvdh\Debugbar\Middleware\InjectDebugbar->handle(Object(Illuminate\Http\Request), Object(Closure)) #15 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #16 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php(31): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure)) #17 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull->handle(Object(Illuminate\Http\Request), Object(Closure)) #18 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #19 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php(40): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure)) #20 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TrimStrings->handle(Object(Illuminate\Http\Request), Object(Closure)) #21 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #22 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure)) #23 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #24 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure)) #25 C:\xampp1\htdocs\solution_12\vendor\fruitcake\laravel-cors\src\HandleCors.php(52): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #26 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure)) #27 C:\xampp1\htdocs\solution_12\vendor\fideloper\proxy\src\TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #28 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure)) #29 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #30 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(142): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #31 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(111): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) #32 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php(510): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) #33 C:\xampp1\htdocs\solution_12\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php(294): Illuminate\Foundation\Testing\TestCase->call('GET', '/test_jwt', Array, Array, Array, Array) #34 C:\xampp1\htdocs\solution_12\tests\php\Unit\SkillTest.php(32): Illuminate\Foundation\Testing\TestCase->get('/test_jwt') #35 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\Framework\TestCase.php(1545): Tests\Php\Unit\SkillTest->test_can_retrive_all_skill_data() #36 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\Framework\TestCase.php(1151): PHPUnit\Framework\TestCase->runTest() #37 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\Framework\TestResult.php(726): PHPUnit\Framework\TestCase->runBare() #38 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\Framework\TestCase.php(903): PHPUnit\Framework\TestResult->run(Object(Tests\Php\Unit\SkillTest)) #39 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\Framework\TestSuite.php(670): PHPUnit\Framework\TestCase->run(Object(PHPUnit\Framework\TestResult)) #40 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\Framework\TestSuite.php(670): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #41 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\Framework\TestSuite.php(670): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #42 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\TextUI\TestRunner.php(673): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #43 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\TextUI\Command.php(143): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\Framework\TestSuite), Array, Array, true) #44 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\src\TextUI\Command.php(96): PHPUnit\TextUI\Command->run(Array, true) #45 C:\xampp1\htdocs\solution_12\vendor\phpunit\phpunit\phpunit(98): PHPUnit\TextUI\Command::main() #46 {main} "}

0

can you show you api.php code?

did you declare auth in your route ?

0

below is my whole api.php

use App\Http\Controllers\api\Auth\LogoutController;
use App\Http\Controllers\api\Auth\RegisterController;
use App\Http\Controllers\api\UserController as V1UserController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\EducationController;
use App\Http\Controllers\ExperienceController;
use App\Http\Controllers\HobbyController;
use App\Http\Controllers\LanguageController;
use App\Http\Controllers\SkillController;
use App\Http\Controllers\UserController;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::get('/', [Controller::class, 'routes'])
    ->name('route information')
    ->withoutMiddleware('api');
Route::get('/example', [Controller::class, 'example'])->name('example route');
Route::get('/error', [Controller::class, 'error'])->name('error route');

Route::middleware('auth:api')->group(function () {
    Route::resource('/skills', SkillController::class)->except([
    'create', 'edit'
    ]);
    Route::resource('/experiences', ExperienceController::class)->except([
    'create', 'edit'
    ]);
    Route::resource('/educations', EducationController::class)->except([
    'create', 'edit'
    ]);
    Route::resource('/languages', LanguageController::class)->except([
    'create', 'edit'
    ]);
    Route::resource('/hobbies', HobbyController::class)->except([
    'create', 'edit'
    ]);
});

Route::prefix('api/v1')->group(function () {
    Route::post('logout', [LogoutController::class, 'logout']);
    Route::post('register', [RegisterController::class, 'register']);
    Route::get('user', [V1UserController::class, 'show']);
    Route::patch('user', [V1UserController::class, 'update']);
    Route::delete('user', [V1UserController::class, 'destroy']);
});

Route::middleware('auth:api')->get('/test_jwt', function () {
    return response()->json([ 'valid' => auth()->check() ]);
});
0

@skynet-technologies , Can you see anything wrong in the "api.php"?

0

@amitleuva1987 It seems like you don't have login route over there so you will have to add those route.

0
Passport::actingAsClient(
            Client::factory()->create(),
);

by using above code, I guess, a user will be logged in as a client. If the user is already logged in as client, why would it require login route? (I am not sure, but this looks a bit logical)

This is the url to one of the git repository where laravel passport has been used and I don't see any login route defined in laravel api.php or nuxt.config.js.

0

When we use laravel passport, all auth routes are being defined using below code in authserviceprovider.

if (! $this->app->routesAreCached()) {
            Passport::routes();
}

which generates 'oauth/authorize ' route and it is used for login. I don't think, we need to define login route separately in 'api.php'.

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.

© 2024 Laravel.io - All rights reserved.