Skip to content

Commit

Permalink
implement ConfirmEmailToken
Browse files Browse the repository at this point in the history
  • Loading branch information
imaun committed Aug 31, 2023
1 parent f53e8ac commit 7b243af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Idyfa.Core/Exceptions/IdyfaInvalidEmailTokenException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Idyfa.Core.Exceptions;

public class IdyfaInvalidEmailTokenException : Exception
{

public IdyfaInvalidEmailTokenException()
: base("The Email confirmation failed because the token is invalid.")
{
}
}
30 changes: 28 additions & 2 deletions src/Idyfa.Core/Services/IdyfaAuthManager.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -165,9 +167,33 @@ public async Task<string> 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<string> GenerateResetPasswordTokenAsync(string userName)
Expand Down

0 comments on commit 7b243af

Please sign in to comment.