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

Confirm email feature #1056

Open
Mike6x opened this issue Nov 11, 2024 · 0 comments
Open

Confirm email feature #1056

Mike6x opened this issue Nov 11, 2024 · 0 comments
Assignees

Comments

@Mike6x
Copy link

Mike6x commented Nov 11, 2024

Hi All,

I try to migrate this feature from v1, but unsucessfully. I always catch the error with usermanager... Please support me !!!
image

Flow is my code:

  1. Endpoint:
    ` public static class GetConirmEmailEndpoint
    {
    internal static RouteHandlerBuilder MapGetCornfirmEmailEndpoint(this IEndpointRouteBuilder endpoints)
    {
    return endpoints.MapGet("/confirm-email", async (
    [FromQuery] string userId,
    [FromQuery] string code,
    [FromQuery] string tenant,
    HttpContext context,
    ITenantService tenantService,
    IUserService userService,
    CancellationToken cancellationToken) =>
    {
    TenantDetail tenantDetail = await tenantService.GetByIdAsync(tenant) ?? throw new NotFoundException($"Tenant: {tenant} not found");
    var tenantInfo = tenantDetail.Adapt();
    context.SetTenantInfo(tenantInfo, true);

             context.Request.Headers.Append("tenant", tenant);
             
    
             return Task.FromResult(userService.ConfirmEmailAsync(userId, code, tenant, cancellationToken));
         })
         .WithName(nameof(GetConirmEmailEndpoint))
         .WithSummary("Confirm email")
         .WithDescription("Confirm email address for a user.")
         .AllowAnonymous();
     }
    

    }`

  2. Service
    ` public async Task ConfirmEmailAsync(string userId, string code, string tenant, CancellationToken cancellationToken)
    {
    EnsureValidTenant();

     var user = await userManager.Users
         .Where(u => u.Id == userId )
         .FirstOrDefaultAsync(cancellationToken) ?? throw new NotFoundException($"User with Id: {userId} not found!");
    
     if (user!.EmailConfirmed) return  $"Account: {userId}  already confirmed with E-Mail {user.Email} ";
     code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
     var result = await userManager.ConfirmEmailAsync(user, code);
     return result.Succeeded
         ? $"Account Confirmed for E-Mail {user.Email}. You can now use the /api/tokens endpoint to generate JWT."
         : throw new InternalServerException($"An error occurred while confirming {user.Email}");
    

    }`

@iammukeshm iammukeshm self-assigned this Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants