Skip to content

Commit

Permalink
allow verifying email with short codes
Browse files Browse the repository at this point in the history
  • Loading branch information
GNUGradyn committed Oct 31, 2023
1 parent 8fb5030 commit 33ce115
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion JournalyApiV2/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ public async Task<IActionResult> VerifyEmail([FromBody] VerifyEmailRequest reque
}
else
{

if (request.ShortCode == null) throw new HttpBadRequestException("No verification code provided");
try
{
await _authService.VerifyEmailWithShortCode(GetUserId(), request.ShortCode);
}
catch (ArgumentException ex)
{
throw new HttpBadRequestException(ex.Message);
}
}

return StatusCode(204);
Expand Down
18 changes: 18 additions & 0 deletions JournalyApiV2/Services/BLL/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,22 @@ public async Task VerifyEmailWithLongCode(string longCode)

await _userManager.UpdateAsync(userObj);
}

public async Task VerifyEmailWithShortCode(Guid userId, string shortCode)
{
var result = await _authDbService.CheckShortCode(userId, shortCode);

if (result)
{
var user = await _userManager.FindByIdAsync(userId.ToString());
if (user == null) throw new Exception("Short code was found, but not the associated user");
user.EmailConfirmed = true;
await _userManager.UpdateAsync(user);
await _authDbService.VerifyUser(userId);
}
else
{
throw new ArgumentException("Invalid verification code");
}
}
}
1 change: 1 addition & 0 deletions JournalyApiV2/Services/BLL/IAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface IAuthService
Task ChangePassword(Guid userId, string oldPassword, string newPassword, int tokenId);
Task VerifyEmail(Guid userId, string toEmail, string firstName, string lastName);
Task VerifyEmailWithLongCode(string longCode);
Task VerifyEmailWithShortCode(Guid userId, string shortCode);
}
8 changes: 8 additions & 0 deletions JournalyApiV2/Services/DAL/AuthDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,12 @@ public async Task VerifyUser(Guid user)
db.RemoveRange(toRemove);
await db.SaveChangesAsync();
}

public async Task<bool> CheckShortCode(Guid userId, string shortCode)
{
await using var db = _db.Journaly();
var code = await db.EmailVerificationCodes.SingleOrDefaultAsync(x => x.User == userId && x.ShortCode == shortCode);

return code != null;
}
}
1 change: 1 addition & 0 deletions JournalyApiV2/Services/DAL/IAuthDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface IAuthDbService
Task<EmailVerification> GetOrCreateEmailVerificationCode(Guid userId);
Task<Guid?> GetUserByLongCode(string longCode);
Task VerifyUser(Guid user);
Task<bool> CheckShortCode(Guid userId, string shortCode);
}

0 comments on commit 33ce115

Please sign in to comment.