Reputation: 61
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!!
Upvotes: 6
Views: 9149
Reputation: 1
this worked for me, i added this code in controller
public function __construct()
{
$this->middleware('auth:api');
}
then run this command: php artisan optimize:clear and get my Auth::user()
Upvotes: 0
Reputation: 1658
Add Below line in the controller,
public function __construct()
{
$this->middleware('auth:api');
}
And also, please try with below command,
php artisan optimize:clear
Upvotes: 1
Reputation: 196
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
Upvotes: 0
Reputation: 83
Edit your .htaccess file, may be there is some configuration issue.
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Upvotes: 0
Reputation: 83
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
Upvotes: 1
Reputation: 547
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.
Upvotes: 0
Reputation: 11
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
Upvotes: 1