public async Task <IActionResult> GetAccountDetail(int id) { var user = await _repo.GetAccountDetail(id); if (user == null) { return(NotFound()); } var returnUser = _mapper.Map <AccountDetailDTO>(user); return(Ok(returnUser)); }
public async Task <Account> GetOrCreateExternalLoginUser(string provider, string key, string email, string fullname, string picture) { var user = await _userManager.FindByLoginAsync(provider, key); if (user != null) { var account = await _authRepo.GetAccountDetail(user.Id); return(account); } var userFromEmail = await _userManager.FindByEmailAsync(email); if (userFromEmail == null) { userFromEmail = new Account() { UserName = key, Email = email, PhotoUrl = picture, Fullname = fullname, EmailConfirmed = true }; var result = await _userManager.CreateAsync(userFromEmail, _config.GetSection("Defaults:Account:Password").Value); if (result.Succeeded) { await _userManager.AddToRoleAsync(userFromEmail, "learner"); } } if (string.IsNullOrEmpty(userFromEmail.PhotoUrl)) { userFromEmail.PhotoUrl = picture; await _db.SaveChangesAsync(); } var userInfo = new UserLoginInfo(provider, key, provider.ToUpperInvariant()); var loginResult = await _userManager.AddLoginAsync(userFromEmail, userInfo); if (loginResult.Succeeded) { return(userFromEmail); } return(null); }
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 // }); // } }