hapi-auth-facebook
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -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); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27473
0
163
70
6
- Removedhapi@^11.1.2
- Removedquerystring@^0.2.0
- Removed@hapi/address@1.0.1(transitive)
- Removedaccept@2.2.3(transitive)
- Removedammo@2.1.2(transitive)
- Removedb64@3.1.1(transitive)
- Removedboom@3.2.24.3.15.3.3(transitive)
- Removedcall@3.0.4(transitive)
- Removedcall-bind-apply-helpers@1.0.1(transitive)
- Removedcall-bound@1.0.3(transitive)
- Removedcatbox@7.2.1(transitive)
- Removedcatbox-memory@2.1.1(transitive)
- Removedcontent@3.1.2(transitive)
- Removedcryptiles@3.2.1(transitive)
- Removeddunder-proto@1.0.1(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.7(transitive)
- Removedget-proto@1.0.1(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhapi@11.1.4(transitive)
- Removedhas-symbols@1.1.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedheavy@4.1.1(transitive)
- Removedhoek@3.0.44.3.1(transitive)
- Removediron@3.0.1(transitive)
- Removedisemail@2.2.1(transitive)
- Removeditems@2.2.1(transitive)
- Removedjoi@12.1.17.3.0(transitive)
- Removedkilt@2.0.2(transitive)
- Removedmath-intrinsics@1.1.0(transitive)
- Removedmime-db@1.53.0(transitive)
- Removedmimos@3.1.1(transitive)
- Removedmoment@2.30.1(transitive)
- Removednigel@2.1.1(transitive)
- Removedobject-inspect@1.13.3(transitive)
- Removedpeekaboo@2.0.2(transitive)
- Removedpez@2.2.2(transitive)
- Removedqs@6.13.1(transitive)
- Removedquerystring@0.2.1(transitive)
- Removedshot@2.0.1(transitive)
- Removedside-channel@1.1.0(transitive)
- Removedside-channel-list@1.0.0(transitive)
- Removedside-channel-map@1.0.1(transitive)
- Removedside-channel-weakmap@1.0.2(transitive)
- Removedstatehood@3.1.0(transitive)
- Removedsubtext@3.0.2(transitive)
- Removedtopo@2.1.1(transitive)
- Removedvise@2.1.1(transitive)
- Removedwreck@7.2.1(transitive)