11using System . Security . Cryptography ;
2+ using CleanArchitecture . Blazor . Application . Common . Interfaces . Identity . DTOs ;
23using CleanArchitecture . Blazor . Application . Common . Interfaces . MultiTenant ;
34using CleanArchitecture . Blazor . Infrastructure . Extensions ;
45using Microsoft . AspNetCore . Components ;
@@ -8,12 +9,14 @@ namespace CleanArchitecture.Blazor.Infrastructure.Services.JWT;
89public 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