public async Task <bool> AuthenticateGoogleUser(LoginWithGoogleFormDataViewModel loginFormDataViewModel, Func <Contracts.Models.User, Task <bool> > accessValidation) { bool isFormValid = ValidateLoginForm(loginFormDataViewModel.Email, "", SignUpMethods.Google); if (!isFormValid) { return(false); } var email = loginFormDataViewModel.Email; var queryResult = await _querySender.Send(new LoginWithGoogleUserQuery { Email = email, SocialLoginId = loginFormDataViewModel.SocialLoginId, ChannelId = loginFormDataViewModel.ChannelId, SignUpMethodId = loginFormDataViewModel.SignUpMethodId, SiteId = loginFormDataViewModel.SiteId ?? Site.ComSite, }); var user = queryResult.User; if (!queryResult.Success || user == null) { return(false); } var accessGranted = await accessValidation(queryResult.User); if (accessGranted) { var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.AltId.ToString()), new Claim(ClaimTypes.Name, user.Email), new Claim("Roles", user.RolesId.ToString()) }; var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "login")); await _httpContextAccessor.HttpContext.SignInAsync(claimsPrincipal); // Set so we can access SessionProvider in a normal way during the rest of the request _httpContextAccessor.HttpContext.User = claimsPrincipal; } return(accessGranted); }
public async Task <GoogleSignInResponseViewModel> SignInGoogle([FromBody] GoogleSignInFormDataViewModel model) { if (ModelState.IsValid) { var result = new { Succeeded = true }; var email = model.Email; var passwordHasher = new PasswordHasher <string>(); string password = new Random().NextDouble().ToString(); string PasswordHash = passwordHasher.HashPassword(model.Email, password); try { var Email = model.Email; var userSearchResult = await _querySender.Send(new UserSearchQuery { Email = model.Email, ChannelId = Channels.Feel, SignUpMethodId = SignUpMethods.Google, }); if (userSearchResult.Success) { var UserData = new LoginWithGoogleFormDataViewModel { Email = model.Email, SocialLoginId = model.SocialLoginId, ChannelId = Channels.Feel, SignUpMethodId = SignUpMethods.Google, }; var authenticated = await _authenticationHelper.AuthenticateGoogleUser(UserData, u => { return(Task.FromResult(true)); }); return(new GoogleSignInResponseViewModel { Success = authenticated, Session = await _sessionProvider.Get() }); } else { if (string.IsNullOrEmpty(model.Email)) { return(new GoogleSignInResponseViewModel { Success = false, IsEmailRequired = true, Session = await _sessionProvider.Get() }); } await _commandSender.Send(new RasvRegisterUserCommand { Email = model.Email, PasswordHash = PasswordHash, UserName = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneCode = model.PhoneCode, PhoneNumber = model.PhoneNumber, ChannelId = Channels.Feel, RolesId = 11, SocialLoginId = model.SocialLoginId, OptedForMailer = true, Ip = _clientIpProvider.Get(), SignUpMethodId = SignUpMethods.Google, ReferralId = model.ReferralId }); var UserData = new LoginWithGoogleFormDataViewModel { Email = model.Email, SocialLoginId = model.SocialLoginId, ChannelId = Channels.Feel, SignUpMethodId = SignUpMethods.Google, }; var authenticated = await _authenticationHelper.AuthenticateGoogleUser(UserData, u => { return(Task.FromResult(true)); }); // adding user to mailChimp contacts try { var query = await _querySender.Send(new UserSearchQuery { Email = model.Email, ChannelId = Channels.Feel, SignUpMethodId = SignUpMethods.Google, }); await _mailChimpProvider.AddFILMember(new MCUserModel { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, PhoneCode = model.PhoneCode, PhoneNumber = model.PhoneNumber, IsCreator = false, SignUpType = "Google" }, query.Country); } catch (Exception e) { _logger.Log(Logging.Enums.LogCategory.Error, e); } return(new GoogleSignInResponseViewModel { Success = authenticated, Session = await _sessionProvider.Get() }); } } catch (Exception ex) { _logger.Log(Logging.Enums.LogCategory.Error, ex); return(new GoogleSignInResponseViewModel { Success = false }); } } else { return(new GoogleSignInResponseViewModel { Success = false }); } }