Recently I’ve had a lot of troubles with this exercise since much of the information is outdated. Also, I had to figure out that mongodb v3 uses different things compared to v2 which this exercise was made for in the first place. So to help other people with this exercise, I’ve rewritten some of the code to work with mongodb v3 while eliminating the errors that might happen. Also, the code is documented so that you know what needs to be changed.
Firstly, package.json
{
"//1": "describes your app and its dependencies",
"//2": "https://docs.npmjs.com/files/package.json",
"//3": "updating this file will download and update your packages",
"name": "FCC-Advanced-Node-and-Express",
"author": "http://github.com/JosephLivengood",
"version": "0.0.1",
"description": "What am I about?",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.4",
"cors": "^2.8.5",
"body-parser": "^1.19.0",
"mongodb": "^3.3.2",
"helmet": "^3.21.0",
"pug": "^2.0.3",
"passport": "^0.4.0",
"express-session": "^1.16.1",
"passport-local": "^1.0.0",
"passport-github": "^1.1.0",
"socket.io": "^2.2.0",
"passport.socketio": "^3.7.0",
"cookie-parser": "^1.4.4"
},
"engines": {
"node": "4.4.3"
},
"repository": {
"type": "git",
"url": "https://hyperdev.com/#!/project/welcome-project"
},
"keywords": [
"node",
"hyperdev",
"express"
],
"license": "MIT"
}
Next, app/auth.js
const session = require('express-session');
const mongo = require('mongodb').MongoClient;
const passport = require('passport');
const GitHubStrategy = require('passport-github').Strategy;
module.exports = function (app, db) {
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
db.collection('chatusers').findOne(
{id: id},
(err, doc) => {
done(null, doc);
}
);
});
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
// Make sure this URL points to your project for the github callback
callbackURL: "https://troubled-litter.glitch.me/auth/github/callback"
},
function(accessToken, refreshToken, profile, cb) {
// Since mongodb v3 you use findOneAndUpdate since
// findAndModify is deprecated
db.collection('chatusers').findOneAndUpdate(
{id: profile.id},
{$setOnInsert:{
id: profile.id,
name: profile.displayName || 'Anonymous',
photo: profile.photos[0].value || '',
email: profile.email || 'No public email',
created_on: new Date(),
provider: profile.provider || '',
chat_messages: 0
},$set:{
last_login: new Date()
},$inc:{
login_count: 1
}},
{upsert:true, returnOriginal: false}, //Insert object if not found, Return new object after modify
(err, result) => {
return cb(null, result.value);
}
);
}
));
}
And finally, your server.js
'use strict';
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const auth = require('./app/auth.js');
const routes = require('./app/routes.js');
const mongo = require('mongodb').MongoClient;
const passport = require('passport');
const cookieParser= require('cookie-parser')
const app = express();
const http = require('http').Server(app);
const sessionStore= new session.MemoryStore();
const cors = require('cors');
/* Make sure to add the following variables to your .env file!
SESSION_SECRET=(random number)
DATABASE=(your database url)
GITHUB_CLIENT_ID=(authorize this app with github and get these two variables)
GITHUB_CLIENT_SECRET=(authorize this app with github and get these two variables)
*/
// Documentation for mongodb here
// http://mongodb.github.io/node-mongodb-native/3.2/api/
// Do not put this under fccTesting(app)
// otherwise your tests won't pass
app.use(cors());
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug')
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
key: 'express.sid',
store: sessionStore,
}));
mongo.connect(process.env.DATABASE, (err, client) => {
if(err) console.log('Database error: ' + err);
// Since mongodb v3, the callback function was changed from (err, db)
// to (err, client) and your database is in client.db
const db = client.db('chat');
// Input your database name above
auth(app, db);
routes(app, db);
http.listen(process.env.PORT || 3000);
//start socket.io code
//end socket.io code
});
After pasting these files and replacing the ones that are in your glitch program, you can freely continue following further exercises as the program should pass the tests. Hope this helps someone.
Cheers!