Browse Source

fixes re-creation of anonymousUserId

pull/41/head
Galip Tolga Erdem 5 years ago
parent
commit
5d44eefdcc
  1. 1
      apps/public-web/src/EShopOnAbp.PublicWeb/Components/Basket/BasketWidgetViewComponent.cs
  2. 31
      apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs

1
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;

31
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<CartWidgetViewComponent> _logger;
public CartWidgetViewComponent(
IBasketAppService basketAppService,
ICurrentUser currentUser,
IGuidGenerator guidGenerator)
IGuidGenerator guidGenerator,
ILogger<CartWidgetViewComponent> logger)
{
_basketAppService = basketAppService;
_currentUser = currentUser;
_guidGenerator = guidGenerator;
_logger = logger;
}
public async Task<IViewComponentResult> 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
{

Loading…
Cancel
Save