Skip to content

Commit 2ac8835

Browse files
authored
✨ refactoring login function
Loginfunction
2 parents bd24786 + 621cef7 commit 2ac8835

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/Application/Common/Interfaces/Identity/IIdentityService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace CleanArchitecture.Blazor.Application.Common.Interfaces.Identity;
1111
public interface IIdentityService : IService
1212
{
1313
Task<Result<TokenResponse>> LoginAsync(TokenRequest request, CancellationToken cancellation = default);
14-
Task<string> GenerateJwtAsync(ApplicationUser user);
14+
Task<TokenResponse> GenerateJwtAsync(ApplicationUser user);
1515
Task<Result<TokenResponse>> RefreshTokenAsync(RefreshTokenRequest request, CancellationToken cancellation = default);
1616
Task<ClaimsPrincipal> GetClaimsPrincipal(string token);
1717
Task<string?> GetUserNameAsync(string userId, CancellationToken cancellation = default);

src/Blazor.Server.UI/Pages/Authentication/Login.razor

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
@inject AccessTokenProvider TokenProvider
33
@inject IJSRuntime JS
44
@inherits OwningComponentBase
5+
@using CleanArchitecture.Blazor.Application.Common.Interfaces.Identity;
6+
@using CleanArchitecture.Blazor.Application.Common.Interfaces.Identity.DTOs;
57
@using CleanArchitecture.Blazor.Infrastructure.Services.JWT
68
@using Microsoft.AspNetCore.DataProtection
79
@using CleanArchitecture.Blazor.Application.Common.Security
@@ -93,6 +95,8 @@
9395

9496
[Inject]
9597
protected NavigationManager NavigationManager { get; set; } = null!;
98+
[Inject]
99+
protected IIdentityService _identityService { get; set; } = null!;
96100

97101
private LoginFormModel _model = new()
98102
{
@@ -147,9 +151,18 @@
147151
}
148152
else
149153
{
150-
await TokenProvider.GenerateJwt(user);
151-
NavigationManager.NavigateTo(NavigationManager.Uri, true);
152-
Logger.LogInformation("{@UserName:l} has successfully logged in", user.UserName);
154+
var tokenresponse = await _identityService.LoginAsync(new TokenRequest() { UserName=_model.UserName,Password=_model.Password, RememberMe = _model.RememberMe });
155+
if (tokenresponse.Succeeded && tokenresponse.Data is not null)
156+
{
157+
await TokenProvider.SaveToken(tokenresponse.Data);
158+
NavigationManager.NavigateTo(NavigationManager.Uri, true);
159+
Logger.LogInformation("{@UserName:l} has successfully logged in", user.UserName);
160+
}
161+
else
162+
{
163+
Snackbar.Add(tokenresponse.ErrorMessage, Severity.Error);
164+
}
165+
153166
}
154167
}
155168
}

src/Infrastructure/Services/Identity/IdentityService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private string GenerateRefreshToken()
173173
rng.GetBytes(randomNumber);
174174
return Convert.ToBase64String(randomNumber);
175175
}
176-
public async Task<string> GenerateJwtAsync(ApplicationUser user)
176+
public async Task<TokenResponse> GenerateJwtAsync(ApplicationUser user)
177177
{
178178
var principal = await _userClaimsPrincipalFactory.CreateAsync(user);
179179
var token = GenerateEncryptedToken(GetSigningCredentials(), principal.Claims);

src/Infrastructure/Services/JWT/AccessTokenProvider.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Security.Cryptography;
2+
using CleanArchitecture.Blazor.Application.Common.Interfaces.Identity.DTOs;
23
using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant;
34
using CleanArchitecture.Blazor.Infrastructure.Extensions;
45
using Microsoft.AspNetCore.Components;
@@ -8,12 +9,14 @@ namespace CleanArchitecture.Blazor.Infrastructure.Services.JWT;
89
public class AccessTokenProvider
910
{
1011
private readonly string _tokenKey = nameof(_tokenKey);
12+
private readonly string _refreshTokenKey = nameof(_refreshTokenKey);
1113
private readonly ProtectedLocalStorage _localStorage;
1214
private readonly NavigationManager _navigation;
1315
private readonly IIdentityService _identityService;
1416
private readonly ITenantProvider _tenantProvider;
1517
private readonly ICurrentUserService _currentUser;
1618
public string? AccessToken { get; private set; }
19+
public string? RefreshToken { get; private set; }
1720

1821
public AccessTokenProvider(ProtectedLocalStorage localStorage, NavigationManager navigation, IIdentityService identityService,
1922
ITenantProvider tenantProvider,
@@ -27,15 +30,34 @@ public AccessTokenProvider(ProtectedLocalStorage localStorage, NavigationManager
2730
}
2831
public async Task GenerateJwt(ApplicationUser applicationUser)
2932
{
30-
AccessToken = await _identityService.GenerateJwtAsync(applicationUser);
31-
await _localStorage.SetAsync(_tokenKey, AccessToken);
33+
var token = await _identityService.GenerateJwtAsync(applicationUser,true);
34+
await _localStorage.SetAsync(_tokenKey, token.Token);
35+
await _localStorage.SetAsync(_refreshTokenKey, token.RefreshToken);
3236
_tenantProvider.TenantId = applicationUser.TenantId;
3337
_tenantProvider.TenantName = applicationUser.TenantName;
3438
_currentUser.UserId = applicationUser.Id;
3539
_currentUser.UserName = applicationUser.UserName;
3640
_currentUser.TenantId = applicationUser.TenantId;
3741
_currentUser.TenantName = applicationUser.TenantName;
3842

43+
}
44+
public async Task SaveToken(TokenResponse token)
45+
{
46+
AccessToken = token.Token;
47+
RefreshToken = token.RefreshToken;
48+
await _localStorage.SetAsync(_tokenKey, AccessToken);
49+
await _localStorage.SetAsync(_refreshTokenKey, RefreshToken);
50+
var principal = await _identityService.GetClaimsPrincipal(token.Token);
51+
if (principal?.Identity?.IsAuthenticated ?? false)
52+
{
53+
_tenantProvider.TenantId = principal?.GetTenantId();
54+
_tenantProvider.TenantName = principal?.GetTenantName();
55+
_currentUser.UserId = principal?.GetUserId();
56+
_currentUser.UserName = principal?.GetUserName();
57+
_currentUser.TenantId = principal?.GetTenantId();
58+
_currentUser.TenantName = principal?.GetTenantId();
59+
}
60+
3961
}
4062
public async Task<ClaimsPrincipal> GetClaimsPrincipal()
4163
{
@@ -45,6 +67,8 @@ public async Task<ClaimsPrincipal> GetClaimsPrincipal()
4567
if (token.Success && !string.IsNullOrEmpty(token.Value))
4668
{
4769
AccessToken = token.Value;
70+
var refreshToken = await _localStorage.GetAsync<string>(_refreshTokenKey);
71+
RefreshToken = refreshToken.Value;
4872
var principal = await _identityService.GetClaimsPrincipal(token.Value);
4973
if (principal?.Identity?.IsAuthenticated ?? false)
5074
{

0 commit comments

Comments
 (0)