Kyon147 / laravel-shopify

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

Attempt to read property "name" on null #133

Closed Kungpowpow closed 1 year ago

Kungpowpow commented 1 year ago

Expected Behavior

Load the route & view from a submitted form.

Current Behavior

I'm trying to submit a form via GET, and the page errors out after reload. The Auth::user() is null when the page is loaded after form submit, however the Auth::user() works completely fine on first page load. The @sessionToken works fine on first load, and is empty after submission.

Ideally my goal is to submit to a controller via POST so I can validate the form. "return Redirect::tokenRedirect('apply');" was giving me similar errors with that method. My code shared below is an attempt to reduce this to a simpler recreateable format.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

Route::match(['get','post'],'apply', function(){
//    $shop = Auth::user();
    return view('test');
})->middleware(['verify.shopify'])->name('application.submit');
@extends('shopify-app::layouts.default')

@section('content')
    <form id="apply-form" method="get" action="{{ URL::tokenRoute('application.submit') }}" >
        @sessionToken
        <button type="submit">submit</button>
    </form>

@endsection

Context

Failure Logs

https://flareapp.io/share/4m4xOwBm#F66

Kyon147 commented 1 year ago

@Kungpowpow

I can't be 100% sure without trying it replicate it but what does your test method look like? I can also see that host is not being pass through either which will also cause issues.

Kyon147 commented 1 year ago

@Kungpowpow are you still finding this issue?

Kyon147 commented 1 year ago

Closing issue as it looks stale, re-open if issue persists.

tom-rice commented 12 months ago

Hi @Kungpowpow did you ever find a resolution for submitting non-SPA forms?

Normal links work perfectly fine multiple times over (even if I add GET parameters to them e.g. <a href="{{ URL::tokenRoute('home', ['test' => 1]) }}">) but I tried 4 different ways of submitting a form and even when I got it to submit successfully I always lose the session token on the resulting pageload.

  1. If I use method=POST, action={{ URL::tokenRoute('myroute') }} I get the error The POST method is not supported for route authenticate/token
  2. If I use method=GET, action={{ URL::tokenRoute('myroute') }}, I immediately get the Attempt to read property "name" on null error*
  3. If I use method=POST, action={{ route('myroute') }}, it works but after submitting it the sessionToken is blank, so any further links I click on result in the Attempt to read property "name" on null error*, or if I submit the form again I get No authenticated user or shop domain
  4. If I use method=GET, action={{ route('myroute') }}, it works but after submitting it the sessionToken is blank, so any further links I click on result in the Attempt to read property "name" on null error*, or if I submit the form again I get No authenticated user or shop domain

Is there something else I'm missing in the form other than including @sessionToken?

* error comes from default.blade.php where Auth::user() is null: apiKey: "{{ \Osiset\ShopifyApp\Util::getShopifyConfig('api_key', $shopDomain ?? Auth::user()->name ) }}",

Kungpowpow commented 12 months ago

Unfortunately I couldn't get it working. I switched to react for making my app and couldn't continue testing this.

tom-rice commented 12 months ago

@Kungpowpow Ah ok, thanks for letting me know. I seem to have almost been able to work around this now by explicitly passing $request->input('host') to my view and then passing it on with <input type="hidden" name="host" value="{{ $host }}">

Then I can use either GET or POST with action={{ route('myroute') }} (not tokenRoute) and do so over and over long as I'm passing along the 'host' value each time it will still have the session token on the next page etc.

EDIT: However, after the first form submit, any regular links are still broken, so although I can use the link multiple times and then submit the form multiple times, as soon as I've submitted the form the link stops working. e.g. After I submit form and then click <a href="{{ URL::tokenRoute('home', ['host'=>$host]) }}">Link</a> (with or without including host) I get Attempt to read property "name" on null. Very strange... putting this down for today, I'll post back if I find a way to make form submits AND links work!

Using this fix instead of passing the $host value myself also leaves me in the same predicament: https://github.com/Kyon147/laravel-shopify/pull/194

Perhaps this is related to issue #200 @Kyon147 ? Although I tried applying this change and it didn't make any difference for me for this.

Kyon147 commented 12 months ago

@tom-rice it sounds like you are not getting the host param consistently. This is a known thing for blade templates and you have to pass the host param on every request or page change for blade.

I'd use xDebug or similar to step through your code and see where the issue stems from and why by looking at the data available at that point in the app. There's too many variables from each app, to know from such a generic error of the user not being defined.

tom-rice commented 12 months ago

Thank you @Kyon147 I have now fixed the last issue I was having by changing same_site from lax to none in config/session.php - thanks to this comment you posted in issue #82

So the whole solution was 2 things: 1) Used the change in this pull request to always include the host parameter in @sessionToken output rather than passing it through the form myself: https://github.com/Kyon147/laravel-shopify/pull/194 Could that pull request be included in a future release?

2) Set "same_site": "none" in config/session.php Perhaps this could be explicitly noted in the installation or routing wiki, unless it's mentioned somewhere already and I missed it?

Now I can POST to {{ route('myroute') }} multiple times and continue to click on tokenRoute URLs without issue 😃

Kyon147 commented 12 months ago

@tom-rice glad you got it working. The PR i've just merged into main, now that the pipeline has passed - so I'll get an official patch out this week for it.

Glad that my cookie investigation has come in use. I don't think i've added it to the wiki yet which is a good suggestion as it will affect mainly Blade template uses, because they rely on the session state whereas SPA uses, typically use a JS based state like VueX etc.

Kyon147 commented 12 months ago

I've added some blade specific support notes to the installation section, this should reduce issues popping up for set up too hopefully.

https://github.com/Kyon147/laravel-shopify/wiki/Installation#blade-specific-support