Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6114801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:01:06+00:00 2026-05-23T15:01:06+00:00

I am integrating in my application a sign on using facebook and twitter. I

  • 0

I am integrating in my application a sign on using facebook and twitter. I intend to later on down the line include other sign on providors. I’ve actually build it from some open source code I found online but I think I’ve made a mess of things here.

My db sturcture is as below:

Users
ID|NAME|....|EMAIL|PASSWORD

USER SIGNONS
USER_ID | SIGNIN_TYPE| SIGNIN_ID

Wheneevr anyone creates an account using by signing in using facebook or twitter account – an entry is made in the user signons table indicating that the user has a signintype of ‘facebook’ or ‘twitter’.

I’ve used the following code below for authentication:

public function loginAction() {

    $this->ajaxInit();
    
// get an 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 a user is already logged
// this checks if he is logged into an open id providor
    /*if ($auth->hasIdentity()) {
    return $this->_redirect('/index/index');
}*/

// if the user is not logged, the do the logging
// $openid_identifier will be set when users 'clicks' on the 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 will be set after first query to the openid provider
$openid_mode = $this->getRequest()->getParam('openid_mode', null);

// this one will be set by facebook connect
$code = $this->getRequest()->getParam('code', null);

// while this one will be set by twitter
$oauth_token = $this->getRequest()->getParam('oauth_token', null);


// do the first query to an 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 {
        // for openid
        $adapter = $this->_getOpenIdAdapter($openid_identifier);

        // specify what to grab from the provider and what extension to use
        // for this purpose
        $toFetch = _config('openid', 'tofetch');
        // for google and 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 a user is redirect to the provider for loging
        
    $result = $auth->authenticate($adapter);
        
    // the following two lines should never be executed unless the 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) {
    // this will be exectued after provider redirected the user back to us
    if ($code) {
        // for facebook
        $adapter = $this->_getFacebookAdapter();
    } else if ($oauth_token) {
        // for twitter
        $adapter = $this->_getTwitterAdapter()->setQueryData($_GET);
    } else {
        // for openid                
        $adapter = $this->_getOpenIdAdapter(null);

        // specify what to grab from the provider and what extension to use
        // for this purpose
        $ext = null;
        
        $toFetch = _config('openid');
        
        // for google and 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) {
            // for openId
            $toStore['properties'] = $ext->getProperties();
                $options['signin_type'] = 'open_id';
                $toStore['signin_type'] = 'open_id';
                $options['signin_id'] = $auth->getIdentity();
        } else if ($code) {
            // for 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 into is I’m building a search for your friends feature. I got it to work with facebook that was easy. However for twitter I wanted to use the Zend frameworks Zend_Service_Twitter code. However I found out I needed the username inorder to login and use an access token as well – I found out I’m not exactly storing the username at this point.

CUrrently theres only one place in my entire program which does the authenticating and its the code posted above accessible via www.mysite.com/accounts/login

There’s something wrong with my design but I can’t uite tell what. The thing is that now I can’t login to a twitter account to retrieve any user details. However I can sign in via twitter since I’m storing just enough to authenticate that a user has signed in using a twitter account and that a corresponding user exists with the authenticated account. Apart from that once the user has signed in I can’t access the users twitter information.

Facebook makes it easy I guess because they have a dedicated api to handle this. This issue will defnitely cause me trouble later on if I choose to add more signons.

Any help would be most appreciated here. The code above of course doesn’t have provision to associate acocunts to users already logged in.

How should I structure my login code and tables.

I’m using the zend framework here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:01:07+00:00Added an answer on May 23, 2026 at 3:01 pm

    From what I understand your problem is that you do not store username and access_token after authenticating with Twitter. I think you should be able to get this info, and subsequently, store it as follows (the new bits are marked by 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 be an instance of Zend_Oauth_Token_Access
            // and it should contain screen_name, auth_token, etc.
            // to make sure it contains what it should do:
            // var_dump($accessToken); exit;            
    
            /* new line */ 
            $toStore['access_token'] = $accessToken;
    
            $options['signin_id'] = $identity['user_id'];                   
        }
    

    So assuming that everything went fine here, your $accessToken should be stored together with your identity. So, to use it somewhere else, you could do as 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 {
                 // do whatever twitter operation you want (or is permitted) ,e.g.
                 $response   = $twitter->status->update('My Great Tweets'); 
    
                 var_dump($response);
    
            } catch (Exception $e) {
    
                // In case something went wrong, e.g. our access token expired or is wrong
                var_dump($e->getMessage());
            }
         }
    

    Hope this helps or at least give some clues what to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a small application and thinking about integrating BLAST or other local
I am developing an Android application. Here I'm integrating the facebook api to my
i am integrating twitter application in my application my application is created in xcode
I am working on integrating the social network vkontakte into my desktop application using
I have created android application integrating with twitter and my application wants to authenticate
I am integrating paypal service in my application using Activemerchant. Now I want to
I am integrating twitter bootstrap css into my application. Going along just fine,but I
I am integrating security to my MVC application using spring security. My web.xml snippet
I'm currently integrating Facebook Connect with my application which has an existing users table.
I am currently integrating twitter bootstrap. My application layout currently includes my flash messages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.