zend framework - Need to sign in using twitter plus retrieve friends.. think I've messed it all up -
hi guys i'm integrating in application sign on using facebook , twitter. intend later on down line include other sign on providors. i've build open source code found online think i've made mess of things here.
my db sturcture below:
users id|name|....|email|password
user signons user_id | signin_type| signin_id
whenevr creates account using signing in using facebook or twitter account - entry made in user signons table indicating user has signintype of 'facebook' or 'twitter'.
i've used following code below authentication:
public function loginaction() { $this->ajaxinit(); // instace of zend_auth $auth = zend_auth::getinstance(); $p = $this->_getallparams(); if(isset($p['redirectto'])){ $this->setredirect($p['redirectto']); }else{ $redirect = explode('?', $_server['http_referer']); $this->setredirect($redirect[0]); } // check if user logged // checks if logged open id providor /*if ($auth->hasidentity()) { return $this->_redirect('/index/index'); }*/ // if user not logged, logging // $openid_identifier set when users 'clicks' on account provider $openid_identifier = $this->getrequest()->getparam('openid_identifier', null); if($this->getrequest()->getparam('rememberme', null)>0){ //zend_session::rememberme(60 * 60 * 24 * 30); } // $openid_mode set after first query openid provider $openid_mode = $this->getrequest()->getparam('openid_mode', null); // 1 set facebook connect $code = $this->getrequest()->getparam('code', null); // while 1 set twitter $oauth_token = $this->getrequest()->getparam('oauth_token', null); // first query authentication provider if ($openid_identifier) { if ('https://www.twitter.com' == $openid_identifier) { $adapter = $this->_gettwitteradapter($redirect); _log('inside here'); } else if ('https://www.facebook.com' == $openid_identifier) { $adapter = $this->_getfacebookadapter($redirect); } else { // openid $adapter = $this->_getopenidadapter($openid_identifier); // specify grab provider , extension use // purpose $tofetch = _config('openid', 'tofetch'); // google , yahoo use atributeexchange extension if ('https://www.google.com/accounts/o8/id' == $openid_identifier || 'http://me.yahoo.com/' == $openid_identifier) { $ext = $this->_getopenidext('ax', $tofetch); } else { $ext = $this->_getopenidext('sreg', $tofetch); } $adapter->setextensions($ext); } // here user redirect provider loging $result = $auth->authenticate($adapter); // following 2 lines should never executed unless redirection faild. //$this->_helper->flashmessenger('redirection faild'); if(strstr($redirect, 'import')){ return $this->_redirect($redirect.'?cmsg=redirection-failure'); } return $this->_redirect('/accounts/sign-in?error=redirection-failure'); }else if ($openid_mode || $code || $oauth_token) { // exectued after provider redirected user if ($code) { // facebook $adapter = $this->_getfacebookadapter(); } else if ($oauth_token) { // twitter $adapter = $this->_gettwitteradapter()->setquerydata($_get); } else { // openid $adapter = $this->_getopenidadapter(null); // specify grab provider , extension use // purpose $ext = null; $tofetch = _config('openid'); // google , yahoo use atributeexchange extension if (isset($_get['openid_ns_ext1']) || isset($_get['openid_ns_ax'])) { $ext = $this->_getopenidext('ax', $tofetch); } else if (isset($_get['openid_ns_sreg'])) { $ext = $this->_getopenidext('sreg', $tofetch); } if ($ext) { $ext->parseresponse($_get); $adapter->setextensions($ext); } } $result = $auth->authenticate($adapter); if ($result->isvalid()) { $tostore = array('identity' => $auth->getidentity()); $options = array(); if ($ext) { // openid $tostore['properties'] = $ext->getproperties(); $options['signin_type'] = 'open_id'; $tostore['signin_type'] = 'open_id'; $options['signin_id'] = $auth->getidentity(); } else if ($code) { // facebook $msgs = $result->getmessages(); $tostore['properties'] = (array) $msgs['user']; $options['signin_type'] = 'facebook'; $tostore['signin_type'] = 'facebook'; $options['signin_id'] = $auth->getidentity(); } else if ($oauth_token) { $identity = $result->getidentity(); $twitteruserdata = (array) $adapter->verifycredentials(); $tostore = array('identity' => $identity['user_id']); if (isset($twitteruserdata['status'])) { $twitteruserdata['status'] = (array) $twitteruserdata['status']; } _log($twitteruserdata); $tostore['properties'] = $twitteruserdata; $options['signin_type'] = 'twitter'; $tostore['signin_type'] = 'twitter'; $options['signin_id'] = $identity['user_id']; } $user = _factory('people')->get(false, $options); if(count($user)>0){ $user = array_pop($user); $auth->getstorage()->write($user['account_email']); return $this->_redirect($this->setredirect); //return $this->_redirect('/accounts/index'); }else{ $auth->getstorage()->write($tostore); return $this->_redirect('/accounts/welcome'); } } else { return $this->_redirect('/index/index'); } } }
the problem i've run i'm building search friends feature. got work facebook easy. twitter wanted use zend frameworks zend_service_twitter code. found out needed username inorder login , use access token - found out i'm not storing username @ point.
currently theres 1 place in entire program authenticating , code posted above accessible via www.mysite.com/accounts/login
there's wrong design can't uite tell what. thing can't login twitter account retrieve user details. can sign in via twitter since i'm storing enough authenticate user has signed in using twitter account , corresponding user exists authenticated account. apart once user has signed in can't access users twitter information.
facebook makes easy guess because have dedicated api handle this. issue defnitely cause me trouble later on if choose add more signons.
any appreciated here. code above of course doesn't have provision associate acocunts users logged in.
how should structure login code , tables.
i'm using zend framework here.
from understand problem not store username , access_token after authenticating twitter. think should able info, , subsequently, store follows (the new bits marked comments /* new line */):
} else if ($oauth_token) { $identity = $result->getidentity(); $twitteruserdata = (array) $adapter->verifycredentials(); $tostore = array('identity' => $identity['user_id']); if (isset($twitteruserdata['status'])) { $twitteruserdata['status'] = (array) $twitteruserdata['status']; } _log($twitteruserdata); $tostore['properties'] = $twitteruserdata; $options['signin_type'] = 'twitter'; $tostore['signin_type'] = 'twitter'; /* new line */ $accesstoken = $adapter->getaccesstoken(); // $accesstoken should instance of zend_oauth_token_access // , should contain screen_name, auth_token, etc. // make sure contains should do: // var_dump($accesstoken); exit; /* new line */ $tostore['access_token'] = $accesstoken; $options['signin_id'] = $identity['user_id']; } so assuming went fine here, $accesstoken should stored identity. so, use somewhere else, follows:
$auth = zend_auth::getinstance(); if ($auth->hasidentity()) { $identity = $auth->getidentity(); $accesstoken = $identity['access_token']; // zend_service_twitter can accept instance of zend_oauth_token_access $twitter = new zend_service_twitter(array( 'accesstoken' => $accesstoken )); try { // whatever twitter operation want (or permitted) ,e.g. $response = $twitter->status->update('my great tweets'); var_dump($response); } catch (exception $e) { // in case went wrong, e.g. our access token expired or wrong var_dump($e->getmessage()); } } hope helps or @ least give clues do.
Comments
Post a Comment