https://learn.freecodecamp.org/information-security-and-quality-assurance/advanced-node-and-express/create-new-middleware
this app is doing what it says and redirecting me I cannot go to https://authentication-fcc-curriculum.glitch.me/passport
why is it not passing the challenge?
The old way:
app.route('/profile')
.get((req,res) => {
res.render(process.cwd() + '/views/pug/profile');
})
Does allow the user to go to the profile.
1 Like
The challenge says “Now add ensureAuthenticated as a middleware”. and it gives you these lines to just copy and paste.
app.route('/profile')
.get(ensureAuthenticated, (req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
yes i did, and its working. but not passing the challenge.
any advice on how to set it up or why its not passing?
here is the code:
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const mongo = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const LocalStrategy = require('passport-local');
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')
app.use(session({
secret: process.env.SECRET,
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
mongo.connect(process.env.DATABASE, (err, db) => {
if(err) {
console.log('Database error: ' + err);
} else {
console.log('Successful database connection');
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser( (id, done) => {
db.collection('users').findOne(
{_id: new ObjectID(id)},
(err, doc) => {
done(null, doc);
}
);
});
passport.use(new LocalStrategy(
function(username, password, done) {
db.collection('users').findOne({ username: username }, function (err, user) {
console.log('User '+ username +' attempted to log in.');
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (password !== user.password) { return done(null, false); }
return done(null, user);
});
}
));
app.route('/')
.get((req, res) => {
res.render(process.cwd() + '/views/pug/index', {title: 'Hello', message: 'login', showLogin: true});
});
app.route('/login')
.post(passport.authenticate('local', { failureRedirect: '/' }),(req,res) => {
res.redirect('/profile');
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
};
app.route('/profile')
.get(ensureAuthenticated, (req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
}});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
here is a link to my live app:
Combining automated deployment, instant hosting & collaborative editing, Glitch gets you straight to coding so you can build full-stack web apps, fast
guys this was fixed by changing the title to ‘Home page’ in the pug index template. I found this solution on the forums, why was this not mentioned in the challenge? time was spent trying to solve this when nothing was wrong with the code.
1 Like
This is still an issue as of 2019-09-24.
Here is a related PR (pull request) to try and fix the boilerplate template: