Skip to content

Commit 42e7e2c

Browse files
authored
Replacement frontend engine with frontend type (#421)
* replacement frontend engine with frontend * rename util function to isMPAApplication
1 parent 556fa7f commit 42e7e2c

File tree

11 files changed

+62
-86
lines changed

11 files changed

+62
-86
lines changed

src/Http/Middleware/Billable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Billable
2727
*/
2828
public function handle(Request $request, Closure $next)
2929
{
30-
if (Util::useNativeAppBridge() === false) {
30+
if (!Util::isMPAApplication()) {
3131
throw new RuntimeException('You cannot use Billable middleware with SPA mode');
3232
}
3333

src/Http/Middleware/VerifyShopify.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function handle(Request $request, Closure $next)
9191
{
9292
// Verify the HMAC (if available)
9393
$hmacResult = $this->verifyHmac($request);
94+
9495
if ($hmacResult === false) {
9596
// Invalid HMAC
9697
throw new SignatureVerificationException('Unable to verify signature.');
@@ -101,6 +102,17 @@ public function handle(Request $request, Closure $next)
101102
return $next($request);
102103
}
103104

105+
if (!Util::isMPAApplication()) {
106+
$shop = $this->getShopIfAlreadyInstalled($request);
107+
$storeResult = !$this->isApiRequest($request) && $shop;
108+
109+
if ($storeResult) {
110+
$this->loginFromShop($shop);
111+
112+
return $next($request);
113+
}
114+
}
115+
104116
$tokenSource = $this->getAccessTokenFromRequest($request);
105117

106118
if ($tokenSource === null) {
@@ -113,17 +125,6 @@ public function handle(Request $request, Closure $next)
113125
throw new HttpException('Access denied.', Response::HTTP_FORBIDDEN);
114126
}
115127

116-
if (!Util::useNativeAppBridge()) {
117-
$shop = $this->getShopIfAlreadyInstalled($request);
118-
$storeResult = !$this->isApiRequest($request) && $shop;
119-
120-
if ($storeResult) {
121-
$this->loginFromShop($shop);
122-
123-
return $next($request);
124-
}
125-
}
126-
127128
//Check if there is a store record in the database
128129
return $this->checkPreviousInstallation($request)
129130
// Shop exists, token not available, we need to get one
@@ -217,10 +218,7 @@ protected function handleInvalidShop(Request $request)
217218
throw new HttpException('Shop is not installed or missing data.', Response::HTTP_FORBIDDEN);
218219
}
219220

220-
return $this->installRedirect(
221-
ShopDomain::fromRequest($request),
222-
$request->has('id_token') ? $request->query('id_token') : null
223-
);
221+
return $this->installRedirect(ShopDomain::fromRequest($request));
224222
}
225223

226224
/**
@@ -326,20 +324,11 @@ protected function tokenRedirect(Request $request): RedirectResponse
326324
* Redirect to install route.
327325
*
328326
* @param ShopDomainValue $shopDomain The shop domain.
329-
* @param string|null $token The session token (for Managed App Installation).
330327
*
331328
* @return RedirectResponse
332329
*/
333-
protected function installRedirect(ShopDomainValue $shopDomain, ?string $token): RedirectResponse
330+
protected function installRedirect(ShopDomainValue $shopDomain): RedirectResponse
334331
{
335-
if ($token !== null) {
336-
// Managed App Installation.
337-
return Redirect::route(
338-
Util::getShopifyConfig('route_names.authenticate'),
339-
['shop' => $shopDomain->toNative(), 'host' => request('host'), 'locale' => request('locale'), 'id_token' => $token]
340-
);
341-
}
342-
343332
return Redirect::route(
344333
Util::getShopifyConfig('route_names.authenticate'),
345334
['shop' => $shopDomain->toNative(), 'host' => request('host'), 'locale' => request('locale')]

src/Objects/Enums/FrontendEngine.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Objects/Enums/FrontendType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Objects\Enums;
4+
5+
use Funeralzone\ValueObjects\Enums\EnumTrait;
6+
use Funeralzone\ValueObjects\ValueObject;
7+
8+
class FrontendType implements ValueObject
9+
{
10+
use EnumTrait;
11+
12+
/**
13+
* @var int
14+
*/
15+
public const MPA = 0;
16+
17+
/**
18+
* @var int
19+
*/
20+
public const SPA = 1;
21+
}

src/Traits/BillingController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function process(
107107
'locale' => $request->get('locale'),
108108
];
109109

110-
if (!Util::useNativeAppBridge()) {
110+
if (!Util::isMPAApplication()) {
111111
$data['billing'] = $result ? 'success' : 'failure';
112112
}
113113

src/Util.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Support\Facades\Config;
77
use Illuminate\Support\Str;
88
use LogicException;
9-
use Osiset\ShopifyApp\Objects\Enums\FrontendEngine;
9+
use Osiset\ShopifyApp\Objects\Enums\FrontendType;
1010
use Osiset\ShopifyApp\Objects\Values\Hmac;
1111

1212
/**
@@ -182,9 +182,11 @@ public static function getShopifyConfig(string $key, $shop = null)
182182
}
183183

184184
// Check if config API callback is defined
185-
if (Str::startsWith($key, 'api')
185+
if (
186+
Str::startsWith($key, 'api')
186187
&& Arr::exists($config, 'config_api_callback')
187-
&& is_callable($config['config_api_callback'])) {
188+
&& is_callable($config['config_api_callback'])
189+
) {
188190
// It is, use this to get the config value
189191
return call_user_func(
190192
Arr::get($config, 'config_api_callback'),
@@ -237,14 +239,13 @@ public static function getShopsTableForeignKey(): string
237239
*
238240
* @return bool
239241
*/
240-
public static function useNativeAppBridge(): bool
242+
public static function isMPAApplication(): bool
241243
{
242-
$frontendEngine = FrontendEngine::fromNative(
243-
self::getShopifyConfig('frontend_engine') ?? 'BLADE'
244+
$frontendType = FrontendType::fromNative(
245+
self::getShopifyConfig('frontend_type') ?? 'MPA'
244246
);
245-
$reactEngine = FrontendEngine::fromNative('REACT');
246247

247-
return !$frontendEngine->isSame($reactEngine);
248+
return !$frontendType->isSame(FrontendType::fromNative('SPA'));
248249
}
249250

250251
public static function hasAppLegacySupport(string $feature): bool

src/resources/config/shopify-app.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,14 @@
577577

578578
/*
579579
|--------------------------------------------------------------------------
580-
| Frontend engine used
580+
| Frontend type used
581581
|--------------------------------------------------------------------------
582582
|
583-
| Available engines: "BLADE", "VUE", or "REACT".
584-
| For example, if you use React, you do not need to be redirected to a separate page to get the JWT token.
585-
| No changes are made for Vue.js and Blade.
583+
| Available types: "SPA" (single-page application), "MPA" (multiple-page application).
584+
| For example, if you use SPA, you do not need to be redirected to a separate page to get the JWT token.
586585
|
587586
*/
588-
'frontend_engine' => env('SHOPIFY_FRONTEND_ENGINE', 'BLADE'),
587+
'frontend_type' => env('SHOPIFY_FRONTEND_TYPE', 'MPA'),
589588

590589
'iframe_ancestors' => '',
591590

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</div>
2020
</div>
2121

22-
@if(\Osiset\ShopifyApp\Util::useNativeAppBridge())
22+
@if(\Osiset\ShopifyApp\Util::isMPAApplication())
2323
@include('shopify-app::partials.token_handler')
2424
@endif
2525
@yield('scripts')

tests/Http/Middleware/VerifyShopifyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public function testNotNativeAppBridgeWithTokenProcessingAndLoginShop(): void
324324
{
325325
// Create a shop that matches the token from buildToken
326326
factory($this->model)->create(['name' => 'shop-name.myshopify.com']);
327-
$this->app['config']->set('shopify-app.frontend_engine', 'REACT');
327+
$this->app['config']->set('shopify-app.frontend_type', 'SPA');
328328

329329
// Setup the request
330330
$currentRequest = Request::instance();

tests/Traits/BillingControllerTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testReactFrontendShopAcceptsBilling(): void
6060
'post_recurring_application_charges_activate',
6161
]);
6262

63-
config(['shopify-app.frontend_engine' => 'REACT']);
63+
config(['shopify-app.frontend_type' => 'SPA']);
6464

6565
// Create the shop and log them in
6666
$shop = factory($this->model)->create();
@@ -99,7 +99,7 @@ public function testBladeFrontendShopAcceptsBilling(): void
9999
'post_recurring_application_charges_activate',
100100
]);
101101

102-
config(['shopify-app.frontend_engine' => 'BLADE']);
102+
config(['shopify-app.frontend_type' => 'MPA']);
103103

104104
// Create the shop and log them in
105105
$shop = factory($this->model)->create();
@@ -267,7 +267,8 @@ public function testUsageChargeFailWithoutShopParam(): void
267267
$data = [
268268
'description' => 'One email',
269269
'price' => 1.00,
270-
'redirect' => 'https://localhost/usage-success',];
270+
'redirect' => 'https://localhost/usage-success',
271+
];
271272
$signature = Util::createHmac(['data' => $data, 'buildQuery' => true], $secret);
272273

273274
// Run the call

0 commit comments

Comments
 (0)