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

Commit

Permalink
Olavosantos 1094 issue branch w/ updated tests (#1191)
Browse files Browse the repository at this point in the history
* fix: fixed app-bridge redirect on fullpage_redirect auth view

* refactor: extracted variable logic from view into the controller

* style: fixed lint error

* fix: fixed serialization of shopDomain

* 🎨 CS fix

* ✅ Add composer normalize plugin to CI setup

* ✅ Roll back test to check against view

Co-authored-by: Olavo Santos <[email protected]>
Co-authored-by: Luke Walsh <[email protected]>
Co-authored-by: Stephen Sweetland <[email protected]>
  • Loading branch information
4 people authored Aug 26, 2022
1 parent 8eb0181 commit 9cdd1b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ jobs:
- name: Install Laravel and Orchestra Testbench
run: composer require "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-update

- name: Allow composer-normalize plugin
run: composer config allow-plugins.ergebnis/composer-normalize true

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
Expand Down
16 changes: 14 additions & 2 deletions src/Traits/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,20 @@ public function authenticate(Request $request, AuthenticateShop $authShop)
throw new MissingAuthUrlException('Missing auth url');
}

// Just return them straight to the OAUTH flow.
return Redirect::to($result['url']);
$shopDomain = $shopDomain->toNative();
$shopOrigin = $shopDomain ?? $request->user()->name;

return View::make(
'shopify-app::auth.fullpage_redirect',
[
'apiKey' => Util::getShopifyConfig('api_key', $shopOrigin),
'appBridgeVersion' => Util::getShopifyConfig('appbridge_version') ? '@'.config('shopify-app.appbridge_version') : '',
'authUrl' => $result['url'],
'host' => $request->host ?? base64_encode($shopOrigin.'/admin'),
'shopDomain' => $shopDomain,
'shopOrigin' => $shopOrigin,
]
);
} else {
// Go to home route
return Redirect::route(
Expand Down
16 changes: 12 additions & 4 deletions src/resources/views/auth/fullpage_redirect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<title>Redirecting...</title>

<script src="https://unpkg.com/@shopify/app-bridge{!! $appBridgeVersion !!}"></script>
<script src="https://unpkg.com/@shopify/app-bridge-utils{!! $appBridgeVersion !!}"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
var redirectUrl = "{!! $authUrl !!}";
Expand All @@ -17,11 +19,17 @@
normalizedLink = document.createElement('a');
normalizedLink.href = redirectUrl;
data = JSON.stringify({
message: 'Shopify.API.remoteRedirect',
data: { location: redirectUrl },
var AppBridge = window['app-bridge'];
var createApp = AppBridge.default;
var Redirect = AppBridge.actions.Redirect;
var app = createApp({
apiKey: "{{!! $apiKey !!}}",
shopOrigin: "{{!! $shopOrigin !!}}",
host: "{{!! $host !!}}",
});
window.parent.postMessage(data, "https://{{ $shopDomain }}");
var redirect = Redirect.create(app);
redirect.dispatch(Redirect.Action.REMOTE, normalizedLink.href);
}
});
</script>
Expand Down
7 changes: 5 additions & 2 deletions tests/Traits/AuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public function testAuthRedirectsToShopifyWhenNoCode(): void
$response = $this->call('post', '/authenticate', ['shop' => 'example.myshopify.com']);

// Check the redirect happens and location is set properly in the header.
$response->assertStatus(302);
$response->assertHeader('location', 'https://example.myshopify.com/admin/oauth/authorize?client_id='.Util::getShopifyConfig('api_key').'&scope=read_products%2Cwrite_products&redirect_uri=https%3A%2F%2Flocalhost%2Fauthenticate');
$response->assertViewHas('shopDomain', 'example.myshopify.com');
$response->assertViewHas(
'authUrl',
'https://example.myshopify.com/admin/oauth/authorize?client_id='.Util::getShopifyConfig('api_key').'&scope=read_products%2Cwrite_products&redirect_uri=https%3A%2F%2Flocalhost%2Fauthenticate'
);
}

public function testAuthAcceptsShopWithCode(): void
Expand Down

0 comments on commit 9cdd1b2

Please sign in to comment.