Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-auth-facebook

Package Overview
Dependencies
Maintainers
4
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-auth-facebook - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

31

example/facebook_server.js

@@ -1,3 +0,4 @@

var Hapi = require('hapi');
var server = new Hapi.Server();
var Hapi = require('hapi');
var server = new Hapi.Server();
var assert = require('assert');

@@ -11,7 +12,29 @@ var facebookAuth = require('../lib/index.js');

server.register({ register: facebookAuth }, {
var facebookAuthRequestUrl = '/authfacebook';
server.register({
register: facebookAuth,
options: {
handler: require('./facebook_oauth_handler'),
redirectUri: '/facebooklogin',
tokenRequestPath: facebookAuthRequestUrl
}
}, function (err) {
if (err) console.log(err);
assert(!err, 'failed to load plugin');
});
var createLoginButton = function() {
return '<a href="'+ facebookAuthRequestUrl +
'"><img src="http://i.stack.imgur.com/pZzc4.png"></a>'
};
server.route({
path: '/facebook',
method: 'GET',
handler: function(request, reply) {
reply(createLoginButton());
}
});
server.start(function() {

@@ -18,0 +41,0 @@ console.log("Server listening on " + server.info.uri);

117

lib/index.js
var env = require('env2')('.env');
var querystring = require('querystring');
var https = require('https');
var https = require('https');
exports.register = function(server, options, next) {
server.route([{
method: 'GET',
path: '/facebook',
handler: function(request, reply) {
var redirectUri = 'http://0.0.0.0:8000/facebookLogin';
var url = 'https://www.facebook.com/dialog/oauth?client_id=' + process.env.FACEBOOK_APP_ID + '&redirect_uri=' + redirectUri;
var btn = '<a href="' + url + '"><img src="http://i.stack.imgur.com/pZzc4.png"></a>';
reply(btn);
}
},
{
method: 'GET',
path: '/facebookLogin',
handler: function(request, reply) {
console.log(request.query);
var body = createAccessTokenRequestBody(request.query.code);
var getAccessTokenOpts = {
hostname: 'graph.facebook.com',
path: '/v2.3/oauth/access_token?' + body,
method: 'GET'
};
httpsRequest(getAccessTokenOpts, function(accessToken) {
console.log(accessToken);
reply('<img src="http://pix.iemoji.com/images/emoji/apple/8.3/256/light-brown-thumbs-up-sign.png">');
});
}
}]);
next();
var createAccessTokenQuery = function(request, server, options) {
var query = querystring.stringify({
client_id: process.env.FACEBOOK_APP_ID,
redirect_uri: server.info.uri + options.redirectUri,
client_secret: process.env.FACEBOOK_APP_SECRET,
code: request.query.code
});
return query;
};
var createAccessTokenReqestOpts = function(request, server, options) {
var accessTokenQuery = createAccessTokenQuery(request, server, options);
return {
hostname: 'graph.facebook.com',
path: '/v2.3/oauth/access_token?' + accessTokenQuery,
method: 'GET'
};
}
exports.register.attributes = {
name: 'hapi-auth-facebook'
var getBody = function(response, callback) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
callback(body);
});
};
function httpsRequest(options, callback) {
var httpsRequest = function(options, callback) {
var request = https.request(options, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
callback(body);
});
getBody(response, callback);
});
request.end();
}
};
function createAccessTokenRequestBody(code) {
var qs = querystring.stringify({
var redirectHandler = function(request, reply, server, options) {
var requestOpts = createAccessTokenReqestOpts(request, server, options);
httpsRequest(requestOpts, function(accessTokenData) {
var token = JSON.parse(accessTokenData).access_token;
options.handler(request, reply, token);
});
};
function createFacebookAuthReqUrl(server, redirectUri) {
host = 'https://www.facebook.com';
path = '/dialog/oauth?' + querystring.stringify({
client_id: process.env.FACEBOOK_APP_ID,
redirect_uri: 'http://0.0.0.0:8000/facebookLogin',
client_secret: process.env.FACEBOOK_APP_SECRET,
code: code
redirect_uri: server.info.uri + redirectUri
});
return qs;
return host + path;
}
var authReqHandler = function(request, reply, server, options) {
reply.redirect(createFacebookAuthReqUrl(server, options.redirectUri));
};
exports.register = function(server, options, next) {
server.route([
{
method: 'GET',
path: options.tokenRequestPath,
handler: function(request, reply) {
authReqHandler(request, reply, server, options);
}
},
{
method: 'GET',
path: options.redirectUri,
handler: function(request, reply) {
redirectHandler(request, reply, server, options);
}
}
]);
next();
}
exports.register.attributes = {
name: 'hapi-auth-facebook'
};
{
"name": "hapi-auth-facebook",
"version": "1.0.0",
"version": "1.0.1",
"description": "Simple Facebook Authentication for Hapi.js Apps",

@@ -11,3 +11,3 @@ "main": "lib/index.js",

"scripts": {
"test": "PORT=8000 node test/facebook.test.js",
"test": "PORT=8000 istanbul cover tape test/facebook.test.js",
"start": "PORT=8000 nodemon index.js",

@@ -35,10 +35,10 @@ "example": "PORT=8000 nodemon example/facebook_server.js"

"devDependencies": {
"env2": "^2.0.4",
"nodemon": "^1.8.1",
"tape": "^4.2.2"
"env2": "^2.1.1",
"hapi": "^14.1.0",
"istanbul": "^0.4.1",
"nock": "^8.0.0",
"nodemon": "^1.10.0",
"tape": "^4.6.0"
},
"dependencies": {
"hapi": "^11.1.2",
"querystring": "^0.2.0"
}
"dependencies": {}
}
# hapi-auth-_facebook_ so you can: ![img](http://i.stack.imgur.com/pZzc4.png)
[![Build Status](https://travis-ci.org/dwyl/hapi-auth-facebook.svg)](https://travis-ci.org/dwyl/hapi-auth-facebook)
[![codecov](https://codecov.io/gh/dwyl/hapi-auth-facebook/branch/master/graph/badge.svg)](https://codecov.io/gh/dwyl/hapi-auth-facebook)
[![Code Climate](https://codeclimate.com/github/dwyl/hapi-auth-facebook/badges/gpa.svg)](https://codeclimate.com/github/dwyl/hapi-auth-facebook)
[![dependencies Status](https://david-dm.org/dwyl/hapi-auth-facebook/status.svg)](https://david-dm.org/dwyl/hapi-auth-facebook)
[![devDependencies Status](https://david-dm.org/dwyl/hapi-auth-facebook/dev-status.svg)](https://david-dm.org/dwyl/hapi-auth-facebook?type=dev)
:+1: Easy Facebook Authentication for Hapi Apps

@@ -26,3 +31,3 @@

#### Step 1: Upgrade your personal Facebook account to a developer account
### Step 1: Upgrade your personal Facebook account to a developer account

@@ -35,7 +40,7 @@ Go to developers.facebook.com/apps

#### Step 2: Select what platform your app is on
### Step 2: Select what platform your app is on
![makeapp](https://files.gitter.im/jackcarlisle/hapi-auth-facebook/YOYX/facebook3.png)
#### Step 3: Skip to create an App
### Step 3: Skip to create an App

@@ -46,3 +51,3 @@ On this page, you can click the button in the top right to quickly access your app's id.

#### Step 4: Create App
### Step 4: Create App

@@ -55,3 +60,3 @@ Here you can specify your app's name (doesn't ***have*** to be unique!)

#### Step 5: Specify Redirect URI
### Step 5: Specify Redirect URI

@@ -64,3 +69,3 @@ Inside the facebook app's **advanced** settings, specify the redirect URI near the *bottom* of the page:

#### Step 6: Make a request in your Hapi server
### Step 6: Make a request in your Hapi server

@@ -67,0 +72,0 @@ In your hapi server, make a request to the following url specifying your individual ```app-id``` and ```redirect-uri```

var test = require('tape');
var nock = require('nock');
var dir = __dirname.split('/')[__dirname.split('/').length - 1];
var file = dir + __filename.replace(__dirname, '') + ' > ';
var server = require('../example/facebook_server.js');
test('our first test!', function(t) {
test(file + 'our first test!', function(t) {
var options = {

@@ -11,7 +14,45 @@ method: 'GET',

server.inject(options, function(response) {
t.equal(response.statusCode, 200, 'woop')
t.equal(response.statusCode, 200, 'woop');
setTimeout(function() {
server.stop(t.end);
}, 100);
}, 10);
});
})
});
var mockToken = {
"access_token": "abcdefghijklmnopqrstuvwxyzDUMMY_TOKEN1234567890",
"token_type": "bearer",
"expires_in": 5183971
};
test(file + 'first nock test', function(t) {
var options = {
method: 'GET',
url: '/facebooklogin?code=mockcode'
};
var nock = require('nock');
var scope = nock('https://graph.facebook.com')
.get('/v2.3/oauth/access_token')
.query(true)
.reply(200, mockToken);
server.inject(options, function(response) {
console.log(response.payload);
t.equal(response.statusCode, 200, "Mock Test Working!");
var expected = "Your token: abcdefghijklmnopqrstuvwxyzDUMMY_TOKEN1234567890";
t.equal(response.payload, expected, "Correct Token Received (mock)");
server.stop(t.end);
});
});
test(file + 'Checking redirect path', function(t){
var options = {
method: 'GET',
url: '/authfacebook'
};
server.inject(options, function(response){
t.equal(response.statusCode, 302, "Success!");
server.stop(t.end);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc