Skip to content

Commit

Permalink
include email verification status in jwts
Browse files Browse the repository at this point in the history
  • Loading branch information
GNUGradyn committed Oct 31, 2023
1 parent c3780f8 commit 8f9349f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions JournalyApiV2/Services/BLL/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AuthService(UserManager<JournalyUser> userManager, IConfiguration config,
_emailService = emailService;
}

private string GenerateJwtToken(string userId, string email, string givenName, string familyName, int tokenId)
private string GenerateJwtToken(string userId, string email, string givenName, string familyName, int tokenId, bool verified)
{
var claims = new List<Claim>
{
Expand All @@ -41,7 +41,8 @@ private string GenerateJwtToken(string userId, string email, string givenName, s
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.GivenName, givenName),
new Claim(JwtRegisteredClaimNames.FamilyName, familyName),
new Claim("token_id", tokenId.ToString())
new Claim("token_id", tokenId.ToString()),
new Claim("email_verified", verified ? "true" : "false")
};

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Identity:Key"]));
Expand Down Expand Up @@ -72,7 +73,7 @@ public async Task<AuthenticationResponse> SignIn(string email, string password)
var refreshToken = await _authDbService.NewRefreshTokenAsync(Guid.Parse(user.Id));
return new AuthenticationResponse
{
Token = GenerateJwtToken(user.Id, email, user.FirstName, user.LastName, refreshToken.TokenId),
Token = GenerateJwtToken(user.Id, email, user.FirstName, user.LastName, refreshToken.TokenId, user.EmailConfirmed),
ExpiresIn = _config.GetValue<int>("Identity:ExpireSeconds"),
RefreshToken = refreshToken.Token
};
Expand Down Expand Up @@ -123,7 +124,7 @@ public async Task<AuthenticationResponse> RefreshToken(string refreshToken)
{
RefreshToken = newToken.Token,
ExpiresIn = _config.GetValue<int>("Identity:ExpireSeconds"),
Token = GenerateJwtToken(user.Id, user.Email, user.FirstName, user.LastName, newToken.TokenId)
Token = GenerateJwtToken(user.Id, user.Email, user.FirstName, user.LastName, newToken.TokenId, user.EmailConfirmed)
};
}

Expand All @@ -139,7 +140,7 @@ public async Task<AuthenticationResponse> ChangeName(string firstName, string la
// Generate new JWT and associated refresh token with the name updated
await _authDbService.VoidRefreshTokensAsync(tokenId);
var refreshToken = await _authDbService.NewRefreshTokenAsync(userId);
var accessToken = GenerateJwtToken(userId.ToString(), user.Email, firstName, lastName, refreshToken.TokenId);
var accessToken = GenerateJwtToken(userId.ToString(), user.Email, firstName, lastName, refreshToken.TokenId, user.EmailConfirmed);

return new AuthenticationResponse
{
Expand All @@ -161,7 +162,7 @@ public async Task<AuthenticationResponse> ChangeEmail(string email, Guid userId,
// Generate new JWT and associated refresh token with the name updated
await _authDbService.VoidRefreshTokensAsync(tokenId);
var refreshToken = await _authDbService.NewRefreshTokenAsync(userId);
var accessToken = GenerateJwtToken(userId.ToString(), email, user.FirstName, user.LastName, refreshToken.TokenId);
var accessToken = GenerateJwtToken(userId.ToString(), email, user.FirstName, user.LastName, refreshToken.TokenId, user.EmailConfirmed);

return new AuthenticationResponse
{
Expand Down

0 comments on commit 8f9349f

Please sign in to comment.