public async Task <IActionResult> RegisterUser([FromBody] RegisterUserVM registerUserVM)
        {
            try
            {
                var user = await _userStore.GetUserByPhoneAsync(registerUserVM.Phone);

                if (user != null)
                {
                    return(Conflict(1));
                }

                var result = await _userStore.CreateUserAsync(new UserEntity
                {
                    Name     = registerUserVM.Name,
                    Surname  = registerUserVM.Surname,
                    Phone    = registerUserVM.Phone,
                    Password = registerUserVM.PasswordHash
                });

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
 public async Task <IActionResult> Post([FromBody] CreateUserModel model)
 {
     model.RoleId = ReservedConstants.SUPER_ADMIN_ROLE_ID;
     try
     {
         return(Ok(await _store.CreateUserAsync(model)));
     }
     catch (EmailAlreadyTakenException)
     {
         return(BadRequest(_AddModelStateError(FieldNamesConstants.EMAIL, ErrorMessagesConstants.EMAIL_ALREADY_TAKEN)));
     }
 }
Example #3
0
        public async Task <IActionResult> SignUp(SignUpViewModel signUpViewModel)
        {
            if (ModelState.IsValid && signUpViewModel.AgreeTerms)
            {
                try
                {
                    // new datadock identity inc claim
                    var datadockIdentity = new ClaimsIdentity();
                    datadockIdentity.AddClaim(new Claim(DataDockClaimTypes.DataDockUserId, User.Identity.Name));
                    User.AddIdentity(datadockIdentity);

                    // create user in datadock
                    var newUser = await _userStore.CreateUserAsync(User.Identity.Name, User.Claims);

                    if (newUser == null)
                    {
                        Log.Error("Creation of new user account returned null");
                        return(RedirectToAction("Error", "Home"));
                    }

                    // create userSettings
                    var userSettings = new UserSettings
                    {
                        UserId         = User.Identity.Name,
                        LastModified   = DateTime.UtcNow,
                        LastModifiedBy = "DataDock"
                    };
                    await _userStore.CreateOrUpdateUserSettingsAsync(userSettings);

                    // create ownerSettings for the github user owner
                    var userOwner = new OwnerSettings
                    {
                        OwnerId                  = User.Identity.Name,
                        IsOrg                    = false,
                        DisplayGitHubAvatar      = true,
                        DisplayGitHubDescription = true,
                        LastModified             = DateTime.UtcNow,
                        LastModifiedBy           = "DataDock"
                    };
                    await _ownerSettingsStore.CreateOrUpdateOwnerSettingsAsync(userOwner);

                    // logout and back in to persist new claims
                    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

                    return(Challenge(new AuthenticationProperties()
                    {
                        RedirectUri = "account/welcome"
                    }));
                }
                catch (UserAccountExistsException)
                {
                    Log.Warning("User account {0} already exists. Identities: {1}. Claims Total: {2}", User.Identity.Name, User.Identities.Count(), User.Claims.Count());
                    var datadockUser = await _userStore.GetUserAccountAsync(User.Identity.Name);

                    if (datadockUser.Claims.FirstOrDefault(c => c.Type.Equals(DataDockClaimTypes.DataDockUserId)) ==
                        null)
                    {
                        // new datadock identity inc claim
                        var datadockIdentity = new ClaimsIdentity();
                        datadockIdentity.AddClaim(new Claim(DataDockClaimTypes.DataDockUserId, User.Identity.Name));
                        User.AddIdentity(datadockIdentity);
                        await _userStore.UpdateUserAsync(User.Identity.Name, User.Claims);

                        // logout and back in to persist new claims
                        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

                        return(Challenge(new AuthenticationProperties()
                        {
                            RedirectUri = "account/settings"
                        }));
                    }

                    return(RedirectToAction("Settings"));
                }
                catch (Exception ex)
                {
                    Log.Error("Error creating user account", ex);
                    Console.WriteLine(ex);
                    throw;
                }
            }

            ViewBag.Success = "failed";
            return(View(signUpViewModel));
        }