Esempio n. 1
0
        public async Task <IActionResult> Register([FromBody] RegisterModel registerModel)
        {
            if (registerModel.Password != registerModel.PasswordConfirmation)
            {
                throw new ApiException(400, "Passwords don't match", ErrorCode.InvalidCredentials);
            }

            var newUser = new User()
            {
                Email    = registerModel.Email,
                UserName = registerModel.Email
            };

            var result = await _userManager.CreateAsync(newUser, registerModel.Password);

            ErrorResultsHandler.ThrowIfIdentityError(result);

            var response = new
            {
                access_token = await _tokenService.GenerateToken(newUser),
                user         = _mapper.Map <CurrentUserViewModel>(newUser)
            };

            return(Ok(response));
        }
Esempio n. 2
0
        public async Task <User> ConfirmResetPassword(ConfirmResetPasswordModel model)
        {
            var user = await _userManager.FindByIdAsync(model.Id.ToString());

            if (user == null)
            {
                throw new ApiException(404, "User id not found", ErrorCode.NotFound);
            }

            if (model.NewPassword != model.PasswordConfirmation)
            {
                throw new ApiException(400, "Passwords don't match", ErrorCode.ValidationError);
            }

            var correctToken = Url.DecodeQueryParamValue(model.Token);
            var result       = await _userManager.ResetPasswordAsync(user, correctToken, model.NewPassword);

            ErrorResultsHandler.ThrowIfIdentityError(result);
            return(user);
        }
Esempio n. 3
0
        public async Task <User> UpdateProfile(int userId, UpdateProfileModel model)
        {
            using (var transaction = _db.Database.BeginTransaction())
            {
                try
                {
                    var user = await _db.Users.FindAsync(userId);

                    user.UserName = model.Username ?? user.UserName;
                    user.Email    = model.Email ?? user.Email;

                    var result = await _userManager.UpdateAsync(user);

                    ErrorResultsHandler.ThrowIfIdentityError(result);

                    if (!string.IsNullOrEmpty(model.NewPassword))
                    {
                        if (model.NewPassword != model.PasswordConfirmation)
                        {
                            throw new ApiException(400, "Passwords don't match", ErrorCode.ValidationError);
                        }

                        result = await _userManager.ChangePasswordAsync(user, model.Password, model.NewPassword);

                        ErrorResultsHandler.ThrowIfIdentityError(result);
                    }

                    if (model.Avatar != null)
                    {
                        byte[] avatarBytes = null;

                        using (var memoryStream = new MemoryStream())
                        {
                            await model.Avatar.CopyToAsync(memoryStream);

                            avatarBytes = memoryStream.ToArray();
                        }

                        var fileExtension = Path.GetExtension(model.Avatar.FileName);


                        var newAvatarPath =
                            await _avatarsService.UpdateAvatar(avatarBytes, fileExtension);

                        if (user.AvatarPath != null)
                        {
                            _avatarsService.DeleteAvatar(user.AvatarPath);
                        }

                        user.AvatarPath = newAvatarPath;
                    }

                    await _userManager.UpdateAsync(user);

                    transaction.Commit();

                    return(user);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }