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

Verify shopify with SPA #1173

Merged
merged 18 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Http/Middleware/VerifyShopify.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,16 @@ public function handle(Request $request, Closure $next)
return $next($request);
}

if (!Util::useNativeAppBridge()) {
$storeResult = !$this->isApiRequest($request) && $this->checkPreviousInstallation($request);

if ($storeResult) {
return $next($request);
}
}

$tokenSource = $this->getAccessTokenFromRequest($request);

if ($tokenSource === null) {
//Check if there is a store record in the database
return $this->checkPreviousInstallation($request)
Expand Down
35 changes: 35 additions & 0 deletions src/Objects/Enums/FrontendEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Osiset\ShopifyApp\Objects\Enums;

use Funeralzone\ValueObjects\Enums\EnumTrait;
use Funeralzone\ValueObjects\ValueObject;

/**
* Online Store 2.0 theme support
*/
class FrontendEngine implements ValueObject
{
use EnumTrait;

/**
* Laravel Blade
*
* @var int
*/
public const BLADE = 0;

/**
* Vue.js
*
* @var int
*/
public const VUE = 1;

/**
* React
*
* @var int
*/
public const REACT = 2;
}
16 changes: 16 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use LogicException;
use Osiset\ShopifyApp\Objects\Enums\FrontendEngine;
use Osiset\ShopifyApp\Objects\Values\Hmac;

/**
Expand Down Expand Up @@ -230,4 +231,19 @@ public static function getShopsTableForeignKey(): string
{
return Str::singular(self::getShopsTable()).'_id';
}

/**
* Checking to see if you need to use the native App Bridge
*
* @return bool
*/
public static function useNativeAppBridge(): bool
{
$frontendEngine = FrontendEngine::fromNative(
self::getShopifyConfig('frontend_engine') ?? 'BLADE'
);
$reactEngine = FrontendEngine::fromNative('REACT');

return !$frontendEngine->isSame($reactEngine);
}
}
12 changes: 12 additions & 0 deletions src/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,16 @@
],

'session_token_refresh_interval' => env('SESSION_TOKEN_REFRESH_INTERVAL', 2000),

/*
|--------------------------------------------------------------------------
| Frontend engine used
|--------------------------------------------------------------------------
|
| Available engines: "BLADE", "VUE", or "REACT".
| For example, if you use React, you do not need to be redirected to a separate page to get the JWT token.
| No changes are made for Vue.js and Blade.
|
*/
'frontend_engine' => env('SHOPIFY_FRONTEND_ENGINE', 'BLADE'),
];
2 changes: 1 addition & 1 deletion src/resources/views/layouts/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
</div>

@if(\Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_enabled'))
@if(\Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_enabled') && \Osiset\ShopifyApp\Util::useNativeAppBridge())
<script src="https://unpkg.com/@shopify/app-bridge{{ \Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_version') ? '@'.config('shopify-app.appbridge_version') : '' }}"></script>
<script src="https://unpkg.com/@shopify/app-bridge-utils{{ \Osiset\ShopifyApp\Util::getShopifyConfig('appbridge_version') ? '@'.config('shopify-app.appbridge_version') : '' }}"></script>
<script
Expand Down
20 changes: 19 additions & 1 deletion tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testRouteNames(): void

public function testGetShopifyConfig(): void
{
Config::set('shopify-app.config_api_callback', function (string $key, $shop) {
$this->app['config']->set('shopify-app.config_api_callback', function (string $key, $shop) {
if ($key === 'api_secret') {
return 'hello world';
}
Expand Down Expand Up @@ -105,4 +105,22 @@ public function testGraphQLWebhookTopic(): void
Util::getGraphQLWebhookTopic('ORDERS_PARTIALLY_FULFILLED')
);
}

public function testUseNativeAppBridgeIsTrue(): void
{
$this->app['config']->set('shopify-app.frontend_engine', 'VUE');

$result = Util::useNativeAppBridge();

$this->assertTrue($result);
}

public function testUseNativeAppBridgeIsFalse(): void
{
$this->app['config']->set('shopify-app.frontend_engine', 'REACT');

$result = Util::useNativeAppBridge();

$this->assertFalse($result);
}
}