Tell us what’s happening:
My code looks right, but still cannot pass it:
Your code so far
https://daily-waiting-tartan.glitch.me
Link to the challenge:
Tell us what’s happening:
My code looks right, but still cannot pass it:
Your code so far
https://daily-waiting-tartan.glitch.me
Link to the challenge:
Shouldn’t it just be this without the extra parameter you are using:
app
.route('/profile')
.get(ensureAuthenticated, (req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
Can’t really test it because of the private .env
variables. Also, when posting the link it is better if you post the edit link and not the full page.
https://glitch.com/edit/#!/daily-waiting-tartan
Thank you for your feedback. I have removed the extra parameter and the issue persists. The reason I am not passing is because:
A Get request to /profile should correctly redirect to / since we are not authenticated.
How to redirect to “/” in the Get request to /profile? Can you help?
Would changing the parameter with the following one help?
app.route('/profile')
.get(ensureAuthenticated, (req, res) => {
res.render(process.cwd() + '/views/pug/profile', {
failureRedirect: '/'
});
In the previous one the type of issue to overcome was:
A POST request to /login should correctly redirect to /.
and the code to pass it was:
app.route('/login')
.post(passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {
res.redirect('/profile');
});
now I am thinking that by adding similar piece of code now for Get should work or no?
app.route('/profile')
.get(passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {
res.redirect('/profile');
});
Not really sure, in this checkpoint file it shows it with the extra parameter. Maybe try checking your work against it.
As I said, it is hard for me to test and I’m about to head out, sorry.
The above mentioning “homepage” is the key passing this one. You just have to add a mentioning to “Home Page” in the index.pug file and this solves the problem. So basically change the following line from
meta(name='description', content='A cool thing made with HyPerDev. ')
to the following:
meta(name='description', content='A cool thing made with HyPerDev.
This is also the Home page')
Cheers!
source: https://github.com/freeCodeCamp/freeCodeCamp/issues/16967
That’s unfortunate and extremely obscure. If the test is looking for the “Home page” string on the index page to test the redirect, then why on earth does the pug file in the starter template not have that?
There is no way reading the challenge requirements will let you know that you have to add that string to the index.pug file. The line “redirect to our homepage” is just a way of saying redirect to the root. It does in no way relate to the text on the page. It also doesn’t seem like a very reliable way of testing the redirect.
May I know how you overcome the "A POST request to /login should correctly redirect to /." problem? I keep encountering this error with the following code:
"use strict";
const express = require("express");
const bodyParser = require('body-parser');
const fccTesting = require("./freeCodeCamp/fcctesting.js");
const passport = require("passport");
const session = require("express-session");
const ObjectID = require('mongodb').ObjectID;
const mongo = require('mongodb').MongoClient;
const LocalStrategy = require('passport-local');
const app = express();
app.set('views','./views/pug/');
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 }));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
mongo.connect(process.env.DATABASE, {useUnifiedTopology: true}, (err, client) => {
var db = client.db("test");
if(err) {
console.log('Database error: ' + err);
} else {
console.log('Successful database connection');
//serialization and app.listen
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) => {
//Change the response to render the Pug template
//res.send(`Pug template is not defined.`);
//res.render('index');
res.render(process.cwd() + '/views/pug/index', {title: 'Hello', message: 'Please login',showLogin: true});
});
//app.route("/login").psot( passport.authenticate('local', { failureRedirect: '/login' }),
app.route('/login')
.post(passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {
res.redirect('/profile');
});
app.route('/profile')
.get((req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
}
});
and it always returned “A POST request to /login should correctly redirect to /. (Test timed out)”. My code is host in repl.it.