Read article in Spanish

With current apps, you must have observed that an option is provided for one to sign in with Twitter, LinkedIn, FaceBook, Google, and other types of social media. This feature is associated with a number of advantages. First, the users will not be required to sign up for a new account. Secondly, the developers will not be required to implement the part for signing up the user.

In this post, I need to create an app which will let us sign in with Twitter. In this case, you will be required to click on the “Sign in with Twitter” button, after which you will be directed to the Twitter page. This will work in the same way that OAuth sign ins works. My assumption is that you have already installed the Node.js in your system, so we just have to initialize and configure it as shown below:

[code lang=text]
npm init npm install express node-twitter-api bluebird

[/code]

The first implementation of the app should be after the user has clicked on the button for “Sign in with Twitter.” We should make use of jQuery for the purpose of sending an Ajax request to the server-side code which will generate a request token, and then the user will be redirected to the page for sign in to Twitter. Here is the code:

[code lang=text]
<br /><br /><a href="https://code.jquery.com/jquery-2.1.4.min.js">https://code.jquery.com/jquery-2.1.4.min.js</a>

$(function() { $("#sign-with-twitter").on("click", function()
{ window.location.href = "/request-token"; }); });

<button id="sign-in-with-twitter">Sign in with Twitter</button>

[/code]

Once the user has clicked on the button, a request will then be directed for GET “/request-token.” It is after this that the request will then redirect the user to the sign in page for Twitter on the server-side

The route for handling the “/request-token” request on the server-side is as shown below:

[code lang=text]
"use strict";
var Twitter = require("node-twitter-api"),
secret = include("secret");
module.exports = function(application)
{
var twitter = new Twitter({
consumerKey: secret.twitter.consumerKey,
consumerSecret: secret.twitter.consumerSecret,
callback: secret.twitter.callbackUrl
});
var _requestSecret;
application.get("/request-token", function(req, res) {         twitter.getRequestToken(function(error, requestToken, requestSecret) {
if (error)  res.status(500).send(error);
else {
_requestSecret = requestSecret;
res.redirect("https://api.twitter.com/oauth/authenticate?oauth_token=" + requestToken);               }
});
});

};

[/code]

Once the user has signed in, redirection to the URL which we have specified will be done, so we should create a page for this. The following code can be used for doing this:

[code lang=text]
<a href="https://code.jquery.com/jquery-2.1.4.min.js">https://code.jquery.com/jquery-2.1.4.min.js</a>

$(function()
{
$.get("<a>/access-token" + location.search).done(function(user) {</a>

// Perform an activity with the signed in user's information here.
});
});

 

[/code]

Our callback page given above will just request for an access token from the server.

It is now time for us to handle the request from the callback page for the access token. This can be done as shown in the code given below:

[code lang=text]
"use strict";

var Twitter = require("node-twitter-api"),
secret = include("secret");
module.exports = function(application)
{
var twitter = new Twitter({
consumerKey: secret.twitter.consumerKey,
consumerSecret: secret.twitter.consumerSecret,
callback: secret.twitter.callbackUrl
});

var _reqSecret;

application.get("/access-token", function(req, res)
{
var reqToken = req.query.oauth_token,
verifier = req.query.oauth_verifier;
twitter.getAccessToken(reqToken, _reqSecret, verifier, function(error, accessToken, accessSecret)
{
if (error)  res.status(500).send(error);
else
twitter.verifyCredentials(accessToken, accessSecret, function(error, user)
{
if (error)  res.status(500).send(error);
else res.send(user);
});
});
});
};

[/code]

Read article in Spanish

2 comentarios sobre “Twitter Sign in with Node.js

  1. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on a variety of
    websites for about a year and am concerned about switching to another platform.
    I have heard very good things about blogengine.net. Is there a way I can transfer all my wordpress
    posts into it? Any kind of help would be greatly appreciated!

Deja un comentario