Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, enter image description here

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!!

like image 520
Jchauhan Avatar asked May 30 '18 06:05

Jchauhan


1 Answers

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 u r trying to ac ess . Another possibility is u need to run the Passport install command again if ur environment change from local to live

like image 155
Mohamed Nafil Avatar answered Oct 02 '22 10:10

Mohamed Nafil