public void OnActionExecuting(ActionExecutingContext context)
            {
                AuthUserHolder userHolder = context.ActionArguments["userHolder"] as AuthUserHolder;

                if (null == userHolder)
                {
                    InvalidateRequest(context, "No user holder is present.", _logger, 422);
                }
                AuthUser user = userHolder.User;

                if (null == user)
                {
                    InvalidateRequest(context, "No user is present in the user holder.", _logger, 422);
                }
                if (null == user.Username)
                {
                    InvalidateRequest(context, "No username is present in the user.", _logger, 422);
                }
                if (!context.ModelState.IsValid)
                {
                    List <string> errors = context
                                           .ModelState
                                           .Values
                                           .SelectMany(e => e.Errors)
                                           .Select(e => e.ErrorMessage)
                                           .Distinct()
                                           .Cast <string>()
                                           .ToList();
                    InvalidateRequest(context, errors, _logger, 422);
                }
            }
Exemple #2
0
        public async Task <IActionResult> LoginUser([FromBody] AuthUserHolder userHolder)
        {
            try
            {
                User user = await _userService.AuthenticateUser(userHolder.User.Email, userHolder.User.Password);

                return(Ok(new UserHolder(user)));
            } catch (Exception ex)
            {
                return(HandleException(ex, _logger));
            }
        }
Exemple #3
0
        public async Task <IActionResult> RegisterUser([FromBody] AuthUserHolder userHolder)
        {
            try
            {
                User user = await _userService.RegisterUser(userHolder.User.Username, userHolder.User.Email, userHolder.User.Password);

                this.HttpContext.Response.StatusCode = 201;
                return(Json(new UserHolder(user)));
            } catch (Exception ex)
            {
                return(HandleException(ex, _logger));
            }
        }