public async Task <Result <UserWithToken> > RegisterAsync(UserRegistration user, CancellationToken cancellationToken = default) { if (!_userManager.Users.Any()) { var adminResult = CreateAdminAsync(cancellationToken); if (adminResult.Result.IsError) { return(Result <UserWithToken> .Fail <UserWithToken>(ExceptionConstants.UNABLE_TO_CREATE_ADMIN)); } } var existingUser = await _userManager.FindByEmailAsync(user.Email); if (existingUser != null) { return(Result <UserWithToken> .Quite <UserWithToken>(ExceptionConstants.USER_ALREADY_EXIST)); } var newUser = new AppUser { Email = user.Email, UserName = user.Email, PersonalDiscount = 0, Role = AppIdentityConstants.USER, IsEmailConfirmed = false }; var createUser = await _userManager.CreateAsync(newUser, user.Password); if (!createUser.Succeeded) { return(Result <UserWithToken> .Fail <UserWithToken>(createUser.Errors.Select(_ => _.Description).Join("\n"))); } await _userManager.AddToRoleAsync(newUser, AppIdentityConstants.USER); var profile = await _userService.CreateAccountAsyncById(newUser.Id, cancellationToken); var token = await GenerateToken(newUser); UserWithToken result = new UserWithToken() { UserView = profile.Data, UserToken = token.Data, RefreshToken = GenerateRefreshToken(), ExpiresIn = NumberСonstants.REFRESH }; var isRefreshToketAdded = await _userService.AddRefreshTokenAsync(result.RefreshToken, profile.Data.UserDTO.IdFromIdentity); if (isRefreshToketAdded.IsError) { return(Result <UserWithToken> .Fail <UserWithToken>(isRefreshToketAdded.Message)); } await _emailBuilder.SendConfirmMessage(newUser, user.CallBackUrl, cancellationToken); return(Result <UserWithToken> .Ok(result)); }