-
Notifications
You must be signed in to change notification settings - Fork 11
/
facebook.js
60 lines (50 loc) · 1.45 KB
/
facebook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* ultimate.server.middleware.passport.facebook
*/
'use strict';
var _ = require('lodash'),
passport = require('passport'),
passportFacebook = require('passport-facebook');
var _app = null;
/**
* Attach Facebook strategy.
*
* @private
* @param {object} facebookConfig Facebook config object.
* @param {string} baseUrl Base URL.
* @return {undefined}
*/
function _attachStrategy(facebookConfig, baseUrl) {
if (!_.isString(facebookConfig.appId)) {
throw new Error('Missing string in config: api.facebook.appId');
}
if (!_.isString(facebookConfig.appSecret)) {
throw new Error('Missing string in config: api.facebook.appSecret');
}
passport.use(new passportFacebook.Strategy({
clientID: facebookConfig.appId,
clientSecret: facebookConfig.appSecret,
callbackURL: baseUrl + '/auth/facebook/callback'
}, function (accessToken, refreshToken, profile, done) {
_app.models.User.findOrCreateFacebook(accessToken, refreshToken, profile, function (err, user) {
return done(null, user);
});
}));
}
/**
* Attach passport middleware.
*
* @param {App} app Application.
* @return {undefined}
*/
function attach(app) {
_app = app;
if (!_.isObject(app.config) ||
!_.isObject(app.config.api) ||
!_.isObject(app.config.api.facebook)) {
throw new Error('Missing string in config: api.facebook');
}
_attachStrategy(_app.config.api.facebook, _app.config.url);
}
// Public API
exports.attach = attach;