Your address will show here +12 34 56 78

Home Forums WP ESTATE – Real Estate WordPress Theme FORUM Google Login not working Reply To: Google Login not working

#124146

Fiz
Participant

Hey,
I have found the issue and its fix. I hope people would get help from this. In wpresidence/libs/3rdparty.php

After Google provide you with the access token, it gets information from your User’s google profile. As we all know google have an option to display nickname as well and and full name is like NAME (Nick). and they are setting full name as your username but wordpress doesn’t allow brackets () in username. So i just extract the username from email and set it as wordpress username. below is code to fix the issue you can copy and replace the code in above mentioned file. I hope i helped.

////////////////////////////////////////////////////////////////////////////////
/// estate_google_oauth_login  Login
////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_google_oauth_login') ):

function estate_google_oauth_login($get_vars){
    set_include_path( get_include_path() . PATH_SEPARATOR . get_template_directory().'/libs/resources');
    $allowed_html   =   array();
    require_once 'src/Google_Client.php';
    require_once 'src/contrib/Google_Oauth2Service.php';
    $google_client_id       =   esc_html ( get_option('wp_estate_google_oauth_api','') );
    $google_client_secret   =   esc_html ( get_option('wp_estate_google_oauth_client_secret','') );
   $google_redirect_url    =   get_dashboard_profile_link();
    $google_developer_key   =   esc_html ( get_option('wp_estate_google_api_key','') );

    $gClient = new Google_Client();
    $gClient->setApplicationName('Login to GwadarLinks');
    $gClient->setClientId($google_client_id);
    $gClient->setClientSecret($google_client_secret);
    $gClient->setRedirectUri($google_redirect_url);
    $gClient->setDeveloperKey($google_developer_key);
    $google_oauthV2 = new Google_Oauth2Service($gClient);
    
    if (isset($_GET['code'])) { 
        $code=wp_kses($_GET['code'],$allowed_html);
        $gClient->authenticate($code);

    }
    
    
    
    if ($gClient->getAccessToken()) 
    {     
        $allowed_html      =   array();
        $dashboard_url     =   get_dashboard_profile_link();
        $user              =   $google_oauthV2->userinfo->get();
        $user_id           =   $user['id'];		
		$split_email = explode( '@' , $user['email']);
		$custom_username = $split_email[0];
		$full_name         =   wp_kses($user['name'], $allowed_html);
        $email             =   wp_kses($user['email'], $allowed_html);

        $profile_url                      = filter_var($user['link'], FILTER_VALIDATE_URL);
        $profile_image_url                = filter_var($user['picture'], FILTER_VALIDATE_URL);
       
        
        
        
        $full_name=  str_replace(' ','.',$full_name);  

        wpestate_register_user_via_google($email,$custom_username,$user_id); 
        $wordpress_user_id=username_exists($custom_username);
        wp_set_password( $code, $wordpress_user_id ) ;
        
        $info                   = array();
        $info['user_login']     = $custom_username;
        $info['user_password']  = $code;
        $info['remember']       = true;
        $user_signon            = wp_signon( $info, false );
        
 
        
        if ( is_wp_error($user_signon) ){ 
            wp_redirect( home_url() );  
        }else{
            wpestate_update_old_users($user_signon->ID);
            wp_redirect($dashboard_url);
        }
    }
    
    
    
}

endif; // end   estate_google_oauth_login 


Regards,
Fiz
fayzanzahid.com