Skip to content

Commit

Permalink
add cookie keyy
Browse files Browse the repository at this point in the history
  • Loading branch information
n1crack committed Aug 31, 2023
1 parent 4b14d5e commit 3d2f1c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ozdemir\Aurora;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Ozdemir\Aurora\Storage\StorageInterface;

class Cart implements \Serializable
Expand Down Expand Up @@ -417,7 +418,17 @@ public function updateConditionStorage()

public static function defaultSessionKey(): string
{
return Auth::check() ? Auth::id() : session()->getId();
if (Auth::check()) {
return 'user:' . Auth::id();
}
$guestToken = Cookie::get('guest_token');

if (!$guestToken) {
$guestToken = uniqid();
Cookie::queue('guest_token', $guestToken, 1440);
}

return 'guest:' . $guestToken;
}

public function loadSession($key)
Expand Down
2 changes: 1 addition & 1 deletion tests/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@
expect(Cart::quantity())->toBe(2);
expect(Cart::weight())->toBe(8);
expect(Cart::getInstanceKey())->toBe('cart');
expect(Cart::getSessionKey())->toBe(session()->getId());
expect(Cart::getSessionKey())->toContain('guest:');

$user = (new \Illuminate\Foundation\Auth\User())->forceFill([
'id' => 123,
Expand Down

0 comments on commit 3d2f1c5

Please sign in to comment.