Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit f07ccce

Browse files
enmaboyagnikyt
andauthored
Verify shopify with SPA (#1173)
* update config, add spa variable * Continue if app already installed and has access token and not deleted * disable cdn app bridge if spa used * update verify * add util helper * update validation * add frontend engine enum * update frontend validation * add frontend_engine config setting * linter fix * add tests * simplified verification * Fixes issue comparing enum for `useNativeAppBridge` utility method * Changes test to ensure all tests use app config over Config::set for consistency * Changes test to ensure all tests use app config over Config::set for consistency * Changes enum/config setup for app bridge configuration to be string based * Fixes to solve undefined magic method for frontend engine Co-authored-by: Tyler King <tyler@osiset.com>
1 parent f4b4a01 commit f07ccce

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

src/Http/Middleware/VerifyShopify.php

+9
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,16 @@ public function handle(Request $request, Closure $next)
101101
return $next($request);
102102
}
103103

104+
if (!Util::useNativeAppBridge()) {
105+
$storeResult = !$this->isApiRequest($request) && $this->checkPreviousInstallation($request);
106+
107+
if ($storeResult) {
108+
return $next($request);
109+
}
110+
}
111+
104112
$tokenSource = $this->getAccessTokenFromRequest($request);
113+
105114
if ($tokenSource === null) {
106115
//Check if there is a store record in the database
107116
return $this->checkPreviousInstallation($request)

src/Objects/Enums/FrontendEngine.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Objects\Enums;
4+
5+
use Funeralzone\ValueObjects\Enums\EnumTrait;
6+
use Funeralzone\ValueObjects\ValueObject;
7+
8+
/**
9+
* Online Store 2.0 theme support
10+
*/
11+
class FrontendEngine implements ValueObject
12+
{
13+
use EnumTrait;
14+
15+
/**
16+
* Laravel Blade
17+
*
18+
* @var int
19+
*/
20+
public const BLADE = 0;
21+
22+
/**
23+
* Vue.js
24+
*
25+
* @var int
26+
*/
27+
public const VUE = 1;
28+
29+
/**
30+
* React
31+
*
32+
* @var int
33+
*/
34+
public const REACT = 2;
35+
}

src/Util.php

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Config;
77
use Illuminate\Support\Str;
88
use LogicException;
9+
use Osiset\ShopifyApp\Objects\Enums\FrontendEngine;
910
use Osiset\ShopifyApp\Objects\Values\Hmac;
1011

1112
/**
@@ -230,4 +231,19 @@ public static function getShopsTableForeignKey(): string
230231
{
231232
return Str::singular(self::getShopsTable()).'_id';
232233
}
234+
235+
/**
236+
* Checking to see if you need to use the native App Bridge
237+
*
238+
* @return bool
239+
*/
240+
public static function useNativeAppBridge(): bool
241+
{
242+
$frontendEngine = FrontendEngine::fromNative(
243+
self::getShopifyConfig('frontend_engine') ?? 'BLADE'
244+
);
245+
$reactEngine = FrontendEngine::fromNative('REACT');
246+
247+
return !$frontendEngine->isSame($reactEngine);
248+
}
233249
}

src/resources/config/shopify-app.php

+12
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,16 @@
478478
],
479479

480480
'session_token_refresh_interval' => env('SESSION_TOKEN_REFRESH_INTERVAL', 2000),
481+
482+
/*
483+
|--------------------------------------------------------------------------
484+
| Frontend engine used
485+
|--------------------------------------------------------------------------
486+
|
487+
| Available engines: "BLADE", "VUE", or "REACT".
488+
| For example, if you use React, you do not need to be redirected to a separate page to get the JWT token.
489+
| No changes are made for Vue.js and Blade.
490+
|
491+
*/
492+
'frontend_engine' => env('SHOPIFY_FRONTEND_ENGINE', 'BLADE'),
481493
];

src/resources/views/layouts/default.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</div>
1818
</div>
1919

20-
@if(\Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_enabled'))
20+
@if(\Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_enabled') && \Osiset\ShopifyApp\Util::useNativeAppBridge())
2121
<script src="https://unpkg.com/@shopify/app-bridge{{ \Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_version') ? '@'.config('shopify-app.appbridge_version') : '' }}"></script>
2222
<script src="https://unpkg.com/@shopify/app-bridge-utils{{ \Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_version') ? '@'.config('shopify-app.appbridge_version') : '' }}"></script>
2323
<script

tests/UtilTest.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testRouteNames(): void
6868

6969
public function testGetShopifyConfig(): void
7070
{
71-
Config::set('shopify-app.config_api_callback', function (string $key, $shop) {
71+
$this->app['config']->set('shopify-app.config_api_callback', function (string $key, $shop) {
7272
if ($key === 'api_secret') {
7373
return 'hello world';
7474
}
@@ -105,4 +105,22 @@ public function testGraphQLWebhookTopic(): void
105105
Util::getGraphQLWebhookTopic('ORDERS_PARTIALLY_FULFILLED')
106106
);
107107
}
108+
109+
public function testUseNativeAppBridgeIsTrue(): void
110+
{
111+
$this->app['config']->set('shopify-app.frontend_engine', 'VUE');
112+
113+
$result = Util::useNativeAppBridge();
114+
115+
$this->assertTrue($result);
116+
}
117+
118+
public function testUseNativeAppBridgeIsFalse(): void
119+
{
120+
$this->app['config']->set('shopify-app.frontend_engine', 'REACT');
121+
122+
$result = Util::useNativeAppBridge();
123+
124+
$this->assertFalse($result);
125+
}
108126
}

0 commit comments

Comments
 (0)