Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diky Arga done Google + FB + Twitter + Local = Awesome PassportJS #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FACEBOOK_CLIENTID =
FACEBOOK_CLIENTSECRET =
FACEBOOK_CALLBACK = http://localhost:3000/auth/facebook/callback

TWITTER_CONSUMERKEY =
TWITTER_CONSUMERSECRET =
TWITTER_CALLBACKURL = http://localhost:3000/auth/twitter/callback

GOOGLE_CLIENTID =
GOOGLE.CLIENTSECRET =
GOOGLE.CALLBACKURL = http://localhost:3000/auth/google/callback
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jspm_packages

# Optional npm cache directory
.npm
# ENV

.env

# Optional REPL history
.node_repl_history
283 changes: 283 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var http = require('http');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session')
var bodyParser = require('body-parser');
var flash = require('connect-flash');
// Mongoose
var mongoose = require('mongoose')
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/library')
require('dotenv').config()
var app = express();

// passport

let passport = require('passport')
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

var oauthConfig = require('./config/oauth.js');

// Import Mongoose Models


// passport config
var User = require('./models/user');

passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done) {
process.nextTick(function() {
User.findOne({
'local.email': email
}, function(err, user) {
if (err) {
return done(err)
}
if (user) {
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
} else {
var newUser = new User();

// set the user's local credentials
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);

// save the user
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
})
})
}));

passport.use('local-login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
}, function(req, email, password, done){
User.findOne({
'local.email': email
}, function(err, user){
if (err) {
return done(err)
}

if (!user) {
return done(null, false, req.flash('loginMessage', 'Gak ada user'))
}

if(!user.validPassword(password)){
return done(null, false, req.flash('loginMessage', 'Password salah'))
}

return done(null, user)
})
}))

passport.use(new FacebookStrategy({
clientID: oauthConfig.facebook.clientID,
clientSecret: oauthConfig.facebook.clientSecret,
callbackURL: oauthConfig.facebook.callbackURL
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
User.findOne({
'facebook.id' : profile.id
}, function(err, user){
if (err) {
return done(err)
}
if(user){
return done(null, user)
} else {

var newUser = new User()

newUser.facebook.id = profile.id
newUser.facebook.token = accessToken
newUser.facebook.email = profile.emails
newUser.facebook.name = profile.displayName

newUser.save(function(err){
if (err) {
throw err
}

return done(null, newUser)
})

}
})
// return done(null, profile);
});
}
));

passport.use(new TwitterStrategy({
consumerKey: oauthConfig.twitter.consumerKey,
consumerSecret: oauthConfig.twitter.consumerSecret,
callbackURL: oauthConfig.twitter.callbackURL
}, function(token, tokenSecret, profile, done){
process.nextTick(function(){
User.findOne({
'twitter.id': profile.id
}, function(err, user){
if (err) {
return done(err)
}

if(user){
return done(err, user)
} else {
let newUser = new User()
newUser.twitter.id = profile.id
newUser.twitter.token = token
newUser.twitter.username = profile.username
newUser.twitter.displayName = profile.displayName

newUser.save(function(err){
if (err) {
throw err
}

return done(null, newUser)
})
}
})
})
}))

passport.use(new GoogleStrategy({

clientID : oauthConfig.google.clientID,
clientSecret : oauthConfig.google.clientSecret,
callbackURL : oauthConfig.google.callbackURL,

}, function(token, refreshToken, profile, done){
process.nextTick(function(){
console.log('profile', profile);
User.findOne({
'google.id': profile.id
}, function(err, user){
if (err) {
return done(err)
}

if (user) {
return done(user)
} else {
let newUser = new User()

newUser.google.id = profile.id
newUser.google.name = profile.displayName
newUser.google.email = profile.emails[0].value
newUser.google.token = token

newUser.save(function(err){
if (err) {
throw err
}

return done(null, newUser)
})
}
})
})
}))

app.use(bodyParser());

app.use(cookieParser('sssshhhh'));
app.use(session({
cookieName: 'session',
secret: 'sssshhhh',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
httpOnly: true,
secure: true,
ephemeral: true,
resave: true,
saveUninitialized: true
}));


app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash());

app.use(function(req, res, next) {
res.locals.session = req.session;
next();
});

// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user.id);
});

// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});

var index = require('./routes/index');
var users = require('./routes/users');
var books = require('./routes/books');
var customers = require('./routes/customers');
var transactions = require('./routes/transactions');
var dashboard = require('./routes/dashboard');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));

app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);
app.use('/books', books);
app.use('/customers', customers);
app.use('/transactions', transactions);
app.use('/dashboard', dashboard);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('mongodb-crud:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
Loading