简体   繁体   中英

Laravel - Passport API integration | Error: {“message”:“Unauthenticated.”}

I am new to Laravel . I have just started learning Laravel passport integration using this reference link .

Below is my route file api.php

Route::post('login', 'API\PassportController@login'); // this is working fine
Route::post('register', 'API\PassportController@register'); // this is working fine
Route::group(['middleware' => 'auth:api'], function(){
   Route::post('get-details' , 'API\PassportController@getDetails'); // This is giving me error
});

PassportController.php >> getDetails() function code,

/**
 * details api
 *
 * @return \Illuminate\Http\Response
 */
public function getDetails()
{
    $user = Auth::user();
    return response()->json(['success' => $user], $this->successStatus);
}

config/auth.php

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

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

I have updated rest of the files as mentioned in the article .

Registration and login APIs are working fine but when I try to run /get-details api, I am getting {"message":"Unauthenticated."} in response.

In the headers I am passing values like below, 在此处输入图片说明

Strange thing is that same code is working fine at my localhost server (OS- Windows) but when I run this API on my hosting server (OS- ubuntu) then it does not work.

Could anyone help me in resolving this issue? Is this related to server configuration settings? I am even not able to debug this code to check where I am doing wrong.

Thanks in advance!!

Did u pass the api token in header. If not , first you have to pass login details to api and get the api token. Then pass this token to the request , which ur trying to ac ess . Another possibility is u need to run the Passport install command again if ur environment change from local to live

Did you add Passport::routes(); in AuthServiceProvider

<?php

 namespace App\Providers;

 use Laravel\Passport\Passport;
 use Illuminate\Support\Facades\Gate;
 use Illuminate\Foundation\Support\Providers\AuthServiceProvider as 
 ServiceProvider;

 class AuthServiceProvider extends ServiceProvider
 {
/**
 * The policy mappings for the application.
 *
 * @var array
 */
protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy',
];

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
  public function boot()
  {
    $this->registerPolicies();
    Passport::routes();
    Passport::tokensExpireIn(now()->addDays(15));
    //Passport::refreshTokensExpireIn(now()->addDays(30));
    //
   }
}

Please refer this link https://medium.com/techcompose/create-rest-api-in-laravel-with-authentication-using-passport-133a1678a876

Add Below line in the controller,

public function __construct()
    {
        $this->middleware('auth:api');
    }

And also, please try with below command,

php artisan optimize:clear

auth:api middleware is expecting the request to come in via JSON.

It's looking for X-Requested-With : XMLHttpRequest in the incoming request headers. When it can't find it, it falls into the unauthenticated method in app/Exceptions/Handler , and returns an error.

If you add X-Requested-With : XMLHttpRequest to your request headers, it should resolve the issue.

Edit your .htaccess file, may be there is some configuration issue.

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

try installing the package laravel-cors and adding the Authorization header in the allowedHeaders option provided by the package.

'allowedHeaders' => ['Authorization']

May be that can solve the header not sending problem.

or you may need to change the apache setting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM