Home > Net >  Laravel Passport API return Unauthenticated
Laravel Passport API return Unauthenticated

Time:09-22

I am trying to authenticate api calls in my laravel application. I have installed Passport following the documentation and I think I have not missed anything. but API call returns a 401 Unauthenticated.

Auth.php

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

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
        'hash' => false,
    ],
],

AuthServiceProvider.php

 public function boot() {

    $this->registerPolicies();
    Passport::tokensExpireIn(now()->addMinutes(config('auth.token_expiration.token')));
    Passport::refreshTokensExpireIn(now()->addMinutes(config('auth.token_expiration.refresh_token')));

}

RouteServiceProvider

    $this->configureRateLimiting();

    $this->routes(function () {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));

        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    });

header

Header

and .htaccess

 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

CodePudding user response:

Add this code to .htaccess of root folder (not only inside the public folder)

RewriteCond %{HTTP:Authorization} ^(. )$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

CodePudding user response:

It looks like you need to register the necessary routes to issue tokens. Simply, you call Passport::routes method within the boot method.

 public function boot() {

    $this->registerPolicies();
    Passport::routes();
    Passport::tokensExpireIn(now()->addMinutes(config('auth.token_expiration.token')));
    Passport::refreshTokensExpireIn(now()->addMinutes(config('auth.token_expiration.refresh_token')));

}

If you have any problems, tell me.

  • Related