//----------------------------------------------------------------------- // // Copyright (c) aliasvault. All rights reserved. // Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information. // //----------------------------------------------------------------------- namespace AliasVault.Api.Controllers; using AliasServerDb; using AliasVault.Api.Controllers.Abstracts; using AliasVault.Api.Helpers; using Asp.Versioning; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; /// /// Controller for generating identities taking into account existing information on the AliasVault server. /// /// UserManager instance. /// DbContextFactory instance. [ApiVersion("1")] public class IdentityController(UserManager userManager, IAliasServerDbContextFactory dbContextFactory) : AuthenticatedRequestController(userManager) { /// /// Verify that provided email address is not already taken by another user. /// /// The full email address to check. /// True if the email address is already taken, false otherwise. [HttpPost("CheckEmail/{email}")] public async Task CheckEmail(string email) { var user = await GetCurrentUserAsync(); if (user == null) { return Unauthorized(); } bool isTaken = await EmailClaimExistsAsync(email); return Ok(new { isTaken }); } /// /// Verify that provided email address is not already taken by another user. /// /// The email address to check. /// True if the email address is already taken, false otherwise. private async Task EmailClaimExistsAsync(string email) { await using var context = await dbContextFactory.CreateDbContextAsync(); var sanitizedEmail = EmailHelper.SanitizeEmail(email); var claimExists = await context.UserEmailClaims.FirstOrDefaultAsync(c => c.Address == sanitizedEmail); return claimExists != null; } }