caradoc-user
Version:
default user for caradoc framework
178 lines (157 loc) • 4.57 kB
JavaScript
/*
* DEPENDENCIES
*/
var passport = require('passport')
, FacebookStrategy = require('passport-facebook').Strategy
, TwitterStrategy = require('passport-twitter').Strategy
, GoogleStrategy = require('passport-google').Strategy
, path = require('path')
, entity = require('caradoc-entity')
;
/*
* VARIABLES
*/
var login_path = ""
, redirect_path = ""
, site_url = ""
;
/*
* Export function
*/
exports.passport = function(app){
/*
* INITIALISATION
*/
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
/*
* FACEBOOK
*/
passport.use(new FacebookStrategy({
clientID: 'FACEBOOK_APP_ID',
clientSecret: 'FACEBOOK_APP_SECRET',
callbackURL: path.join(site_url , "/auth/facebook/callback")
},
function(accessToken, refreshToken, profile, done) {
findOrCreate('facebook', profile.id,function(err, user) {
if (err) { done(err); }
done(null, user);
});
}
));
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: redirect_path,
failureRedirect: login_path }));
/*
* TWITTER
*/
passport.use(new TwitterStrategy({
consumerKey: 'TWITTER_CONSUMER_KEY',
consumerSecret: 'TWITTER_CONSUMER_SECRET',
callbackURL: path.join(site_url , "/auth/twitter/callback")
},
function(token, tokenSecret, profile, done) {
findOrCreate('twitter', profile.id, function(err, user) {
if (err) { done(err); }
done(null, user);
});
}
));
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { successRedirect: redirect_path,
failureRedirect: login_path }));
/*
* GOOGLE
*/
passport.use(new GoogleStrategy({
returnURL: path.join(site_url , '/auth/google/return'),
realm: site_url
},
function(identifier, profile, done) {
findOrCreate('google', identifier, function(err, user) {
done(err, user);
});
}
));
app.get('/auth/google', passport.authenticate('google'));
app.get('/auth/google/return',
passport.authenticate('google', { successRedirect: redirect_path,
failureRedirect: login_path }));
/*
* LOCAL
*/
// TODO : create local strategy
};
/**
*
* @param strategy {String}
* @param id{int}
* @param callback {function}
*/
function findOrCreate(strategy, id, callback){
var findUser = entity.find('user');
if(strategy == 'facebook'){
findUser.getByFacebookId(id, function(err, rows){
if(err) callback(err);
if(rows.length == 0){
User = entity.generate.user;
User = new User({
id : null,
facebook_id : id
});
entity.insert(User);
entity.flush();
}
else{
done(null, rows[0]);
}
});
}
else if(strategy == 'twitter'){
findUser.getByTwitter_Id(id, function(err, rows){
if(err) callback(err);
if(rows.length == 0){
User = entity.generate.user;
User = new User({
id : null,
twitter_id : id
});
entity.insert(User);
entity.flush();
}
else{
done(null, rows[0]);
}
});
}
else if(strategy == 'google'){
findUser.getByGoogle_id(id, function(err, rows){
if(err) callback(err);
if(rows.length == 0){
User = entity.generate.user;
User = new User({
id : null,
google_id : id
});
entity.insert(User);
entity.flush();
}
else{
done(null, rows[0]);
}
});
}
else if(strategy == 'local'){
}
else{
process.nextTick(function(){
callback('no such strategy');
});
}
}