Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fixed compile error #512

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CleanArchitecture.Blazor.Application.Common.Interfaces.Identity;
public interface IIdentityService : IService
{
Task<Result<TokenResponse>> LoginAsync(TokenRequest request, CancellationToken cancellation = default);
Task<TokenResponse> GenerateJwtAsync(ApplicationUser user);
Task<TokenResponse> GenerateJwtAsync(ApplicationUser user, bool rememberMe = false);
Task<Result<TokenResponse>> RefreshTokenAsync(RefreshTokenRequest request, CancellationToken cancellation = default);
Task<ClaimsPrincipal> GetClaimsPrincipal(string token);
Task<string?> GetUserNameAsync(string userId, CancellationToken cancellation = default);
Expand Down
27 changes: 13 additions & 14 deletions src/Infrastructure/Services/Identity/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,10 @@ public async Task<Result<TokenResponse>> LoginAsync(TokenRequest request, Cancel
{
return await Result<TokenResponse>.FailureAsync(new string[] { _localizer["Invalid Credentials."] });
}
user.RefreshToken = GenerateRefreshToken();
var tokenExpiryTime = DateTime.Now.AddDays(7);

if (request.RememberMe)
{
tokenExpiryTime = DateTime.Now.AddYears(1);
}
user.RefreshTokenExpiryTime = tokenExpiryTime;
await _userManager.UpdateAsync(user);

var token = await GenerateJwtAsync(user);
var response = new TokenResponse { Token = token, RefreshTokenExpiryTime = tokenExpiryTime, RefreshToken = user.RefreshToken, ProfilePictureDataUrl = user.ProfilePictureDataUrl };
return await Result<TokenResponse>.SuccessAsync(response);

var token = await GenerateJwtAsync(user,request.RememberMe);
return await Result<TokenResponse>.SuccessAsync(token);
}

public async Task<Result<TokenResponse>> RefreshTokenAsync(RefreshTokenRequest request, CancellationToken cancellation = default)
Expand Down Expand Up @@ -173,11 +164,19 @@ private string GenerateRefreshToken()
rng.GetBytes(randomNumber);
return Convert.ToBase64String(randomNumber);
}
public async Task<TokenResponse> GenerateJwtAsync(ApplicationUser user)
public async Task<TokenResponse> GenerateJwtAsync(ApplicationUser user,bool rememberMe=false)
{
user.RefreshToken = GenerateRefreshToken();
var principal = await _userClaimsPrincipalFactory.CreateAsync(user);
var tokenExpiryTime = DateTime.Now.AddDays(7);
if (rememberMe)
{
tokenExpiryTime = DateTime.Now.AddYears(1);
}
user.RefreshTokenExpiryTime = tokenExpiryTime;
await _userManager.UpdateAsync(user);
var token = GenerateEncryptedToken(GetSigningCredentials(), principal.Claims);
return token;
return new TokenResponse { Token = token, RefreshTokenExpiryTime = tokenExpiryTime, RefreshToken = user.RefreshToken, ProfilePictureDataUrl = user.ProfilePictureDataUrl };
}
private string GenerateEncryptedToken(SigningCredentials signingCredentials, IEnumerable<Claim> claims)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/Services/JWT/AccessTokenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public AccessTokenProvider(ProtectedLocalStorage localStorage, NavigationManager
public async Task GenerateJwt(ApplicationUser applicationUser)
{
var token = await _identityService.GenerateJwtAsync(applicationUser,true);
await _localStorage.SetAsync(_tokenKey, token.Token);
await _localStorage.SetAsync(_refreshTokenKey, token.RefreshToken);
await _localStorage.SetAsync(_tokenKey, token.Token??"");
await _localStorage.SetAsync(_refreshTokenKey, token.RefreshToken??"");
_tenantProvider.TenantId = applicationUser.TenantId;
_tenantProvider.TenantName = applicationUser.TenantName;
_currentUser.UserId = applicationUser.Id;
Expand Down