nodeoauth2

Facebook and Google auth module for NodeJS

npm install nodeoauth2
12 downloads in the last month

NodeOAuth2

Authentication for Facebook and Google using OAuth2. Authenticates the user and returns their access_token and basic user information.

NOTE: Does not provide a session system to provide a drop-in authentication solution. Most other Facebook node modules do this already; this library simply provides a way to authenticate. Session/account management is up to you to implement.

Dependencies

Example

NodeOAuth2 was written with the intention of being used with ExpressJS.

Initialization options

{
    // REQUIRED
    app_id:                        Your facebook app secret
    app_secret:                    Your facebook app secret
    app:                         Express app instance
    permissions:                 Comma seperated list of facebook permissions
    base_url:                     Base URL of your site/app

    // OPTIONAL
    callback_path:              The path you want facebook to redirect to.
    oauth_complete_redirect:    Callback function OR a path (string) of where to redirect to    
    auth_path:                     Entry point to start OAuth process. Defaults to /auth/facebook
}

Authentication Flow

- User visits /auth/facebook 
- Redirects to Facebook
- User returns from facebook through /oauth/facebook
- Process success or failure
- Redirect to / or execute callback function

Full example

var express         = require('express'),
    app             = express(),
    nodeOAuth         = require('NodeOAuth2'),
    redis_store        = require('connect-redis')(express);

app.configure(function(){
    app.use(express.cookieParser('somesecretkey'));
    app.use(express.session({
            secret: 'secretsessionkey',
            store: new redis_store({
                host: '127.0.0.1',
                port: '6379',
                db: 1,
                prefix: '_auth'
            })
        }
    ));
});

var google_data = {
    app_id            :'google-app-id',
    app_secret        :'google-app-secret',
    base_url         :'http://your-callback-url',
    permissions        :'list+of+scope+permissions'
};

var fb_data = {
    app_id            :'facebook-app-id',
    app_secret        :'facebook-app-secret',
    base_url         :'http://your-callback-url',
    permissions        :'list,of,permissions'
};

// ----------------------------------------------------
// -- Create and initialize handler for Facebook
// ----------------------------------------------------
var nodeFbAuth = nodeOauth.createHandler('facebook');

nodeFbAuth.initialize({
    app_id:         fb_data.app_id, 
    app_secret:     fb_data.app_secret, 
    base_url:         fb_data.base_url,
    permissions:     fb_data.permissions,
    app:             app
});

// ----------------------------------------------------
// -- Create and initialize handler for Google
// ----------------------------------------------------
var nodeGoogleAuth = nodeOauth.createHandler('google');

nodeGoogleAuth.initialize({
    app_id:         google_data.app_id, 
    app_secret:     google_data.app_secret, 
    base_url:         google_data.base_url,
    permissions:     google_data.permissions,
    app:             app
});

app.get('/', function(req, res){
    res.json(req.session);
});

app.listen(4000);
npm loves you