summaryrefslogtreecommitdiff
path: root/lib/auth.js
blob: dc8b94cad3dec3bd5a58f8c253d8ce583092e881 (plain)
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
//auth
//external modules
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GithubStrategy = require('passport-github').Strategy;
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy;

//core
var User = require('./user.js');
var config = require('../config.js');
var logger = require("./logger.js");

function callback(accessToken, refreshToken, profile, done) {
    //logger.info(profile.displayName || profile.username);
    User.findOrNewUser(profile.id, profile, function (err, user) {
        if (err || user == null) {
            logger.error('auth callback failed: ' + err);
        } else {
            if (config.debug && user)
                logger.info('user login: ' + user._id);
            done(null, user);
        }
    });
}

//facebook
module.exports = passport.use(new FacebookStrategy({
    clientID: config.facebook.clientID,
    clientSecret: config.facebook.clientSecret,
    callbackURL: config.getserverurl() + config.facebook.callbackPath
}, callback));
//twitter
passport.use(new TwitterStrategy({
    consumerKey: config.twitter.consumerKey,
    consumerSecret: config.twitter.consumerSecret,
    callbackURL: config.getserverurl() + config.twitter.callbackPath
}, callback));
//github
passport.use(new GithubStrategy({
    clientID: config.github.clientID,
    clientSecret: config.github.clientSecret,
    callbackURL: config.getserverurl() + config.github.callbackPath
}, callback));
//dropbox
passport.use(new DropboxStrategy({
    clientID: config.dropbox.clientID,
    clientSecret: config.dropbox.clientSecret,
    callbackURL: config.getserverurl() + config.dropbox.callbackPath
}, callback));