Authnticating Magento users with Twitter Oauth

Twitter Oauth which is a greate way to pull users on our website without registering themselves on website.You can use this authentication as a parallel system of your site registration like Fconnect and Google connect.With this you simply need to just logging once from your twitter account and allow us to share your very initial information (which is safe with us) from twitter. With this initial information we will register user at our site so that from the next time if he wants to logged in to our site then s/he only needs to enter their tweeter username and will get logged in to our site as well as twiiter itself.

To do this you have to follow some steps which as as follows:

  1. Register a new application on twitter by the Twitter Application
  2. Put your essential information about your website here. Here callback URL is the url which will get called after successfull authentication.
  3. From the next page you will get consumer Key and Consumer secret which we need soon.

Your setting has been done on twitter. Now back to our application in magento

Step 1: Create a new link after My Account link on Your magento site which says Login with Twitter Oauth

Step 2: This link will taken to customer/account/twitter_login/ page which will ask you for your twitter username and submit button

 

Here we are asking twitter username so that we can check if you already available in our database or not. If you want to learn how to create block from controller then read from here . As a recommendation you should overided you core account controller for more info read

Step 3: Add some attrubuters in customer table

<pre>$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('customer', 'oauth_provide', array(

'type' => 'text',

'required' => false,

));

$setup->addAttribute('customer', 'oauth_uid', array(

'type' => 'text',

'required' => false,

));

$setup->addAttribute('customer', 'oauth_token', array(

'type' => 'text',

'required' => false,

));

$setup->addAttribute('customer', 'oauth_secret', array(

'type' => 'text',

'required' => false,

));

$setup->addAttribute('customer', 'twitter_username', array(

'type' => 'text',

'required' => true,

));

Above code will add some more attribute to the customer table.

Step 4: Download best twitter Oauth library from ; and after extracting it copy it in lib directory rename this directory to twitteroauth

Step 5: Suppose we have create the form action at step 2 is

customer/account/oauthtest

now you need to create oauthtestAction function in which we will add following code

Authorization


protected function oauthtestAction()

{

$path = getcwd()."/lib/twitteroauth/twitteroauth.php";

// creating TwitterOAuth instance

$twitter_oauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');

// Requesting authentication tokens, the parameter is the URL we will be redirected to

$return url = Mage::getUrl('customer/account/receiveoauthtoken')

$request_token_val = $twitter_oauth->getRequestToken($return url);

// Saving them into the session

$session = $this->_getSession();

$session->setOauthToekn($request_token['oauth_token']);

$session->setOauthToeknSecret($request_token['oauth_token_secret']);

// If we are right..

if($twitter_oauth->http_code==200){

// Let's generate the URL and redirect

$url = $twitter_oauth->getAuthorizeURL($request_token['oauth_token']);

header('Location: '. $url);

} else {

// all is not well

die('Recheck the way');

}

}

Step 6: Now you will have to receive twitter information in another function on same file. See below code


protected function receiveoauthtokenAction()

{

$path = getcwd()."/lib/twitteroauth/twitteroauth.php";

$session = $this->_getSession();

$auth_token = $session->getOauthToekn();

$auth_token_service = $session->getOauthToeknSecret();

// creating TwitterOAuth instance

$twitter_oauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET', $auth_token, $auth_token_service);

// Requesting the access token

$access_token = $twitter_oauth->getAccessToken($_GET['oauth_verifier']);

// create a global session

$_SESSION['access_token'] = $access_token;

// Let's get the user's info

$twitter_user_info = $twitter_oauth->get('account/verify_credentials');

// see user information is ready with you

print_r($user_info);

}

Step 7: Your authentication is done now and you are receiving users data from twitter now save it in you customer table I understand now you are able to do it if not let me know… Next time onward when user comes from step 2 then you will check if user is already avialable then recieve token from our database and make it login to your site.

Related posts:

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)