Tell us what’s happening:
Failing tests. I found a way to get around the tests when your only issue is that you didn’t implement your solution using the syntax expected by the test.
Your code so far
I upgraded to MongoDb 3.5.2 because of various issues with the outdated version. (It didn’t seem like a good use of time to learn how to fix problems in the old version.). However, findAndModify
is deprecated, so I had to use findOneAndUpdate
. Not a problem. I used the Mongoose documentation and modified my code. It works great, but it would not pass the tests. After reading a few posts, I realized the test was using regex
to find key words. After spending a couple hours trying to get past similar issues with the previous lessons tests, I came upon an idea. I took the provided code, copy and pasted it into my working code, and then commented it out. Suddenly I passed the tests. It would be awesome to have some updated tests that actually tested the program and not implementation details. However, I can only imagine, that would be quite a task.
"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 GitHubStrategy = require("passport-github").Strategy;
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.MONGO_URI,
{ useUnifiedTopology: true },
(err, client) => {
if (err) {
console.log("Database error: " + err);
} else {
const db = client.db(process.env.DB_NAME);
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 GitHubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: "https://sore-pea.glitch.me/auth/github/callback"
},
function(accessToken, refreshToken, profile, cb) {
const { id, displayName, photos, emails, provider } = profile;
const conditions = { id };
const update = {
$setOnInsert: {
id,
name: displayName || "John Doe",
photo: photos[0].value || "",
email: (emails && emails[0].value) || "No public email",
created_on: new Date(),
provider: provider || ""
},
$set: {
last_login: new Date()
},
$inc: {
login_count: 1
}
};
const options = { upsert: true, new: true };
// db.collection('socialusers').findAndModify(
// {id: profile.id},
// {},
// {$setOnInsert:{
// id: profile.id,
// name: profile.displayName || 'John Doe',
// photo: profile.photos[0].value || '',
// email: profile.emails[0].value || 'No public email',
// created_on: new Date(),
// provider: profile.provider || ''
// },$set:{
// last_login: new Date()
// },$inc:{
// login_count: 1
// }},
// {upsert:true, new: true},
// (err, doc) => {
// return cb(null, doc.value);
// }
// );
db.collection("socialusers").findOneAndUpdate(
conditions,
update,
options,
(err, doc) => cb(null, doc && doc.value)
);
}
)
);
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");
});
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);
});
}
}
);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
.
Challenge: Get the test(s) to pass.
Link to the challenge:
https://www.freecodecamp.org/learn/information-security-and-quality-assurance/advanced-node-and-express/implementation-of-social-authentication-iii