hedgedoc/lib/auth.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-05-04 03:53:29 -04:00
//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
2015-07-01 12:10:20 -04:00
var User = require('./user.js');
var config = require('../config.js');
var logger = require("./logger.js");
2015-05-04 03:53:29 -04:00
function callback(accessToken, refreshToken, profile, done) {
2015-07-01 12:10:20 -04:00
//logger.info(profile.displayName || profile.username);
2015-05-04 03:53:29 -04:00
User.findOrNewUser(profile.id, profile, function (err, user) {
if (err || user == null) {
2015-07-01 12:10:20 -04:00
logger.error('auth callback failed: ' + err);
2015-05-04 03:53:29 -04:00
} else {
2015-07-01 12:10:20 -04:00
if (config.debug && user)
logger.info('user login: ' + user._id);
2015-05-04 03:53:29 -04:00
done(null, user);
}
});
}
//facebook
module.exports = passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
2015-05-15 00:58:13 -04:00
callbackURL: config.getserverurl() + config.facebook.callbackPath
2015-05-04 03:53:29 -04:00
}, callback));
//twitter
passport.use(new TwitterStrategy({
consumerKey: config.twitter.consumerKey,
consumerSecret: config.twitter.consumerSecret,
2015-05-15 00:58:13 -04:00
callbackURL: config.getserverurl() + config.twitter.callbackPath
2015-05-04 03:53:29 -04:00
}, callback));
//github
passport.use(new GithubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
2015-05-15 00:58:13 -04:00
callbackURL: config.getserverurl() + config.github.callbackPath
2015-05-04 03:53:29 -04:00
}, callback));
//dropbox
passport.use(new DropboxStrategy({
clientID: config.dropbox.clientID,
clientSecret: config.dropbox.clientSecret,
2015-05-15 00:58:13 -04:00
callbackURL: config.getserverurl() + config.dropbox.callbackPath
2015-05-04 03:53:29 -04:00
}, callback));