Exemplo n.º 1
0
        public async Task <IActionResult> Login(AccountForLoginDTO accountLogin)
        {
            Models.Account accountFromDb;
            if (accountLogin.LoginMethod.Contains("@"))
            {
                accountFromDb = await _userManager.FindByEmailAsync(accountLogin.LoginMethod);
            }
            else
            {
                accountFromDb = await _repo.GetAccountDetail(accountLogin.LoginMethod);
            }
            if (accountFromDb == null)
            {
                return(Unauthorized(new
                {
                    Error = "Không tìm thấy tài khoản"
                }));
            }
            if (accountFromDb.IsDisabled)
            {
                return(Unauthorized(new
                {
                    Error = "Tài khoản đã bị khóa"
                }));
            }
            var result = await _signinManager.CheckPasswordSignInAsync(accountFromDb, accountLogin.Password, false);

            if (result.Succeeded)
            {
                var token = await _repo.GenerateToken(accountFromDb, ipAddress());

                var accountForDetail = _mapper.Map <AccountDetailDTO>(accountFromDb);
                setTokenCookie(token.RefreshToken);
                return(Ok(new
                {
                    account = accountForDetail,
                    token = token.Token
                }));
            }
            else
            {
                return(BadRequest("Sai mật khẩu"));
            }
            // if (ComparePassword(accountLogin.Password, accountFromDb.PasswordHashed, accountFromDb.PasswordSalt))
            // {
            //     var token = CreateToken(accountFromDb);
            //     var returnAccount = _mapper.Map<AccountDetailDTO>(accountFromDb);
            //     return Ok(new
            //     {
            //         Token = token,
            //         Account = returnAccount
            //     });
            // }
        }