Laravel V4.1.0: 401 code "message": "Unauthenticated." for API

Viewed 310

I am facing a problem with calling Laravel API when not using php artisan serve I have used Passport for authentication

When I use php artisan serve and login using http://127.0.0.1:8000/api/login it works fine and when I take the token and fetch data using http://127.0.0.1:8000/api/getData it works fine also.

But, I want to make it run without php artisan serve. When I tried that:

login using http://127.0.0.1/Laravel/MyApp/public/index.php/api/login works fine and when I take the token and fetch data using http://127.0.0.1/Laravel/MyApp/public/index.php/api/getData it gives 401 code - unauthorized.

I am using AppServ : Apache.

I searched SOF and tried all suggested solutions with no luck.

api.php

Route::post('/login','App\Http\Controllers\ApiAuthController@login')->name('login');
Route::post('register', 'App\Http\Controllers\ApiAuthController@register');

Route::group(['middleware'=>'auth:api','namespace'=>'App\Http\Controllers'], function () {
    Route::post('getData', 'DataController@index');
});

User:

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
..........
}

kernel.php

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,

             \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,

            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

gurads:

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

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

my .htaccess inside public dir:

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} ^(.+)$ 
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

I have tried to copy public directory data to the root folder and editing the .htaccess . Also I tried creating new .htaccess in the root folder with and without removing .htaccess in the public folder with no luck also.

This is the Login: Login

This is php artisan serve result and it works fine php artisan serve result

This is when not using php artisan serve when not using php artisan serve

Thanks

0 Answers
Related