Advanced Node and Express - Registration of New Users (Updated with only ONE hack)

There’s been a lot of chatter about the tests failing because of incomplete instructions.

I’ve waded through most of the suggestions and have finally successfully passed the tests without manipulating other files which has been suggested, such as adding the text “home page” and “profile page” to force the test regex to validate.

Here’s what I did:
In profile.pug I added the variable for title which is passed on the object when the page is loaded.

    h1.border.center #{title} - FCC Advanced Node and Express
    h2.center#welcome Welcome, #{username}!

In Index.pug I added the variable for title which is passed on the object when the page is loaded.

h1.border.center #{title} - FCC Advanced Node and Express

In server.js you need to modify the objects which are passed in the render response. This is really most of the missing information in the instructions. This information was completely left out of the instructions.

I also modified the deserializeUser by changing the done(null, id) to done(null, user)

passport.deserializeUser((id, done) => {
  db.collection('users').findOne(
    {_id: new ObjectID(id)},
    (err, user) => done(null, user))})
app.route('/')
  .get((req, res) => {res.render(`${process.cwd()}/views/pug/index.pug`,
      {title: 'Home Page', // add "title: "Home Page" to the object to pass to index.pug
       message:'Please login',
       showLogin: true,
       showRegistration: true})
                     })

Update: I FINALLY passed all the tests with pure code. No hacking the username into the object. One key point missing in the instructions is why or how passport.serializeUser and deserializeUser are used. They basically create a unique Id of the username which is passed in the login/registration process to the database so your data is not transferred over http in plain text. Once it returns back in the object, it’s converted back to plain text where it is then passed in another object to the profile UI component. The instructions did not give any indication to use “req.logIn()” which executes the serialization methods.

I was unable to get the req.body object to return a valid property for username. I spent a few hours trying to make it work and I just couldn’t figure it out. So this is the only “hack” I had to make to get the tests to pass. adding { username: “freeCodeCampTester” } to the response object on the /profile route render


app.route('/profile')
  .get(ensureAuthenticated, (req, res, next) => {
    req.logIn(req.user, (err) => {
    (err) ? next(err) : res.render(`${process.cwd()}/views/pug/profile.pug`,
      { title: "Profile Page", // add "Profile Page" to the object to pass to profile.pug
        username: req.user.username })})})

<strike>app.route('/profile')
  .get(ensureAuthenticated, (req, res) => res.render(`${process.cwd()}/views/pug/profile.pug`, 
                                                     { title: "Profile Page",  // add "Profile Page" to the object to pass to profile.pug
                                                       username: "freeCodeCampTester" // add the user name manually.  
                                                      })) </strike>

The only remaining last “hack” was adding the switch statement for enabling delays in the .env. A switch is performed depending on the route being called, because the tests are not allowing enough time for a server response from the db. This is causing the login test to fail on redirect to profile when the /profile route calls the middleware (ensureAuthenticated) which executes the “next” route (profile) if ensureAuthenticated returns true. This is when req.logIn is executed to return the username and populate the object to pass to the profile.pug, thus displaying Welcome, YOUR_USERNAME !
The enable delays hack was provided by github user: lucasMontenegro #17820

You should be able to test the app directly in glitch by logging in with the username and password and it will redirect you to the profile page.
user: freeCodeCampTester
pw: freeCodeCampTester

You should also be able to register a new user and see the results in the db. You will also see the new user registered name displayed on the profile page. The only thing here is the new user name registered will not display on the profile page because we manually passed “freeCodeCampTester” in the /profile render object. You will see what I mean if you try it out.
If anyone can figure out how to get a valid response object from the profile route, I would be very interested in what you did. Please share in this thread.

Happy Coding!

1 Like

Been working at this for a few hours… Got my code working because of this post, thank you.

Still wasn’t passing the tests despite.

Ended up giving it a try in Firefox instead of Google Chrome and the tests passed right away. Might be worth giving that a shot if it’s still not working for anyone.

2 Likes

I tried several different things to get the tests passing.

In the end the only thing i needed was a change of browser.

Thank you!