Skip to content

Instantly share code, notes, and snippets.

@saadtazi
Created July 19, 2016 11:52
Show Gist options
  • Save saadtazi/f30edf299413a50697cc7cfac235563a to your computer and use it in GitHub Desktop.
Save saadtazi/f30edf299413a50697cc7cfac235563a to your computer and use it in GitHub Desktop.
passport multiple strategies
const express = require('express');
const cons = require('consolidate');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
const FacebookStrategy = require('passport-facebook').Strategy;
const LocalStrategy = require('passport-local').Strategy;
const ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;
const app = express();
app.engine('html', cons.handlebars);
// set .html as the default extension
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static('public'));
app.use(cookieParser());
app.use(bodyParser());
app.use(session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
// name your strategy
passport.use('local', new LocalStrategy(
function(username, password, done) {
console.log('username, password', username, password);
if (username !== 'admin') {
return done(null, false, { message: 'Incorrect username.' });
}
if (password !== 'passwd') {
return done(null, false, { message: 'Incorrect password.' });
}
console.log('LocalStrategy OK');
return done(null, {
username: 'admin'
});
}
));
passport.use('facebook', new FacebookStrategy({
clientID: 'some-fb-client-id',
clientSecret: 'some-secret',
callbackURL: "http://localhost:3001/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
console.log('>>>fbProfile::', profile);
// in real life: create or update user...
return done(null, {username: profile.id});
}
));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successReturnToOrRedirect: '/',
failureRedirect: '/login'
}));
// naming your strategy allows to have only one /login uri
app.post('/login', passport.authenticate(['local', 'facebook'], {
successReturnToOrRedirect: '/',
failureRedirect: '/login'
}));
app.get('/',
(req, res) => {
console.log('>>USER', req.user);
res.status(200).send(req.user);
});
app.get('/login', (req, res) => {
res.status(200).render('login', { user: req.user });
});
console.log(ensureLoggedIn);
app.get('/secure-1',
ensureLoggedIn('/login'),
(req, res) => {
res.send('secure 1!')
});
app.get('/secure-2',
ensureLoggedIn('/login'),
(req, res) => {
res.send('secure 2!')
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.listen(3001, () => console.log('yep.. listening on http://localhost:3001'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment