public async Task <IActionResult> FacebookLogin([FromBody] DangNhapFacebookViewModel model) { return(await _userService.SignInFacebookAsync(model)); }
public async Task <ResponseEntity> SignInFacebookAsync(DangNhapFacebookViewModel modelVm) { string[] ERR_MESSAGE = { "Vui lòng nhập email bạn đã đăng ký!", "Email này đã được sử dụng cho tài khoản facebook khác!", "Email không chính xác!" }; string[] ERR_STATUS = { "EMAIL_ENTER", "EMAIL_EXISTS", "EMAIL_INCORRECT" }; try { var httpClient = new HttpClient { BaseAddress = new Uri("https://graph.facebook.com/v2.9/") }; var response = await httpClient.GetAsync($"me?access_token={modelVm.facebookToken}&fields=id,name,email,first_name,last_name,age_range,birthday,gender,locale"); if (!response.IsSuccessStatusCode) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, "Login facebook failure !", "Please, try login again")); } var result = await response.Content.ReadAsStringAsync(); dynamic facebookAccount = JsonConvert.DeserializeObject <FacebookResult>(result); //Checkfacebook id UserJira facebookUser = await _useJiraRepository.GetSingleByConditionAsync("facebookId", facebookAccount.id); if (facebookUser != null) { UserLoginResult userResult = new UserLoginResult(); userResult.email = facebookUser.email; userResult.accessToken = await GenerateTokenJira(facebookUser); return(new ResponseEntity(StatusCodeConstants.OK, userResult, MessageConstants.SIGNIN_SUCCESS)); } //Đăng nhập thành công fb kiểm tra có email không nếu có cho dn thành công Type objType = facebookAccount.GetType(); if (objType.GetProperty("email") != null) { //Kiểm tra có email chưa lấy ra AppUser userCheckEmail = await _userRepository.GetSingleByConditionAsync("email", facebookAccount.email); if (userCheckEmail != null) { //Cập nhật fb id cho mail đó userCheckEmail.facebookId = facebookAccount.id; await _userRepository.UpdateByConditionAsync("email", facebookAccount.email, userCheckEmail); UserLoginResult userResult = new UserLoginResult(); userResult.email = userCheckEmail.email; userResult.accessToken = await GenerateTokenJira(facebookUser); return(new ResponseEntity(StatusCodeConstants.OK, userResult, MessageConstants.SIGNIN_SUCCESS)); } } //Nếu chưa có tạo tài khoản AppUser userModel = new AppUser(); userModel.facebookId = facebookAccount.id; userModel.name = facebookAccount.first_name + " " + facebookAccount.last_name; userModel.email = userModel.facebookId + "@facebook.com"; userModel.deleted = false; userModel.avatar = "/static/user-icon.png"; userModel.userTypeId = "CUSTOMER"; AppUser userInsert = await _userRepository.InsertAsync(userModel); if (userInsert != null) { UserLoginResult userResult = new UserLoginResult(); userResult.email = userModel.email; userResult.accessToken = await GenerateToken(userModel); return(new ResponseEntity(StatusCodeConstants.OK, userResult, MessageConstants.SIGNIN_SUCCESS)); } return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[0], ERR_MESSAGE[0])); } catch (Exception ex) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[0], ERR_MESSAGE[0])); } //register if required //var facebookUser = _context.FacebookUsers.SingleOrDefault(x => x.Id == facebookAccount.Id); //if (facebookUser == null) //{ // var user = new ApplicationUser { UserName = facebookAccount.Name, Email = facebookAccount.Email }; // var result2 = await _userManager.CreateAsync(user); // if (!result2.Succeeded) return BadRequest(); // facebookUser = new FacebookUser { Id = facebookAccount.Id, UserId = user.Id }; // _context.FacebookUsers.Add(facebookUser); // _context.SaveChanges(); //} // } // return new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS); //} //catch (Exception ex) //{ // return new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ex.Message, MessageConstants.SIGNIN_ERROR); //} //string[] ERR_MESSAGE = { "Vui lòng nhập email bạn đã đăng ký!", "Email này đã được sử dụng cho tài khoản facebook khác!", "Email không chính xác!" }; //string[] ERR_STATUS = { "EMAIL_ENTER", "EMAIL_EXISTS", "EMAIL_INCORRECT" }; //try //{ // UserLoginResult result = new UserLoginResult(); // AppUser entity = await _userRepository.GetByFacebookAsync(modelVm.FacebookId); // if (entity != null) // Nếu FacebookId đúng => đăng nhập thành công // { // // Tạo token // result.accessToken = await GenerateToken(entity); // result.email = entity.email; // return new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS); // } // //// Nếu facebook id sai và email chưa nhập // //if (string.IsNullOrEmpty(modelVm.Email)) // // return new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[0], ERR_MESSAGE[0]); // if (entity == null) // { // var httpClient = new HttpClient { BaseAddress = new Uri("https://graph.facebook.com/v2.9/") }; // var response = await httpClient.GetAsync($"me?access_token={facebookToken.Token}&fields=id,name,email,first_name,last_name,age_range,birthday,gender,locale,picture"); // if (!response.IsSuccessStatusCode) return BadRequest(); // var result = await response.Content.ReadAsStringAsync(); // var facebookAccount = JsonConvert.DeserializeObject<FacebookAccount>(result); // //register if required // var facebookUser = _context.FacebookUsers.SingleOrDefault(x => x.Id == facebookAccount.Id); // if (facebookUser == null) // { // var user = new ApplicationUser { UserName = facebookAccount.Name, Email = facebookAccount.Email }; // var result2 = await _userManager.CreateAsync(user); // if (!result2.Succeeded) return BadRequest(); // facebookUser = new FacebookUser { Id = facebookAccount.Id, UserId = user.Id }; // _context.FacebookUsers.Add(facebookUser); // _context.SaveChanges(); // } // } // return new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS); //} //catch (Exception ex) //{ // return new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ex.Message, MessageConstants.SIGNIN_ERROR); //} }
public async Task <ResponseEntity> SignInFacebookAsync(DangNhapFacebookViewModel modelVm) { string[] ERR_MESSAGE = { "Vui lòng nhập email bạn đã đăng ký!", "Email này đã được sử dụng cho tài khoản facebook khác!", "Email không chính xác!" }; string[] ERR_STATUS = { "EMAIL_ENTER", "EMAIL_EXISTS", "EMAIL_INCORRECT" }; try { await _lopHocRepository.EnableAsync(); await _lopHocRepository.DisableAsync(); NguoiDung entity = await _nguoiDungRepository.GetByFacebookAsync(modelVm.FacebookId); if (entity != null) // Nếu FacebookId đúng => đăng nhập thành công { // Tạo token entity.Token = await GenerateToken(entity); NguoiDungViewModel model = _mapper.Map <NguoiDungViewModel>(entity); return(new ResponseEntity(StatusCodeConstants.OK, model, MessageConstants.SIGNIN_SUCCESS)); } // Nếu facebook id sai và email chưa nhập if (string.IsNullOrEmpty(modelVm.Email)) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[0], ERR_MESSAGE[0])); } // Lấy ra thông tin người dùng từ database dựa vào email entity = await _nguoiDungRepository.GetByEmailAsync(modelVm.Email); if (entity == null) { // Kiểm tra xem email đã tồn tại trong bảng khách hàng chưa // - Nếu chưa có thông báo đăng nhập thất bại // - Nếu có thì tạo tài khoản cho user=> đăng nhập thành công KhachHang khachHang = await _khachHangRepository.GetByEmailAsync(modelVm.Email); if (khachHang == null) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[2], ERR_MESSAGE[2])); } ThongTinKHViewModel thongTinKHVm = JsonConvert.DeserializeObject <ThongTinKHViewModel>(khachHang.ThongTinKH); // Tạo tài khoản mới cho user entity = new NguoiDung(); entity.Id = Guid.NewGuid().ToString(); entity.Email = thongTinKHVm.Email; entity.MatKhau = BCrypt.Net.BCrypt.HashPassword("Cybersoft@123"); entity.HoTen = khachHang.TenKH; entity.BiDanh = khachHang.BiDanh; entity.SoDT = thongTinKHVm.SoDienThoai; entity.Avatar = "/static/user-icon.png"; entity.MaNhomQuyen = "HOCVIEN"; // Thực hiện truy vấn thêm mới entity = await _nguoiDungRepository.InsertAsync(entity); if (entity == null) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.SIGNIN_ERROR)); } } // Email đúng, FacebookId có tồn tại nhưng không khớp với facebook id đang đăng nhập // Cái này để tránh trường hợp 1 email xài cho nhiều tài khoản else if (!string.IsNullOrEmpty(entity.FacebookId)) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[1], ERR_MESSAGE[1])); } // Lưu FacebookId vào database entity.FacebookId = modelVm.FacebookId; entity = await _nguoiDungRepository.UpdateAsync(entity.Id, entity); // Tạo token entity.Token = await GenerateToken(entity); NguoiDungViewModel result = _mapper.Map <NguoiDungViewModel>(entity); return(new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS)); } catch (Exception ex) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ex.Message, MessageConstants.SIGNIN_ERROR)); } }