Skip to content

Commit c566fa7

Browse files
authored
Pass the host when using the token routes on blade (#65)
1 parent 320166a commit c566fa7

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

src/Macros/TokenUrl.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@ abstract class TokenUrl
1515
/**
1616
* Return a URL to token path with shop and target.
1717
*
18-
* @param string $route The route name.
19-
* @param array $params Additional route params.
20-
* @param bool $absolute Absolute or relative?
18+
* @param string $route The route name.
19+
* @param array $params Additional route params.
20+
* @param bool $absolute Absolute or relative?
2121
*
2222
* @return array
2323
*/
24-
public function generateParams(string $route, $params = [], bool $absolute = true): array
24+
public function generateParams(string $route, array $params = [], bool $absolute = true): array
2525
{
2626
return [
2727
Util::getShopifyConfig('route_names.authenticate.token'),
2828
[
2929
'shop' => ShopDomain::fromRequest(Request::instance())->toNative(),
3030
'target' => URL::route($route, $params, $absolute),
31+
'host' => Request::instance()->get('host'),
3132
],
3233
];
3334
}

src/resources/views/auth/fullpage_redirect.blade.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<script type="text/javascript">
1212
document.addEventListener('DOMContentLoaded', function () {
1313
var redirectUrl = "{!! $authUrl !!}";
14-
if (window.top == window.self) {
14+
var normalizedLink;
15+
if (window.top === window.self) {
1516
// If the current window is the 'parent', change the URL by setting location.href
1617
window.top.location.href = redirectUrl;
1718
} else {

src/resources/views/auth/token.blade.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737

3838
@if(config('shopify-app.appbridge_enabled'))
3939
<script>
40+
const host = new URLSearchParams(location.search).get("host")
4041
utils.getSessionToken(app).then((token) => {
41-
window.location.href = `{!! $target !!}{!! Str::contains($target, '?') ? '&' : '?' !!}token=${token}`;
42+
window.location.href = `{!! $target !!}{!! Str::contains($target, '?') ? '&' : '?' !!}token=${token}&host=${host}`;
4243
});
4344
</script>
4445
@endif

tests/Macros/TokenRedirectTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ public function testTokenRedirect(): void
1212
{
1313
// Setup request
1414
$currentRequest = Request::instance();
15+
$host = base64_encode('example.myshopify.com');
1516
$newRequest = $currentRequest->duplicate(
1617
// Query Params
1718
[
1819
'shop' => 'example.myshopify.com',
20+
'host' => $host,
1921
]
2022
);
2123
Request::swap($newRequest);
@@ -25,7 +27,7 @@ public function testTokenRedirect(): void
2527
$location = $response->headers->get('location');
2628

2729
$this->assertSame(
28-
'http://localhost/authenticate/token?shop=example.myshopify.com&target=http%3A%2F%2Flocalhost',
30+
'http://localhost/authenticate/token?shop=example.myshopify.com&target=http%3A%2F%2Flocalhost&host=ZXhhbXBsZS5teXNob3BpZnkuY29t',
2931
$location
3032
);
3133
}

0 commit comments

Comments
 (0)