Kyon147 / laravel-shopify

A full-featured Laravel package for aiding in Shopify App development
MIT License
363 stars 107 forks source link

Auth user returns null on Inertia #316

Closed ignacio-dev closed 3 months ago

ignacio-dev commented 4 months ago

For bug reporting only! If you're posting a feature request or discussion, please ignore.

Expected Behavior

Being able to call $request->user() from HandleInertiaRequests 's share() method and get the Shopify shop.

Current Behavior

Returns null.

Failure Information

I can only get the $request->user() from my controllers.

Steps to Reproduce

  1. Call $request->user() from HandleInertiaRequests.

Context

Failure Logs

--

Kyon147 commented 3 months ago

Inertia is a community included feature, so someone would need to have a look at this as I've not used Interia so would not be the best person to understand what is needed here.

ignacio-dev commented 3 months ago

I think the issue is that, if you have any middleware appended to the routes from the bootstrap/app.php file like this:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ## HERE ##
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(append: [
            \App\Http\Middleware\HandleInertiaRequests::class,
            \Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

Those middlewares are running before the Shopify middlewares, so they don't have access to Shopify's auth user.

ignacio-dev commented 3 months ago

I'll try appending the shopify middleware from there instead of the routes file, and see if I have any success.

vincenzoraco commented 3 months ago

The issue is that the verify.shopify middleware is applied after HandleInertiaRequests is processed, and that's causes user to not be logged in. To fix this issue, you need to apply verify.shopify before Inertia middleware.

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(append: [
            'verify.shopify',
            \App\Http\Middleware\HandleInertiaRequests::class,
            \Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

Note: this will cause any routes in web to be authenticated. Most likely it's what you want, in other cases you might need to create a new routes file and exclude verify.shopify from the middleware lists.

ignacio-dev commented 3 months ago

Indeed, this solves the issue. Thank you!

monsefsolutions commented 1 month ago

Hello guys, I'm having the same issue in Laravel 11.x, but my bootstrap/app.php is different than what was mentioned in this thread:

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

return $app;

Middlewares are assigned in my app/Http/Kernel.php file, I tried to add the verify.shopify middleware (alias) before the HandleInertiaRequests::class like this:

protected $middlewareGroups = [
        'web' => [
           ...
           'verify.shopify',
            \App\Http\Middleware\HandleInertiaRequests::class,
           ...
        ],

I also tried to replace verify.shopify with \Osiset\ShopifyApp\Http\Middleware\VerifyShopify::class, but unfortunately none of the above worked for me. Any suggestions how to solve this within Laravel 11.x with this configuration?

Any help will be really appreciated!

Edit: My bad, after stopping the Laravel process and restarting it with php artisan serve the issue has been solved. For me, this is the working configuration in app/Http/Kernel.php:

protected $middlewareGroups = [
        'web' => [
           ...
           'verify.shopify',
            \App\Http\Middleware\HandleInertiaRequests::class,
           ...
        ],