dimanche 4 mars 2018

Laravel 5.1 dont receive JWT token from Dingo API

I use Laravel 5.1 + Dingo API + JWT token. My route is:

$api = app('Dingo\Api\Routing\Router');

$api->version('v1', function($api) {

$api->post('authenticate', 'App\Http\Controllers\AuthenticateController@authenticate');
$api->post('logout', 'App\Http\Controllers\AuthenticateController@logout');
$api->get('token', 'App\Http\Controllers\AuthenticateController@getToken');
$api->get('test', 'App\Http\Controllers\BitemsController@test');

});

$api->version('v1', ['middleware' => 'api.auth'], function ($api) {

    $api->get('authenticated_user', 'App\Http\Controllers\AuthenticateController@authenticatedUser');
    $api->get('getvoucher', 'App\Http\Controllers\BitemsController@index');
    $api->post('/store', 'App\Http\Controllers\BitemsController@store');
    $api->put('/update/{key}', 'App\Http\Controllers\BitemsController@update');

});

Now when I run at Postman to get token everything is fine, so I get:

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDIsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIwMjAxMzYwLCJleHAiOjE1MjAyMDQ5NjAsIm5iZiI6MTUyMDIwMTM2MCwianRpIjoiNlhTZWdPRllpaGZNWk5sSSJ9.f71ReEY_3I2-uj-7PJNBxbHhO8C50XP5kbShrnmZ8Ig"
}

but now I want to retrieve auth user and I have function protected by middleware, but I get:

{
    "message": "Failed to authenticate because of bad credentials or an invalid authorization header.",
    "status_code": 401,
    "debug": {
        "line": 113,
        "file": "/home/dgadmin/public_html/test/vendor/dingo/api/src/Auth/Auth.php",
        "class": "Symfony\\Component\\HttpKernel\\Exception\\UnauthorizedHttpException", ... ETC...

What is a problem here and how to solve this issue?

Interesting when I make the call like :

https://app.example.com/api/authenticated_user?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDIsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIwMjAxMzYwLCJleHAiOjE1MjAyMDQ5NjAsIm5iZiI6MTUyMDIwMTM2MCwianRpIjoiNlhTZWdPRllpaGZNWk5sSSJ9.f71ReEY_3I2-uj-7PJNBxbHhO8C50XP5kbShrnmZ8Ig

then everything is fine... but when I send token as Authorization header with Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDIsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTIwMjAxMzYwLCJleHAiOjE1MjAyMDQ5NjAsIm5iZiI6MTUyMDIwMTM2MCwianRpIjoiNlhTZWdPRllpaGZNWk5sSSJ9.f71ReEY_3I2-uj-7PJNBxbHhO8C50XP5kbShrnmZ8Ig then I got the 401 error from above...

Why Laravel won't accept authorization header with my token?

I try to add .htaccess file:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On



    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^ - [E=HTTPS_AUTHORIZATION:%{HTTPS:Authorization}]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]




</IfModule>

as you can see I add RewriteRule ^ - [E=HTTPS_AUTHORIZATION:%{HTTPS:Authorization}] but nothing happened

How to solve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire