Beispiel #1
0
        public async Task <IActionResult> RegisterAsync(UserRegistrationViewModel registrationViewModel)
        {
            var validator           = new UserRegistrationViewModelValidator(_accountService);
            ValidationResult result = validator.Validate(registrationViewModel);

            if (!result.IsValid)
            {
                return(BadRequest(new ErrorResponseFormat(result.Errors.First().ErrorMessage)));
            }

            UserRegistrationModel authorizationModel = _mapper.Map <UserRegistrationModel>(registrationViewModel);
            UserModel             user = await _accountService.CreateUserAsync(authorizationModel);

            if (user == null)
            {
                return(BadRequest(new ErrorResponseFormat(Constants.Errors.USER_CREATION_FAILED)));
            }

            string tokenString = _jwtService.GetJWTToken(user, new EmailClaimsService(_jwtSettings));
            await _accountService.AddAccountTokenAsync(
                new AccountTokenModel()
            {
                ExpirationDate = DateTime.Now.AddMinutes(10),
                UserId         = user.Id
            }
                );

            await _emailService.SendEmailConfirmationLinkAsync(user.Email, tokenString);

            return(NoContent());
        }