Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Emit event on twitter connected

  • Loading branch information...
commit 9c676b9148f9315dd05f475ea659a8b0972d3cce 1 parent 3f5286a
Anatoliy Chakkaev authored
Showing with 103 additions and 70 deletions.
  1. +25 −0 README.md
  2. +78 −70 index.js
View
25 README.md
@@ -16,6 +16,8 @@ Configure `config/twitter.yml` with your twitter app credentials:
url: "http://example.com"
key: key
secret: secret
+ connectPath: /twitter_connect
+ callbackPath: /twitter_callback
Callback path: `/twitter_callback`
@@ -25,3 +27,26 @@ Usage
In application add link to twitter connect `/twitter_connect`:
<%- link_to("Connect to twitter", '/twitter_connect') %>
+
+By default twitter connect path is `/twitter_connect`, but it can be configured in `config/twitter.yml`
+Callback path can be configured too, see example below.
+
+When app will connected with twitter, global `app` object will receive message `twitterConnect`
+as event emitter, so you can:
+
+ app.on('twitterConnect', function (user, req) {
+ // find or create account for user
+ // save user id in req.session
+ });
+
+It is fine put code like this into user model (`app/models/user.js`):
+
+ app.on('twitterConnect', function (twitter, req) {
+ User.findOne({twitterId: twitter.id}, function (err, user) {
+ if (user) {
+ req.session.user_id = user.id;
+ } else {
+ User.register({twitter: twitter});
+ }
+ });
+ });
View
148 index.js
@@ -6,28 +6,17 @@ var _twitterConsumerSecret;
var _host;
var _apiHost = 'http://twitter.com';
var _apiSecureHost = 'https://twitter.com';
+var _callbackPath = '/twitter_callback';
+
+var TWITTER_CONNECT_EVENT = 'twitterConnect';
function consumer () {
return new oauth.OAuth(
_apiHost + "/oauth/request_token", _apiSecureHost + "/oauth/access_token",
- _twitterConsumerKey, _twitterConsumerSecret, "1.0A", _host + "/twitter_callback", "HMAC-SHA1");
-}
-
-function redirectBack (req, res, flash) {
- var location = req.session && req.session.beforeTwitterAuth || '/';
- console.log('redirect to', location);
- delete req.session.beforeTwitterAuth;
- if (flash) {
- if (flash.error) {
- req.flash('error', flash.error);
- } else if (flash.info) {
- req.flash('info', flash.info);
- }
- }
- res.redirect(location);
+ _twitterConsumerKey, _twitterConsumerSecret, "1.0A", _host + _callbackPath, "HMAC-SHA1");
}
-exports.init = function () {
+exports.init = function initTwitterConnect () {
try {
var settings = require('yaml').eval(require('fs').readFileSync(app.root + '/config/twitter.yml').toString('utf8'))[app.settings.env];
} catch (e) {
@@ -35,67 +24,86 @@ exports.init = function () {
console.log('Error:', e.message);
}
if (settings) {
- _twitterConsumerKey = settings.key;
- _twitterConsumerSecret = settings.secret;
- _host = settings.url;
+ initApp(settings);
+ }
+};
- app.get('/twitter_connect', function (req, res) {
- console.log(req.headers);
- req.session.beforeTwitterAuth = req.headers.referer;
- console.log('save referer:', req.session.beforeTwitterAuth);
- delete req.session.twitter;
- consumer().getOAuthRequestToken(gotToken);
+function initApp (settings) {
+ _twitterConsumerKey = settings.key;
+ _twitterConsumerSecret = settings.secret;
+ _host = settings.url;
- function gotToken (error, oauthToken, oauthTokenSecret, results) {
- if (error) {
- redirectBack(req, res, {error: "Error getting OAuth request token : " + sys.inspect(error)});
- } else {
- req.session.twitter = {
- oauthRequestToken: oauthToken,
- oauthRequestTokenSecret: oauthTokenSecret
- };
- console.log('got new token');
- res.redirect(_apiSecureHost + "/oauth/authorize?oauth_token=" + oauthToken);
- }
+ app.get(settings.connectPath || '/twitter_connect', function (req, res) {
+ console.log(req.headers);
+ req.session.beforeTwitterAuth = req.headers.referer;
+ delete req.session.twitter;
+ consumer().getOAuthRequestToken(gotToken);
+
+ function gotToken (error, oauthToken, oauthTokenSecret, results) {
+ if (error) {
+ redirectBack(req, res, {error: "Error getting OAuth request token : " + sys.inspect(error)});
+ } else {
+ req.session.twitter = {
+ oauthRequestToken: oauthToken,
+ oauthRequestTokenSecret: oauthTokenSecret
+ };
+ res.redirect(_apiSecureHost + "/oauth/authorize?oauth_token=" + oauthToken);
}
- });
+ }
+ });
+
+ if (settings.callbackPath) {
+ _callbackPath = callbackPath;
+ }
- app.get('/twitter_callback', function (req, res) {
- console.log('callback');
- consumer().getOAuthAccessToken(
- req.session.twitter.oauthRequestToken,
- req.session.twitter.oauthRequestTokenSecret,
- req.query.oauth_verifier,
- twitterCallback
- );
+ app.get(settings.callbackPath || '/twitter_callback', function (req, res) {
+ consumer().getOAuthAccessToken(
+ req.session.twitter.oauthRequestToken,
+ req.session.twitter.oauthRequestTokenSecret,
+ req.query.oauth_verifier,
+ twitterCallback
+ );
- function twitterCallback (error, oauthAccessToken, oauthAccessTokenSecret, results) {
- if (error) {
- res.send("Error getting OAuth access token : " + sys.inspect(error), 500);
- return;
- }
- consumer().get(
- _apiHost + "/account/verify_credentials.json",
- oauthAccessToken,
- oauthAccessTokenSecret,
- gotData);
+ function twitterCallback (error, oauthAccessToken, oauthAccessTokenSecret, results) {
+ if (error) {
+ res.send("Error getting OAuth access token : " + sys.inspect(error), 500);
+ return;
+ }
+ consumer().get(
+ _apiHost + "/account/verify_credentials.json",
+ oauthAccessToken,
+ oauthAccessTokenSecret,
+ gotData);
- function gotData (error, data, response) {
- if (error) {
- redirectBack(req, res, {error: "Error getting twitter screen name : " + sys.inspect(error)});
- console.log('gotData:', error);
- } else {
- if (typeof data === 'string') {
- data = JSON.parse(data);
- }
- req.session.twitter = data;
- req.session.twitter.oauthAccessToken = oauthAccessToken;
- req.session.twitter.oauthAccessTokenSecret = oauthAccessTokenSecret;
- console.log(req.session.twitter);
- redirectBack(req, res);
+ function gotData (error, data, response) {
+ if (error) {
+ redirectBack(req, res, {error: "Error getting twitter screen name : " + sys.inspect(error)});
+ console.log('gotData:', error);
+ } else {
+ if (typeof data === 'string') {
+ data = JSON.parse(data);
}
+ req.session.twitter = data;
+ req.session.twitter.oauthAccessToken = oauthAccessToken;
+ req.session.twitter.oauthAccessTokenSecret = oauthAccessTokenSecret;
+ app.emit(TWITTER_CONNECT_EVENT, req.session.twitter, req, res);
+ redirectBack(req, res);
}
}
- });
+ }
+ });
+}
+
+function redirectBack (req, res, flash) {
+ var location = req.session && req.session.beforeTwitterAuth || '/';
+ delete req.session.beforeTwitterAuth;
+ if (flash) {
+ if (flash.error) {
+ req.flash('error', flash.error);
+ } else if (flash.info) {
+ req.flash('info', flash.info);
+ }
}
-};
+ res.redirect(location);
+}
+

0 comments on commit 9c676b9

Please sign in to comment.
Something went wrong with that request. Please try again.