Permalink
Browse files

updated sample to use FB.napi with Step

  • Loading branch information...
prabirshrestha committed Mar 25, 2013
1 parent 522d854 commit 08380ecd4ed2b2506ec38434d473cfe6b14ef8ba
Showing with 32 additions and 25 deletions.
  1. +3 −2 samples/scrumptious/package.json
  2. +29 −23 samples/scrumptious/routes/home.js
@@ -7,6 +7,7 @@
},
"dependencies": {
"express": "3.1.0",
- "ejs": "*"
+ "ejs": "*",
+ "step": "0.0.5"
}
-}
+}
@@ -1,5 +1,6 @@
var FB = require('../../../fb'),
+ Step = require('step'),
config = require('../config');
@@ -30,37 +31,42 @@ exports.index = function(req, res) {
};
exports.loginCallback = function (req, res, next) {
- var code = req.query.code,
- accessToken = '',
- expires = 0;
+ var code = req.query.code;
if(req.query.error) {
- // user disallowed the app
- return res.send('login-error');
+ // user might have disallowed the app
+ return res.send('login-error ' + req.query.error_description);
} else if(!code) {
return res.redirect('/');
}
- // exchange code for access token
- FB.api('oauth/access_token', {
- client_id: FB.options('appId'),
- client_secret: FB.options('appSecret'),
- redirect_uri: FB.options('redirectUri'),
- code: code
- }, function (result) {
- if(!result || result.error) {
- console.log(!res ? 'error occurred' : res.error);
- return next(result); // todo: handle error
- }
+ Step(
+ function exchangeCodeForAccessToken() {
+ FB.napi('oauth/access_token', {
+ client_id: FB.options('appId'),
+ client_secret: FB.options('appSecret'),
+ redirect_uri: FB.options('redirectUri'),
+ code: code
+ }, this);
+ },
+ function extendAccessToken(err, result) {
+ if(err) throw(err);
+ FB.napi('oauth/access_token', {
+ client_id: FB.options('appId'),
+ client_secret: FB.options('appSecret'),
+ grant_type: 'fb_exchange_token',
+ fb_exchange_token: result.access_token
+ }, this);
+ },
+ function (err, result) {
+ if(err) return next(err);
- accessToken = result.access_token;
- expires = result.expires ? result.expires : 0;
+ req.session.access_token = result.access_token;
+ req.session.expires = result.expires || 0;
- // todo: extend access token
- req.session.access_token = accessToken;
- req.session.expires = expires;
- res.redirect('/');
- });
+ res.redirect('/');
+ }
+ );
};
exports.logout = function (req, res) {

0 comments on commit 08380ec

Please sign in to comment.