Hi, I’m getting an error while manually running my code here. Any help would be appreciated. I just updated the packages file. I am passing the test for ‘/auth/github’, but failing ‘/auth/github/callback’. Here is my code in server.js:
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const mongo = require('mongodb').MongoClient;
const passport = require('passport');
const app = express();
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug')
mongo.connect(process.env.DATABASE, {useNewUrlParser: true},(err, client) => {
if(err) {
console.log('Database error: ' + err);
} else {
console.log('Successful database connection');
var db = client.db('mytestingdb');
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
*/
app.route('/auth/github')
.get(passport.authenticate('github'));
app.route('/auth/github/callback')
.get(passport.authenticate('github', {failureRedirect: '/'}),
(req, res) => {
res.redirect('/profile');
});
/*
* ADD YOUR CODE ABOVE
*/
app.route('/')
.get((req, res) => {
res.render(process.cwd() + '/views/pug/index.pug');
});
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);
});
}});