Exemple #1
0
        public async Task <IActionResult> LoginUser([FromBody] LoginRequest loginRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.Values));
            }

            var exists = await EmailExistsAsync(loginRequest.Email);

            if (!exists)
            {
                return(NotFound());
            }

            var user = await FindUserByEmailAsync(loginRequest.Email);

            try
            {
                var authenticated = _authenticationService.AuthenticatePassword(user, loginRequest.Password);
                if (authenticated)
                {
                    // return JWT
                    var token = _authenticationService.MakeToken(user);

                    var response = new AuthenticationSuccessResponse(user, token);

                    return(Ok(response));
                }
                else
                {
                    return(Unauthorized());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
 private void OnAuthenticationSuccessResponse(AuthenticationSuccessResponse response)
 {
     MessageBox.Show(response.ToString());
 }