• I’m trying to use the following code to authorize a Facebook user and log him in as a new user

    function connect_to_facebook(){
    	global $current_user;
       if(!is_user_logged_in()) {
    	   echo is_user_logged_in();
    	   if(!isset($_SESSION)) session_start();
    		   if(!isset($_SESSION['access_token'])){
    		   $code = $_REQUEST["code"];
    		   if(empty($code)) {
    			   $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
    
    				 $login = sprintf('https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=APP_URL&state=%s&scope=email',APP_ID,$_SESSION['state']);
    				  echo("<script> top.location.href='" . $login . "'</script>");
    				  die();
    		   }
    
    		   if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
    			 $token_url = "https://graph.facebook.com/oauth/access_token?"
    			   . "client_id=" . APP_ID . "&redirect_uri=" . urlencode(APP_URL)
    			   . "&client_secret=" . APP_SECRET . "&code=" . $code;
    
    			 $response = wp_remote_get($token_url);
    			 $params = null;
    			  parse_str($response['body'], $params);
    		   }
    		   else {
    			 die("The state does not match. You may be a victim of CSRF.");
    		   }
    		   }
    			$_SESSION['access_token'] = isset($_SESSION['access_token'])? $_SESSION['access_token'] : $params['access_token'];
    			$graph_url = "https://graph.facebook.com/me?access_token=" . $_SESSION['access_token'];
    			$response = wp_remote_get($graph_url);
    			$user = json_decode($response['body']);
    			$new_user = get_user_by('login', $user->last_name);
    			if(!$new_user){
    				$userdata = array(
    				  'user_pass'       =>  wp_generate_password(),
    				  'user_login'  =>  $user->last_name, // $user->username,
    				  'user_nicename'   =>  $user->name,
    				  'user_email'  =>  $user->email,
    				  'display_name'    =>  $user->name,
    				  'nickname'        =>  $user->first_namee,
    				  'first_name'  =>  $user->first_name,
    				  'last_name'       =>  $user->last_name,
    				  'role'            =>  'subscriber'
    				);
    				$user_id = wp_insert_user($userdata);
    				// create new user
    				$new_user = get_user_by('id',$user_id);
    			}
    			wp_set_current_user($new_user->ID,$new_user->user_login);
    			wp_set_auth_cookie($new_user->ID);
    			$creds = array();
    			$creds['user_login'] = $new_user->user_login;
    			$creds['user_password'] = $new_user->user_pass;
    			$creds['remember'] = true;
    			$current_user = wp_signon( $creds, true);
    			if ( is_wp_error($current_user) )
    			   echo $current_user->get_error_message();
    		}
    }
    add_action('init', 'connect_to_facebook',0);

    problem is I can’t move pass the home page as it keeps showing is_user_logged_in as false even though I see the user being created in wordpress.

    what seems to be the issue

  • The topic ‘How to use Facebook user as WordPress user’ is closed to new replies.