From 5d44eefdcc39a5f6e0edeb0367b078f81af9d33c Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 17:03:03 +0300 Subject: [PATCH] fixes re-creation of anonymousUserId --- .../Basket/BasketWidgetViewComponent.cs | 1 - .../Toolbar/Cart/CartWidgetViewComponent.cs | 31 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs index fd249aca..3659494f 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.UI.Widgets; using EShopOnAbp.BasketService; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Volo.Abp.Guids; using Volo.Abp.Users; diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs index 078bb8a3..838cafe4 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs @@ -1,13 +1,11 @@ using System; -using System.Security.Claims; using System.Threading.Tasks; using EShopOnAbp.BasketService; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.UI.Widgets; using Volo.Abp.Guids; -using Volo.Abp.Security.Claims; using Volo.Abp.Users; namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; @@ -23,17 +21,18 @@ public class CartWidgetViewComponent : AbpViewComponent private readonly IBasketAppService _basketAppService; private readonly IGuidGenerator _guidGenerator; private readonly ICurrentUser _currentUser; - - protected const string AnonymousUserCookieName = "eshop_anonymousId"; + private readonly ILogger _logger; public CartWidgetViewComponent( IBasketAppService basketAppService, ICurrentUser currentUser, - IGuidGenerator guidGenerator) + IGuidGenerator guidGenerator, + ILogger logger) { _basketAppService = basketAppService; _currentUser = currentUser; _guidGenerator = guidGenerator; + _logger = logger; } public async Task InvokeAsync() @@ -41,11 +40,25 @@ public class CartWidgetViewComponent : AbpViewComponent BasketDto basketDto = new BasketDto(); try { + // Get anonymous user id from cookie + HttpContext.Request.Cookies.TryGetValue(CookieConstants.AnonymousUserCookieName, + out string anonymousUserId); + _logger.LogInformation($"========= Anonymous User Id from Cookie:{anonymousUserId} ========= "); + + // Generate new id for anonymous user + if (string.IsNullOrEmpty(anonymousUserId)) + { + anonymousUserId = _guidGenerator.Create().ToString(); + HttpContext.Response.Cookies.Append(CookieConstants.AnonymousUserCookieName, anonymousUserId); + _logger.LogInformation($"========= Generated new User Id:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); + } + if (!_currentUser.IsAuthenticated) { - var anonymousUserId = _guidGenerator.Create(); - HttpContext.Response.Cookies.Append(AnonymousUserCookieName, anonymousUserId.ToString()); - basketDto = await _basketAppService.GetByAnonymousUserIdAsync(anonymousUserId); + basketDto = await _basketAppService.GetByAnonymousUserIdAsync(Guid.Parse(anonymousUserId)); + _logger.LogInformation( + $"========= Get Basket for Anonymous UserId:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); + _logger.LogInformation($"========= Basket Item Count:{basketDto.Items.Count} ========= "); } else {