Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 12 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;
}
19 changes: 19 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,22 @@ 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
{
$currentFrontendEngine = self::getShopifyConfig('frontend_engine') ?? FrontendEngine::BLADE;

switch ($currentFrontendEngine) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enmaboya Switch is probably not needed since its only React we need to look for.

return !$currentFrontendEngine->isSame(FrontendEngine::REACT);

So "if current engine is not React, return true and use app bridge"... React will return false and not use app bridge.

case FrontendEngine::REACT:
return false;

default:
return true;
}
}
}
13 changes: 13 additions & 0 deletions src/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,17 @@
],

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

/*
|--------------------------------------------------------------------------
| Frontend engine used
|--------------------------------------------------------------------------
|
| Available engines: BLADE, VUE, 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' => \Osiset\ShopifyApp\Objects\Enums\FrontendEngine::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
19 changes: 19 additions & 0 deletions tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Config;
use LogicException;
use Osiset\ShopifyApp\Objects\Enums\FrontendEngine;
use Osiset\ShopifyApp\Util;
use stdClass;

Expand Down Expand Up @@ -105,4 +106,22 @@ public function testGraphQLWebhookTopic(): void
Util::getGraphQLWebhookTopic('ORDERS_PARTIALLY_FULFILLED')
);
}

public function testUseNativeAppBridgeIsTrue(): void
{
Config::set('shopify-app.frontend_engine', FrontendEngine::VUE);

$result = Util::useNativeAppBridge();

$this->assertTrue($result);
}

public function testUseNativeAppBridgeIsFalse(): void
{
Config::set('shopify-app.frontend_engine', FrontendEngine::REACT);

$result = Util::useNativeAppBridge();

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