Tried to modify to the social auth challenge to use facebook instead of github using the following code:
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const mongo = require('mongodb').MongoClient;
const passport = require('passport');
const FacebookStrategy = require('passport-facebook').Strategy;
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
require('dotenv').config()
const pug = require('pug');
const cors = require('cors');
const app = express();
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug')
var UserSchema = mongoose.Schema({
username: {
type: String,
index:true
},
password: {
type: String
},
email: {
type: String
},
name: {
type: String
},
facebook: {
id: String,
token: String,
email: String,
name: String
},
});
var User = module.exports = mongoose.model('User', UserSchema);
mongo.connect(process.env.DATABASE, (err, db) => {
if(err) {
console.log('Database error: ' + err);
} else {
console.log('Successful database connection');
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
};
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
db.collection('socialusers').findOne(
{id: id},
(err, doc) => {
done(null, doc);
}
);
});
/*
* ADD YOUR CODE BELOW
*/
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET,
callbackURL: "/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
// TODO: do sth with returned values
}
));
app.get('/auth/facebook',
passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
console.log(req.user)
res.redirect('/profile');
}
);
/*
* ADD YOUR CODE ABOVE
*/
app.route('/')
.get((req, res) => {
res.render(process.cwd() + '/views/pug/index');
});
app.route('/profile')
.get(ensureAuthenticated, (req, res) => {
res.render(process.cwd() + '/views/pug/profile', {user: req.user});
});
app.route('/logout')
.get((req, res) => {
req.logout();
res.redirect('/');
});
app.use((req, res, next) => {
res.status(404)
.type('text')
.send('Not Found');
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
}});
But it returns
URL blocked: This redirect failed because the redirect URI is not white-listed in the app's client OAuth settings. Make sure that the client and web OAuth logins are on and add all your app domains as valid OAuth redirect URIs.