I am working on this challenge and I am losing my mind. I have attempted every hack and fix possible - and I still have not passed all tests. Failure is happening with the req.isAuthenticated()
function that is placed in the profile page route protection middleware. Cutting the function out gets all but the “Registering should work” test to pass.
My code:
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport')
const session = require('express-session')
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const LocalStrategy = require('passport-local').Strategy;
const ObjectId = require('mongodb').ObjectID;
const mongo = require('mongodb').MongoClient
const cors = require('cors');
const app = express();
app.use(cors());
app.set('view engine', 'pug')
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Setup Session
app.use(session({
secret:process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
}))
app.use(passport.initialize())
app.use(passport.session())
// if (process.env.ENABLE_DELAYS) app.use((req, res, next) => {
// switch (req.method) {
// case 'GET':
// switch (req.url) {
// case '/logout': return setTimeout(() => next(), 500);
// case '/profile': return setTimeout(() => next(), 700);
// default: next();
// }
// break;
// case 'POST':
// switch (req.url) {
// case '/login': return setTimeout(() => next(), 900);
// default: next();
// }
// break;
// default: next();
// }
// });
mongo.connect(process.env.DB, { useNewUrlParser: true }, (err, client) => {
if (err) {console.log('Danger, Will Robinson! ' + err)}
else {
console.log('DB Connected!')
const db = client.db()
passport.serializeUser((user, done) => {
console.log('Passport Serialize Method: User serialized')
done(null, user._id)
})
passport.deserializeUser((id, done) => {
db.collection('users').findOne(
{_id: new ObjectId(id)},
(err, doc) => {
console.log('Passport Deserialize Method: User deserialized')
done(null, doc)
})
})
passport.use(new LocalStrategy((username, password, done) => {
db.collection('users').findOne( {username: username },
(err, user) => {
console.log('Passport LocalStrategy: User ' + username + ' attempted to log in.');
if (err) { return done(err)
} else if (!user) {
console.log('Passport LocalStrategy: No user found')
return done(null, false)
} else if (password != user.password) {
console.log('Passport LocalStrategy: Incorrect Password')
return done(null, false)
} else {
console.log('Passport LocalStrategy: User logged in')
return done(null, user)
}
})
}
))
let ensureAuthenticated = (req, res, next) => {
if(req.isAuthenticated()) {
console.log('ensureAuthenicated Function: User is registered, continuing routing')
return next()
}
console.log('ensureAuthenicated Function: User is not registered, redirecting to homepage')
res.redirect('/')
}
app.route('/')
.get((req, res) => {
res.render(process.cwd() + '/views/pug/index.pug', {title: 'Home Page', message: 'Please login', showLogin: true, showRegistration: true});
});
app.route('/register')
.post((req, res, next) => {
db.collection('users').findOne({ username: req.body.username }, function (err, user) {
if(err) {
console.log('Register Route: ' + err)
next(err);
} else if (user) {
console.log('Register Route: User ' + user.ops[0].username + ' is already registered. Please log in')
res.redirect('/');
} else {
db.collection('users').insertOne(
{username: req.body.username,
password: req.body.password},
(err, doc) => {
if(err) {
console.log('Register Route: Error ' + err)
res.redirect('/');
} else {
console.log('Register Route: User ' + doc.ops[0].username + ' successfully saved to DB')
next(null, user);
}
}
)
}
})},
passport.authenticate('local', { failureRedirect: '/', successRedirect: '/profile' }),
(req, res) => {
console.log('Register Route > Passport Authenticate: User authenticated. Redirecting to profile page.')
res.redirect('/profile');
}
);
app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' }),
(req, res) => {
console.log('Login Route: User logged in, redirecting to profile.')
res.render(res.render(process.cwd() + '/views/pug/profile.pug', {username: req.user.username, title: 'Profile Page'}))
})
app.route('/logout')
.get((req, res) => {
console.log('Logout Route: User logged out')
req.logout()
res.redirect('/')
})
app.route('/profile')
.get(passport.authenticate('local', {failureRedirect: '/'}),
(req, res) => {
console.log('Profile Route: User was authenticated using ensureA function')
res.render(process.cwd() + '/views/pug/profile.pug', {username: req.user.username, title: 'Profile Page'})
}
)
app.use((req, res, next) => {
res.status(404)
.type('text')
.send('Not Found')
})
app.listen(process.env.PORT, () => {
console.log("Listening on port " + process.env.PORT);
});
}})
How am I going to complete this if not even the direct code from github works
My glitch: https://glitch.com/~silly-queen
Please help.