Skip to content

Commit ac6ab8a

Browse files
committed
Cleaned up some logic for AuthShopMiddleware
1 parent f74fe33 commit ac6ab8a

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/ShopifyApp/Middleware/AuthShop.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace OhMyBrew\ShopifyApp\Middleware;
44

55
use Closure;
6-
use Illuminate\Http\JsonResponse;
7-
use Illuminate\Http\RedirectResponse;
86
use Illuminate\Http\Request;
97
use Illuminate\Http\Response;
8+
use Illuminate\Http\JsonResponse;
9+
use Illuminate\Http\RedirectResponse;
1010
use Illuminate\Support\Facades\Config;
11-
use Illuminate\Support\Facades\Redirect;
1211
use Illuminate\Support\Facades\Session;
12+
use Illuminate\Support\Facades\Redirect;
1313
use 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;

tests/Middleware/AuthShopMiddlewareTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,14 @@ public function testHeadersForDisabledEsdk()
137137
$shop = factory(Shop::class)->create();
138138
Session::put('shopify_domain', $shop->shopify_domain);
139139

140-
// Disable ESDL
140+
// Disable ESDK
141141
Config::set('shopify-app.esdk_enabled', false);
142142

143143
// Run the middleware
144144
$result = $this->runAuthShop();
145145

146146
// Assert the headers were not modified
147-
$this->assertNull($result[0]->headers->get('p3p'));
148-
$this->assertNull($result[0]->headers->get('x-frame-options'));
147+
$this->assertFalse(is_object($result[0]));
149148
}
150149

151150
public function testShouldSaveReturnUrl()

0 commit comments

Comments
 (0)