コード例 #1
0
        public IActionResult Login(LoginVm model)
        {
            try
            {
                var user = _context.Users.Find(c => c.Email == model.Email).FirstOrDefault();

                if (user == null)
                {
                    return(new BadRequestObjectResult("Not authorized"));
                }

                if (!user.IsActive)
                {
                    return(new BadRequestObjectResult("User is not active"));
                }

                if (user.IsDeleted)
                {
                    return(new BadRequestObjectResult("Not authorized"));
                }

                if (!string.IsNullOrWhiteSpace(model.Password))
                {
                    if (string.IsNullOrWhiteSpace(user.Password))
                    {
                        return(new BadRequestObjectResult("Not authorized"));
                    }

                    var match = _cipherService.Verify(model.Password, user.Password);

                    if (!match)
                    {
                        return(new BadRequestObjectResult("Not authorized"));
                    }
                }
                else
                {
                    return(new BadRequestObjectResult("Not authorized"));
                }

                var token = _tokenService.Generate(user);

                return(new OkObjectResult(new TokenVm
                {
                    Token = token
                }));
            }
            catch (Exception e)
            {
                _logger.Error($"Error: {e}");
                return(new BadRequestObjectResult("Error occured"));
            }
        }