diff --git a/src/Idyfa.Core/Exceptions/IdyfaInvalidEmailTokenException.cs b/src/Idyfa.Core/Exceptions/IdyfaInvalidEmailTokenException.cs new file mode 100644 index 0000000..f3c1303 --- /dev/null +++ b/src/Idyfa.Core/Exceptions/IdyfaInvalidEmailTokenException.cs @@ -0,0 +1,10 @@ +namespace Idyfa.Core.Exceptions; + +public class IdyfaInvalidEmailTokenException : Exception +{ + + public IdyfaInvalidEmailTokenException() + : base("The Email confirmation failed because the token is invalid.") + { + } +} \ No newline at end of file diff --git a/src/Idyfa.Core/Services/IdyfaAuthManager.cs b/src/Idyfa.Core/Services/IdyfaAuthManager.cs index 61e5332..4fca068 100644 --- a/src/Idyfa.Core/Services/IdyfaAuthManager.cs +++ b/src/Idyfa.Core/Services/IdyfaAuthManager.cs @@ -1,8 +1,10 @@ using System.Security.Claims; +using System.Text; using Idyfa.Core.Events; using Idyfa.Core.Contracts; using Idyfa.Core.Exceptions; using Idyfa.Core.Extensions; +using Microsoft.AspNetCore.WebUtilities; namespace Idyfa.Core.Services; @@ -165,9 +167,33 @@ public async Task GenerateEmailConfirmationTokenAsync(string userName) return await _userManager.GenerateEmailConfirmationTokenAsync(user).ConfigureAwait(false); } - public Task ConfirmEmailAsync(string userName, string token) + public async Task ConfirmEmailAsync(string userName, string token) { - throw new NotImplementedException(); + if (userName.IsNullOrEmpty()) + throw new ArgumentNullException(nameof(userName)); + + if (token.IsNullOrEmpty()) + throw new ArgumentNullException(nameof(token)); + + if (GetUserNameType() == UserNameType.PhoneNumber) + { + userName = userName.NormalizePhoneNumber(); + } + + var validateUsername = _userValidator.ValidateUserName(userName); + if (validateUsername.Any()) + throw new InvalidUserNameException(validateUsername); + + var user = await _userManager.FindByNameAsync(userName).ConfigureAwait(false); + if (user is null) throw new IdyfaUserNotFoundException(); + + var tokenBytes = WebEncoders.Base64UrlDecode(token); + var decodedToken = Encoding.UTF8.GetString(tokenBytes); + + var result = await _userManager.ConfirmEmailAsync(user, token).ConfigureAwait(false); + + if (!result.Succeeded) + throw new IdyfaInvalidEmailTokenException(); } public Task GenerateResetPasswordTokenAsync(string userName)