33namespace OhMyBrew \ShopifyApp \Middleware ;
44
55use Closure ;
6- use Illuminate \Http \JsonResponse ;
7- use Illuminate \Http \RedirectResponse ;
86use Illuminate \Http \Request ;
97use Illuminate \Http \Response ;
8+ use Illuminate \Http \JsonResponse ;
9+ use Illuminate \Http \RedirectResponse ;
1010use Illuminate \Support \Facades \Config ;
11- use Illuminate \Support \Facades \Redirect ;
1211use Illuminate \Support \Facades \Session ;
12+ use Illuminate \Support \Facades \Redirect ;
1313use OhMyBrew \ShopifyApp \Facades \ShopifyApp ;
14+ use Symfony \Component \HttpFoundation \Response as BaseResponse ;
1415
1516/**
1617 * Response for ensuring an authenticated shop.
@@ -33,9 +34,9 @@ public function handle(Request $request, Closure $next)
3334 // Check if shop has a session, also check the shops to ensure a match
3435 if (
3536 $ shop === null ||
36- ( $ shopParam && $ shopParam !== $ shop ->shopify_domain ) === true ||
37+ $ shop ->trashed () ||
3738 empty ($ shop ->shopify_token ) ||
38- $ shop ->trashed ()
39+ ( $ shopParam && $ shopParam !== $ shop ->shopify_domain ) === true
3940 ) {
4041 // Either no shop session or shops do not match
4142 Session::forget ('shopify_domain ' );
@@ -46,20 +47,20 @@ public function handle(Request $request, Closure $next)
4647 return Redirect::route ('authenticate ' , ['shop ' => $ shopParam ]);
4748 }
4849
49- // Shop is OK, move on ...
50+ // Shop is OK, now check if ESDK is enabled and this is not a JSON/AJAX request ...
5051 $ response = $ next ($ request );
51- if (($ request ->ajax () || $ request ->wantsJson () || $ request ->isJson ()) === false ) {
52- // Request is not AJAX, continue as normal
53- if (!$ response instanceof Response && !$ response instanceof RedirectResponse && !$ response instanceof JsonResponse) {
54- // We need a response object to modify headers
52+ if (
53+ Config::get ('shopify-app.esdk_enabled ' ) &&
54+ ($ request ->ajax () || $ request ->expectsJson () || $ request ->isJson ()) === false
55+ ) {
56+ if (($ response instanceof BaseResponse) === false ) {
57+ // Not an instance of a Symfony response, override
5558 $ response = new Response ($ response );
5659 }
5760
58- if (Config::get ('shopify-app.esdk_enabled ' )) {
59- // Headers applicable to ESDK only
60- $ response ->headers ->set ('P3P ' , 'CP="Not used" ' );
61- $ response ->headers ->remove ('X-Frame-Options ' );
62- }
61+ // Attempt to modify headers applicable to ESDK (does not work in all cases)
62+ $ response ->headers ->set ('P3P ' , 'CP="Not used" ' );
63+ $ response ->headers ->remove ('X-Frame-Options ' );
6364 }
6465
6566 return $ response ;
0 commit comments