Exemplo n.º 1
0
        public async Task <AuthenticationResult> RegisterUserAsync(string email, string password)
        {
            var existingUser = await _userManager.FindByEmailAsync(email);

            if (existingUser != null)
            {
                return(new AuthenticationResult
                {
                    Errors = new[] { "User with this email address already exists" }
                });
            }

            var newUser = new IdentityUser
            {
                Email    = email,
                UserName = email
            };

            var createdUser = await _userManager.CreateAsync(newUser, password);

            if (!createdUser.Succeeded)
            {
                return(new AuthenticationResult
                {
                    Errors = createdUser.Errors.Select(x => x.Description)
                });
            }

            DateTime createdDate;

            createdDate = DateTime.Today;

            var userProfile = new UserProfile
            {
                FirstName      = null,
                LastName       = null,
                Email          = email,
                Address        = null,
                ProfilePicture = null,
                DOB            = null,
                AccountNumber  = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds,
                DateCreated    = createdDate
            };

            await _userProfileService.CreateProfileAsync(userProfile);

            var accountData = new AccountData
            {
                AccountNumber  = userProfile.AccountNumber,
                CurrentBalance = 0,
                LedgerBalance  = 0
            };
            await _userAccountService.CreateAccountAsync(accountData);

            var profileId = new UserProfileResponse {
                Id = userProfile.Id
            };

            userProfileId = profileId.Id.ToString();

            return(await GenerateAuthenticationResultForUserAsync(newUser));
        }