Beispiel #1
0
        public async Task <IActionResult> Login(LoginRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new AuthFailedResponse
                {
                    status = false,
                    errors = ModelState.Values.SelectMany(x => x.Errors.Select(xx => xx.ErrorMessage))
                }));
            }

            var authResponse = await _userLoginService.LoginAsync(request.userName, request.password);

            if (!authResponse.success)
            {
                return(BadRequest(new AuthFailedResponse
                {
                    status = false,
                    errors = authResponse.errors
                }));
            }

            return(Ok(new AuthSuccessResponse
            {
                status = true,
                username = request.userName,
                userDetail = await _userService.FindByUsernameAsync(request.userName),
                token = authResponse.token,
                created = new JavaScriptSerializer().Serialize(authResponse.created),
                expiry = new JavaScriptSerializer().Serialize(authResponse.expiry)
            }));
        }