Skip to content

Commit 3914395

Browse files
committed
Added local storage var for identifiying user
1 parent 447a87d commit 3914395

File tree

9 files changed

+58
-17
lines changed

9 files changed

+58
-17
lines changed

LinkDotNet.Blog.Web/Pages/AboutMe.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@
3535

3636
protected override async Task OnInitializedAsync()
3737
{
38-
await userRecordService.StoreUserRecordAsync();
3938
var userIdentity = (await authenticationStateProvider.GetAuthenticationStateAsync()).User.Identity;
4039
if (userIdentity != null)
4140
{
4241
isAuthenticated = userIdentity.IsAuthenticated;
4342
}
4443
}
44+
45+
protected override async Task OnAfterRenderAsync(bool firstRender)
46+
{
47+
if (firstRender)
48+
{
49+
await userRecordService.StoreUserRecordAsync();
50+
}
51+
}
4552
}

LinkDotNet.Blog.Web/Pages/Admin/DashboardService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public DashboardService(
2525
public async Task<DashboardData> GetDashboardDataAsync()
2626
{
2727
var records = (await userRecordRepository.GetAllAsync()).ToList();
28-
var users = records.GroupBy(r => r.IpHash).Count();
28+
var users = records.GroupBy(r => r.UserIdentifierHash).Count();
2929
var users30Days = records
3030
.Where(r => r.DateTimeUtcClicked >= DateTime.UtcNow.AddDays(-30))
31-
.GroupBy(r => r.IpHash).Count();
31+
.GroupBy(r => r.UserIdentifierHash).Count();
3232

3333
var clicks = records.Count;
3434
var clicks30Days = records.Count(r => r.DateTimeUtcClicked >= DateTime.UtcNow.AddDays(-30));

LinkDotNet.Blog.Web/Pages/BlogPostPage.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@
5656

5757
protected override async Task OnInitializedAsync()
5858
{
59-
await userRecordService.StoreUserRecordAsync();
6059
BlogPost = await blogPostRepository.GetByIdAsync(BlogPostId);
6160
}
6261

6362
protected override async Task OnAfterRenderAsync(bool firstRender)
6463
{
6564
if (firstRender)
6665
{
66+
await userRecordService.StoreUserRecordAsync();
6767
await jsRuntime.InvokeVoidAsync("hljs.highlightAll");
6868
StateHasChanged();
6969
}

LinkDotNet.Blog.Web/Pages/Index.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,20 @@
3535

3636
protected override async Task OnInitializedAsync()
3737
{
38-
await userRecordService.StoreUserRecordAsync();
3938
currentPage = await blogPostRepository.GetAllAsync(
4039
p => p.IsPublished,
4140
b => b.UpdatedDate,
4241
pageSize: appConfiguration.BlogPostsPerPage);
4342
}
4443

44+
protected override async Task OnAfterRenderAsync(bool firstRender)
45+
{
46+
if (firstRender)
47+
{
48+
await userRecordService.StoreUserRecordAsync();
49+
}
50+
}
51+
4552
private string GetAbsolutePreviewImageUrl()
4653
{
4754
var backgroundUrl = appConfiguration.Introduction.BackgroundUrl;

LinkDotNet.Blog.Web/Pages/Search.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@
2222

2323
protected override async Task OnInitializedAsync()
2424
{
25-
await userRecordService.StoreUserRecordAsync();
2625
var term = Uri.UnescapeDataString(SearchTerm);
2726
blogPosts = (await blogPostRepository.GetAllAsync(t => t.IsPublished && (t.Title.Contains(term)
2827
|| t.Tags.Any(tag => tag.Content == term)),
2928
b => b.UpdatedDate)).ToList();
3029
}
30+
31+
protected override async Task OnAfterRenderAsync(bool firstRender)
32+
{
33+
if (firstRender)
34+
{
35+
await userRecordService.StoreUserRecordAsync();
36+
}
37+
}
3138
}

LinkDotNet.Blog.Web/Pages/SearchByTag.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@
2222
IList<BlogPost> blogPosts = new List<BlogPost>();
2323
protected override async Task OnInitializedAsync()
2424
{
25-
await userRecordService.StoreUserRecordAsync();
2625
Tag = Uri.UnescapeDataString(Tag);
2726
blogPosts = (await blogPostRepository.GetAllAsync(
2827
b => b.Tags.Any(t => t.Content == Tag),
2928
b => b.UpdatedDate))
3029
.ToList();
3130
}
31+
32+
protected override async Task OnAfterRenderAsync(bool firstRender)
33+
{
34+
if (firstRender)
35+
{
36+
await userRecordService.StoreUserRecordAsync();
37+
}
38+
}
3239
}

LinkDotNet.Blog.Web/Shared/NavMenu.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</button>
1010
<div class="collapse navbar-collapse" id="navbarSupportedContent">
1111
<ul class="navbar-nav ms-auto mb-2 mb-lg-0 me-5">
12-
<li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
12+
<li class="nav-item active"><a class="nav-link" href="/">Home</a></li>
1313
@if (configuration.HasLinkedinAccount)
1414
{
1515
<li class="nav-item"><a class="nav-link" target="_blank" href="@configuration.LinkedinAccountUrl"><i
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using System.Threading.Tasks;
3+
using Blazored.LocalStorage;
34
using LinkDotNet.Domain;
45
using LinkDotNet.Infrastructure.Persistence;
56
using Microsoft.AspNetCore.Components;
67
using Microsoft.AspNetCore.Components.Authorization;
7-
using Microsoft.AspNetCore.Http;
88

99
namespace LinkDotNet.Blog.Web.Shared
1010
{
@@ -16,20 +16,20 @@ public interface IUserRecordService
1616
public class UserRecordService : IUserRecordService
1717
{
1818
private readonly IRepository<UserRecord> userRecordRepository;
19-
private readonly IHttpContextAccessor httpContextAccessor;
2019
private readonly NavigationManager navigationManager;
2120
private readonly AuthenticationStateProvider authenticationStateProvider;
21+
private readonly ILocalStorageService localStorageService;
2222

2323
public UserRecordService(
2424
IRepository<UserRecord> userRecordRepository,
25-
IHttpContextAccessor httpContextAccessor,
2625
NavigationManager navigationManager,
27-
AuthenticationStateProvider authenticationStateProvider)
26+
AuthenticationStateProvider authenticationStateProvider,
27+
ILocalStorageService localStorageService)
2828
{
2929
this.userRecordRepository = userRecordRepository;
30-
this.httpContextAccessor = httpContextAccessor;
3130
this.navigationManager = navigationManager;
3231
this.authenticationStateProvider = authenticationStateProvider;
32+
this.localStorageService = localStorageService;
3333
}
3434

3535
public async Task StoreUserRecordAsync()
@@ -40,17 +40,30 @@ public async Task StoreUserRecordAsync()
4040
return;
4141
}
4242

43-
var httpContext = httpContextAccessor.HttpContext;
44-
var ipHash = httpContext.Connection.RemoteIpAddress?.GetHashCode() ?? 0;
43+
var identifierHash = await GetIdentifierHashAsync();
4544

4645
var record = new UserRecord
4746
{
48-
IpHash = ipHash,
47+
UserIdentifierHash = identifierHash,
4948
DateTimeUtcClicked = DateTime.UtcNow,
5049
UrlClicked = navigationManager.ToBaseRelativePath(navigationManager.Uri),
5150
};
5251

5352
await userRecordRepository.StoreAsync(record);
5453
}
54+
55+
private async Task<int> GetIdentifierHashAsync()
56+
{
57+
var hasKey = await localStorageService.ContainKeyAsync("user");
58+
if (hasKey)
59+
{
60+
var key = await localStorageService.GetItemAsync<Guid>("user");
61+
return key.GetHashCode();
62+
}
63+
64+
var id = Guid.NewGuid();
65+
await localStorageService.SetItemAsync("user", id);
66+
return id.GetHashCode();
67+
}
5568
}
5669
}

LinkDotNet.Domain/UserRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace LinkDotNet.Domain
44
{
55
public class UserRecord : Entity
66
{
7-
public int IpHash { get; set; }
7+
public int UserIdentifierHash { get; set; }
88

99
public DateTime DateTimeUtcClicked { get; set; }
1010

0 commit comments

Comments
 (0)