Skip to content

Commit 6a71e57

Browse files
authored
Merge pull request #44 from BitBagCommerce/UC-30-fix-cookie
[UC-30] Fix cookie handling
2 parents 9ea4488 + d9ffadf commit 6a71e57

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

src/Manager/CookieManager.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,22 @@ public function setUserComCookie(string $value): void
4949
->withValue($value)
5050
->withPath('/')
5151
->withSecure(true)
52-
->withHttpOnly(true)
52+
->withExpires(new \DateTimeImmutable('+1 year'))
53+
->withHttpOnly(false)
5354
->withSameSite('lax');
5455

5556
if (null !== $this->cookieDomain && '' !== $this->cookieDomain) {
5657
$cookie = $cookie->withDomain($this->cookieDomain);
58+
} else {
59+
$request = $this->requestStack->getCurrentRequest();
60+
if (null === $request) {
61+
return;
62+
}
63+
64+
$domain = $this->getBaseDomain($request->getHost());
65+
if (null !== $domain) {
66+
$cookie = $cookie->withDomain($domain);
67+
}
5768
}
5869

5970
$this->queue->queue($cookie);
@@ -70,4 +81,22 @@ private function isShopUser(): bool
7081

7182
return true;
7283
}
84+
85+
private function getBaseDomain(string $host): ?string
86+
{
87+
$host = (string) preg_replace('/:\d+$/', '', $host);
88+
89+
if ($host === 'localhost' || filter_var($host, \FILTER_VALIDATE_IP) !== false) {
90+
return null;
91+
}
92+
93+
$parts = explode('.', $host);
94+
$count = count($parts);
95+
96+
if ($count >= 2) {
97+
return '.' . $parts[$count - 2] . '.' . $parts[$count - 1];
98+
}
99+
100+
return null;
101+
}
73102
}

src/Updater/CustomerWithKeyUpdater.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,20 @@ public function updateWithUserKey(
108108
);
109109

110110
$this->userApi->mergeUsers($apiAwareResource, $userByEmailFromForm['id'], [$userFoundByKey['id']]);
111-
$this->changeCookieWithEvent($user, $apiAwareResource, $eventName, $payload);
111+
if (is_array($user) && isset($user['email']) && is_string($user['email'])) {
112+
$this->sendEvent($apiAwareResource, $user['email'], $eventName, $payload);
113+
}
114+
$this->changeCookie($user);
112115

113116
return $user;
114117
}
115118

116119
$user = $this->userApi->createUser($apiAwareResource, $payload);
117-
$this->changeCookieWithEvent($user, $apiAwareResource, $eventName, $payload);
120+
121+
if (is_array($user) && isset($user['email']) && is_string($user['email'])) {
122+
$this->sendEvent($apiAwareResource, $user['email'], $eventName, $payload);
123+
}
124+
$this->changeCookie($user);
118125

119126
return $user;
120127
}
@@ -154,16 +161,16 @@ private function updateForUserWithoutEmail(
154161
$this->userApi->mergeUsers($apiAwareResource, $customerFoundByEmail['id'], [$userFromUserKey['id']]);
155162
}
156163

157-
$this->changeCookieWithEvent($user, $apiAwareResource, $eventName);
164+
if (is_array($user) && isset($user['email']) && is_string($user['email'])) {
165+
$this->sendEvent($apiAwareResource, $user['email'], $eventName, $payload);
166+
}
167+
$this->changeCookie($user);
158168

159169
return $user;
160170
}
161171

162-
public function changeCookieWithEvent(
172+
public function changeCookie(
163173
?array $user,
164-
UserComApiAwareInterface $apiAwareResource,
165-
string $eventName,
166-
?array $payload = null,
167174
): void {
168175
if (false === is_array($user) ||
169176
false === array_key_exists('id', $user) ||
@@ -173,6 +180,5 @@ public function changeCookieWithEvent(
173180
}
174181

175182
$this->cookieManager->setUserComCookie($user['user_key']);
176-
$this->sendEvent($apiAwareResource, $user['email'], $eventName, $payload);
177183
}
178184
}

0 commit comments

Comments
 (0)