AWS Free Certification Courses

Please go to our Blogger site at blog.backspace.academy This site will no longer be updated.

Facebook Sign in with Passport

 

Set up node.js on a US-East region Amazon Linux EC2 instance and configure firewall settings for HTTP access.

On your EC2 instance create a new app using the express generator.

$ express CognitoExample

$ cd CognitoExample

$ npm install

 

Do npm start and check your browser that you see the default Express page OK.

Install Passport, Passport-Facebook, the AWS javascript SDK and colors (for multi colored console output). Use --save to add to the package.json file.

$ npm install passport --save

$ npm install passport-facebook --save

$ npm install aws-sdk --save

$ npm install colors --save

 

Open in your editor www from the bin folder and change port to 8080.

Open in your editor index.jade from the views folder and change the Express sample page to a Facebook login page:

extends layout

block content

  h1= title

  p Please log in. We only have access to your name and facebook id number. We do not collect sensitive information such as email addresses.

  a(href='/auth/facebook') Sign in with Facebook

 

Open in your editor index.js from the routes folder and add:

var passport = require('passport');

var FacebookStrategy = require('passport-facebook').Strategy;

var AWS = require('aws-sdk');

var colors = require('colors');

 

Next add variables:

AWS_ACCOUNT_ID - This is your AWS account number.

COGNITO_IDENTITY_POOL_ID - You can get this from your Cognito dashboard by selecting Edit Identity Pool

IAM_ROLE_ARN - This is the IAM role created when you created your Cognito pool. You can get this from the the main Services menu - IAM - Roles - then select the role for your identity pool.

FACEBOOK_APP_ID and FACEBOOK_APP_SECRET - From the facebook app page.

 

var AWS_ACCOUNT_ID = 'XXXXXXXX';

var AWS_Region = 'us-east-1';

var COGNITO_IDENTITY_POOL_ID = 'us-east-XXXXXXXXXXXXXXXXXXX';

var COGNITO_IDENTITY_ID, COGNITO_SYNC_TOKEN, AWS_TEMP_CREDENTIALS;

var cognitosync;

var IAM_ROLE_ARN = 'arn:aws:iam::XXXXXXXXX:role/Cognito_AWSCognitoTutorialAuth_DefaultRole';

var COGNITO_SYNC_COUNT;

var COGNITO_DATASET_NAME = 'TEST_DATASET';

var FACEBOOK_APP_ID = 'XXXXXXXXXXXX';

var FACEBOOK_APP_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXX';

var FACEBOOK_TOKEN;

var FACEBOOK_USER = {

  id: '',

  first_name: '',

  gender: '',

  last_name: '',

  link: '',

  locale: '',

  name: '',

  timezone: 0,

  updated_time: '',

  verified: false

};

var userLoggedIn = false;

var cognitoidentity = new AWS.CognitoIdentity();

 

Now let's add our code for Passport to collect the Facebook token:

router.use(passport.initialize());

router.use(passport.session());

 

passport.use(new FacebookStrategy({

  clientID: FACEBOOK_APP_ID,

  clientSecret: FACEBOOK_APP_SECRET,

  callbackURL: 'http://dev.ap-robotics.com/auth/facebook/callback'

}, function(accessToken, refreshToken, profile, done) {

  process.nextTick(function() {

    FACEBOOK_TOKEN = accessToken; 

    FACEBOOK_USER = profile._json;

    userLoggedIn = true;

    done(null, profile);

  });

}));

 

passport.serializeUser(function(user, done) {

  done(null, user);

});

 

passport.deserializeUser(function(obj, done) {

  done(null, obj);

});

 

Now lets add our success and error callbacks:

/* GET Facebook page. */

router.get('/auth/facebook', passport.authenticate('facebook'));

 

/* GET Facebook callback page. */

router.get('/auth/facebook/callback', passport.authenticate('facebook', {

  successRedirect: '/success',

  failureRedirect: '/error'

}));

 

/* GET Facebook success page. */

router.get('/success', function(req, res, next) {

  console.log('FACEBOOK_TOKEN:'.green + FACEBOOK_TOKEN); 

  res.send('Logged in as ' + FACEBOOK_USER.name + ' (id:' + FACEBOOK_USER.id + ').');

});

 

/* GET Facebook error page. */

router.get('/error', function(req, res, next) {

  res.send("Unable to access Facebook servers. Please check internet connection or try again later.");

});

 

Now run the app with npm start and you should get the success page after you have logged in from the browser.:

Logged in as Paul Coady (id:XXXXXXXXXXXXXX).

The console output should be something like:

GET / 200 395ms - 338b

GET / 200 18ms - 338b

GET /stylesheets/style.css 200 5ms - 110b

GET /auth/facebook 302 4ms - 388b

GET /auth/facebook/callback?code=AQD4e7zDMnHkQxtEO-XXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXX_XXXXXXXXXXXXXXXXXXXXXX 302 347ms - 72b

FACEBOOK_TOKEN:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

GET /success 304 2ms

Now that you have your Facebook token you can now use it to get the CognitoID credentials.

Like this tutorial? Please click the share buttons to tell others.

Next - Get CognitoID Credentials

 

 

AWS Free Certification Courses