Пример #1
0
        public async Task <ActionResult> VerifyLogin([FromBody] AuthUserResource resource)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.GetErrorMessages()));
                }

                var user   = _mapper.Map <AuthUserResource, User>(resource);
                var result = await _userService.FirstOrDefaultAsync(user.Login, user.Password);

                if (result == null)
                {
                    return(BadRequest("Erro ao tentar realizar o login."));
                }

                var token = CryptoFunctions.GenerateToken(_configuration, user);

                return(Ok(new
                {
                    error = false,
                    result = new
                    {
                        token,
                        user = new { user.Id, user.Login }
                    }
                }));
            }
            catch (Exception ex)
            {
                var message = "Erro ao tentar realizar o login.";
                return(BadRequest(new { error = true, result = new { message } }));
            }
        }