Exemplo n.º 1
0
        public async Task <IActionResult> Login(UserLoginInputModel input)
        {
            var response = new ResponseModel();

            if (!ModelState.IsValid)
            {
                var errorMessage = this.utilitiesService.GetModelStateErorrs(ModelState);
                foreach (var message in errorMessage)
                {
                    response.AddErrorMessage(message);
                }
                response.StatusCode = 400;
                return(Json(response));
            }

            var user = this.usersService.Login(input);

            if (user == null)
            {
                response.AddErrorMessage("Invalid login information");
                response.StatusCode = 401;
                return(Json(response));
            }

            var tokens = await this.tokenAuthService.GenerateTokensAsync(user);

            response.Data = tokens;
            return(Json(response));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Refresh(RefreshTokenInputModel input)
        {
            var response = new ResponseModel();

            var headerToken = this.utilitiesService.GetAccessTokenHeader(HttpContext);

            if (headerToken == null)
            {
                response.AddErrorMessage("Missing access token header");
                response.StatusCode = 400;
                return(Json(response));
            }

            var accessToken = this.tokenService.GetAccessToken(headerToken, input.RefreshToken);

            if (accessToken == null)
            {
                response.AddErrorMessage("Invalid refresh token");
                response.StatusCode = 400;
                return(Json(response));
            }

            if (accessToken.RefreshExpirationDate < DateTime.UtcNow)
            {
                response.AddErrorMessage("Refresh token expired");
                response.StatusCode = 400;
                return(Json(response));
            }
            var user = this.usersService.GetById(accessToken.UserId);

            var tokens = await this.tokenService.GenerateTokensAsync(user);

            response.Data = tokens;
            return(Json(response));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Submit(SubmitScoreInputModel input)
        {
            var response = new ResponseModel();

            if (!ModelState.IsValid)
            {
                var errorMessages = this.utilitiesService.GetModelStateErorrs(ModelState);

                foreach (var message in errorMessages)
                {
                    response.AddErrorMessage(message);
                }

                response.StatusCode = 400;
                return(Json(response));
            }

            var userId = User.FindFirst("id").Value;
            var result = await this.scoreService.SubmitAsync(userId, input.Points);

            response.StatusCode = 201;
            response.Data       = new
            {
                result.SubmitedAt,
                result.Score
            };

            return(Json(response));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Register(UserRegisterInputModel input)
        {
            var response = new ResponseModel();

            if (!ModelState.IsValid)
            {
                var errorMessages = this.utilitiesService.GetModelStateErorrs(ModelState);
                foreach (var message in errorMessages)
                {
                    response.AddErrorMessage(message);
                }
                response.StatusCode = 400;
                return(Json(response));
            }

            var isEmailAvailable = this.usersService.IsEmailAvailable(input.Email);

            if (isEmailAvailable == false)
            {
                response.AddErrorMessage("This email is already taken");
                response.StatusCode = 400;
                return(Json(response));
            }

            var isUsernameAvailable = this.usersService.IsUsernameAvailable(input.Username);

            if (isUsernameAvailable == false)
            {
                response.AddErrorMessage("This username is already taken");
                response.StatusCode = 400;
                return(Json(response));
            }

            var user = await this.usersService.RegisterAsync(input);

            var tokens = await this.tokenAuthService.GenerateTokensAsync(user);

            response.Data = tokens;
            return(Json(response));
        }