php - Getting Facebook session on Wordpress -
background
we have wordpress site requires special kind of facebook authentication. wp site needs authenticate user different app, , log user app. web app built us, , has api wp site uses interact it. before rowdy, whole authentication process works using normal login form. using api, can see user or not logged app, , display on wp site.
problem
as stated, need facebook authentication. using facebooks php sdk v4 , have built custom plugin keep code seperate theme. when user clicks on fb icon, shows popup correct redirect url. after while, popup closes, there nothing in result. few placed var_dumps
reveal i'm not getting facebookredirectloginhelper
. means can't session in turns means no user info.
code
as stated, i've created plugin (ns-social.php
) handles everything. here file:
/** * plugin name: ns social plugin */ require_once 'ns-social-init.php'; require_once 'ns-callback-functions.php'; require_once 'ns-facebook.php' ; $fb = null; add_action('plugins_loaded','load_fb'); function load_fb() { global $fb; $fb = new wp_facebook(); } /** * social login check */ function get_fb_url() { global $fb; return $fb->login_url(); }
ns-social-init.php
starts session fb can use:
/** * functions required during initialisation of wordpress or plugin */ add_action('plugins_loaded', 'start_session'); function start_session() { if (!session_id()) { session_start(); } }
ns-callback-functions.php
contains callback functions redirects. these shortcodes placed in pages, url www.site.com/facebook-callback
, page have [facebook-callback]
in handle request.
add_shortcode('facebook_callback', 'facebook_callback'); function facebook_callback() { global $fb; if (isset($_get['error'])) { if ($_get['error'] == 'access_denied') { echo "<script> if(window.opener != null) { window.close(); } </script>"; exit; } } $session = $fb->get_session(); $userarr = $fb->get_user(); $user['name'] = $userarr['first_name']; $user['surname'] = $userarr['last_name']; $user['email'] = $userarr['email']; $user['verified'] = $userarr['verified']; $_session['registeruser'] = $user; $_session['registertype'] = 'facebook'; $action = ""; die(var_dump($_session,true)); if (user_exists($user['email'])) { $action = '?login'; } wp_redirect(home_url('social-register/' . $action)); }
and last not least, ns-facebook.php
file:
use facebook\facebookredirectloginhelper; use facebook\facebooksession; use facebook\facebookrequest; class wp_facebook { var $helper; var $session; var $permissions; var $loginurl; public function __construct() { // initialize sdk facebooksession::setdefaultapplication('0appid145', '00hahaitsmysecret23523'); $this->permissions = ['public_profile', 'email']; $this->helper = new facebookredirectloginhelper(home_url('facebook-callback')); $this->loginurl = $this->helper->getloginurl($this->permissions); } /** * returns login url. * * @return string */ public function login_url() { return $this->loginurl; } /** * returns current user's info array. */ public function get_user($session = null) { if(empty($session)) $session = $this->session; if($session) { /** * retrieve user's profile information */ // graph api request user data $request = new facebookrequest($session, 'get', '/me'); $response = $request->execute(); // response array $user = $response->getgraphobject()->asarray(); return $user; } return false; } public function get_session() { try { $this->session = $this->helper->getsessionfromredirect(); } catch(facebookrequestexception $ex) { // when facebook returns error } catch(\exception $ex) { // when validation fails or other local issues } if ($this->session) { return $this->session; } } }
what have tried
i've gone through quite few questions on so. have noticed is, when first run page, fbrlh_state
in session is, abcdef
example. when response after clicking login button, fbrlh_state
xyz
. don't know if has effect on outcome. if could, how use state? don't set it, i'm assuming fb sdk does.
tl;dr
fb php sdk v4 not sending when use facebookredirectloginhelper
. why this, , how fix it?
did make test offical facebookredirectloginhelper official github? have used facebook sdk couple of times , never had problem missing returns.
Comments
Post a Comment