Ejemplo n.º 1
0
        public async Task <IActionResult> Register([FromBody] RegisterRequest registerForm, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(Result.Failure(ModelState.ToInnerErrors())));
            }

            (Result createResult, var userToken) = await identityService.CreateUserAsync(registerForm.UserToken,
                                                                                         registerForm.Username,
                                                                                         registerForm.Password);

            if (!createResult.Succeeded)
            {
                return(createResult.Status == ResultStatus.Validation
                           ? BadRequest(createResult)
                           : InternalServerError(createResult));
            }

            try
            {
                userStateService.RegisterUser(registerForm.UserToken, registerForm.Username);
            }
            catch (Exception ex)
            {
                Result deleteResult = await identityService.DeleteUserAsync(userToken);

                return(InternalServerError(
                           Result.Failure(deleteResult.Errors.Append(new InnerError(ErrorTarget.UserStateFailure, ex.Message)))));
            }

            (Result tokenResult, Token tokens) = await identityService.PostRegisterLoginAsync(registerForm.Username,
                                                                                              registerForm.Password,
                                                                                              cancellationToken);

            return(tokenResult.Succeeded
                       ? Ok(tokens)
                       : InternalServerError(tokenResult));
        }