Advanced Node and Express - How to Use Passport Strategies -OP

Dear all,

This is my first post.

I have been on the forum - found the working code from other’s project but I CAN NOT MAKE IT WORK!

I don’t get it.

The solution from previous post is to change the title to " Home page" which I did but I m not sure I did properly as I changed my title here:image

Your code so far
I m not sure how to share my code so here are two links to my glitch project:

https://cute-allspice.glitch.me

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36.

Challenge:

Link to the challenge:
https://www.freecodecamp.org/learn/information-security-and-quality-assurance/advanced-node-and-express/how-to-use-passport-strategies

Thanks in advance for your time folks

1 Like

Hi, welcome to the forum!

So that means to change the title variable, that you pass to the view index.pug, from “Hello” to “Home Page” here:

app.route("/").get((req, res) => {
      res.sendFile(process.cwd() + "/views/pug/index.pug");

      res.render(process.cwd() + "/views/pug/index", {
        title: "Hello",
        message: "Please login",
        showLogin: true
      });
    });

You should change the filename back to index.pug
See if that works for you.

Thanks for your quick answer but I am afraid to bother you more as this does not work…
I keep getting:
image

I cannot copy the code of the previous post without having “test timed out” answers.

And now, I have an error message in my code ( line 58) that I do not understand: It says that db is not defined although I used it before…
image

Here is the link to my code:

Best,

Things have gotten messed up somehow.

You need to put all the things for authorization and routes inside the mongo.connect() function.
Your routes should not be inside the passport.use(new LocalStrategy function.

First, move everything after line 51 up to after line 47, right after the deserializeUser.
Then, take the three routes that are currently in the LocalStrategy ("/", “/profile”, and “/login”) and move them after the LocalStrategy function.

Also, did you change db to mongo to avoid the error? The error “db is not defined” was because you were trying to access it out of the database connection. The variable db is local to that function so anything that needs access to that variable needs to be inside that function. So change mongo back to db once you have put things in their proper places and there shouldn’t be an error.

The challenge gives the code up to this point here: https://gist.github.com/JosephLivengood/8a335d1a68ed9170da02bb9d8f5b71d5
So look at that to see what it all should look like.

Keep in mind that you still need to change the title variable to “Home Page”, that is not correct in the example code at that link. Also, right now you have it as “Home page” with an lowercase “p”. That might make the tests fail, I don’t know.

1 Like

Hello, dear Stressstressstress savior,

I am so sorry but It has been now 8-9 hours that this challenge is corroding me.

I have re-structured all my code to fit it with your example ( I also have the updated dependencies and the cors package ).

Now all my tests are timed out.
It kills me.

It happened when I changed my routes inside the mongo.connect() function.

See my code here:

https://cute-allspice.glitch.me

Thanks for your time, sincerely.

Sorry to hear you’re still having trouble with this.

Right now your Glitch app is not loading for me at all. I don’t know if that’s a problem with your code, or Glitch. Probably Glitch because there are no errors or anything. So that might be what’s causing the tests to time out. Are you seeing any errors when loading the page or in the console?

When I was doing these challenges, it happened a few times that my code would be fine but the tests would time out and fail. After refreshing the challenge page and trying a few times they would eventually pass. Don’t know if that was a problem with FCC or Glitch. But maybe something like that is happening to you.

1 Like

Here’s another thing you can try. This is required by newer versions of mongodb, v3 I think.

mongo.connect(process.env.DATABASE, (err, client) => { //change db to client
  if (err) {
    console.log("Database error: " + err);
  } else {
    console.log("Successful database connection");
    var db = client.db('test'); //add this line - if 'test' is not the short URL for your database you will have to change it
    
    //put all authorization stuff and routes here

  }
});
1 Like

The homepage hack also doesn’t work for me…

@wongz If you could leave a link to your glitch.com project, it should make it easier for others to see what is going on.

It works when I test it manually, but not able to pass the test.

  1. I changed the head and body in index.pug to “Home Page”
  2. I also changed title: ‘Home Page’ in the app.route("/") object in server.js
1 Like

@caleb-mabry The “How to Use Passport Strategies” works now! Thanks. I’ll keep going and reply if any other issues come up lol

I’m not sure how I made it work but I changed the name of my database back and forth. I think overall it just wasn’t refreshing… or something… :s

const db = client.db(‘auth’);

^ Changed the above ‘auth’ to ‘profiles’ then back to ‘auth’…

1 Like

I remember some time back that I was also having issues with certain lessons. I’m pretty sure that I’ve had a similar issue with this one specifically. I’m glad that you’ve gotten it to work! Keep up the fantastic work.

1 Like