My glitch app is failing for this section and is inconsistent. I even tried copying and pasting the code from the GitHub Gist hint and it fails differently each time. To replicate, I am clearing my db so that it is fresh for each time, however it fails for different reasons. See screenshots below.
Advanced Node and Express - Registration of New Users
Additionally, The Final Project doesn't work either 馃槙
+1
I have this issue as well and 'Create New Middleware' also fails to see the redirect that I even confirmed does happen by logging each visit in the console.
For reference, I am using the same version of Chrome but on Windows 10 Pro build 17763.1 with the project on Glitch.
I think there are additional issues besides this test, mentioned in #17068 and #17763.
I had the same problem, but after some searching I found a solution from stackoverflow https://stackoverflow.com/questions/19035373/how-do-i-redirect-in-expressjs-while-passing-some-context
Just add username to the session object, before the redirect line in the '/register' route, like this:
req.session.username = req.user.username;
And then in the '/profile' route save the username into new variable and delete it from the session object.
const username = req.session.username;
delete req.session.username;
res.render(process.cwd() + '/views/pug/profile', {username: username});
Here's my Glitch project.
Hope it helps, at least it worked for me.
+1
I have this issue as well and 'Create New Middleware' also fails to see the redirect that I even confirmed does happen by logging each visit in the console.
For reference, I am using the same version of Chrome but on Windows 10 Pro build 17763.1 with the project on Glitch.
@VulcanDev
I had this same problem but found a solution in the FCC forums (and a linked, closed, GitHub issue). The test for checking if the page has redirected apparently does so by searching for the string 'Home Page' in the '/' route, so if you change your title variable to that it should work. (At least, it worked for the me and probably the other 11 people that 'liked' the post.)
Facing the same issue, not passing all tests.
Followed the code linked in the exercise:
https://gist.github.com/JosephLivengood/6c47bee7df34df9f11820803608071ed
__Edit:__ Cleared database multiple times. Adding a user manually in the register form loads the user profile. Can login to existing user profile. However, tests still fail.
+1
I have this issue as well and 'Create New Middleware' also fails to see the redirect that I even confirmed does happen by logging each visit in the console.
For reference, I am using the same version of Chrome but on Windows 10 Pro build 17763.1 with the project on Glitch.@VulcanDev
I had this same problem but found a solution in the FCC forums (and a linked, closed, GitHub issue). The test for checking if the page has redirected apparently does so by searching for the string 'Home Page' in the '/' route, so if you change your title variable to that it should work. (At least, it worked for the me and probably the other 11 people that 'liked' the post.)
This solved the 'Create New Middleware' challange, thanks a lot!
The problem 'solved' according to Farlokko above is not the challenge 'Registration of new users'
I've spent the better part of the day trying to get my code to pass all the tests in the freeCodeCamp test suite for the Registration of New Users challenge, to no avail. 馃槩 Everything passes except for the "Registering should work" test. 馃槱
I'm moving on to the other challenges as the code I have behaves perfectly well when tested manually. Hopefully this won't cause me even more frustration with the next challenges.
Before I move on however, I did notice something that I don't think I've seen anyone point out yet in any of the issue reports or FCC forum posts.
With a bunch of console logs at most of the critical steps along the way, I have noticed that in my code the failure seems to happen:
/register
route handler, it is (as expected) redirected to the /profile
route handler.ensureAuthenticated
function./register
route handler to the /profile
route handler, the test suite doesn't pass any user data (i.e. req.user
is undefined
within ensureAuthenticated
). This does not happen when registering a new user manually.ensureAuthenticated
has nothing to test./
home page rather than to the desired /profile
page.( I have tried enforcing delays on the request methods, I've updated the index and profile page titles and H1s, and tried to get the req.session.username
"temporary" variable trick to work for me, etc.)
MongoDB successfully connected!
Listening on port 3000
requested
ensureAuthenticated called...
there's no req.user
... the user failed the authentication test. Redirecting to home page.
logging someone out.
User "freeCodeCampTester" tried to login.
We've created and added a new user to the DB: freeCodeCampTester
User "freeCodeCampTester" tried to login.
About to redirect new user "freeCodeCampTester" to /profile route handler.
ensureAuthenticated called...
there's no req.user
... the user failed the authentication test. Redirecting to home page.
(note that I have ternary operators on some console logs because of the aforementioned undefined
issues)
MongoDB successfully connected!
Listening on port 3000
We've created and added a new user to the DB: Diana
User "Diana" tried to login.
About to redirect new user "Diana" to /profile route handler.
ensureAuthenticated called...
req.user.username is: Diana
... the user passed the authentication test.
Authenticated. Allowing "Diana" to access /profile .
(There are also a bunch of other annoyances with the testing for this challenge, among which is the need to use res.render()
instead of res.redirect()
in order for the /login
route handler to pass one of the tests.)
PS: I'm quite frustrated by the later FCC certifications/modules and have found that I easily spend 50% of my time trying to understand the poorly written instructions or finding workarounds to pass faulty test suites. I've also noticed that unresolved issues about these topics have existed for at least 6 months now. Given that I don't like frustrating experiences, is there any way for myself and other users to actively contribute to improving the latter certifications/modules so that students after us don't also have to muddle their way through the (apologies, but frankly kinda terrible) last third of the curriculum? 馃槯
hi JulianNF the 'solution' to this problem is a workaround check #17820 for it .hope it helps.
Hey @ibnthani2016, thanks for sharing that workaround. Unfortunately I tried @lucasMontenegro 's timeout trick and I couldn't get it to work for me, even after playing around with the delays extensively.
TBH though, by that point I was quite fed up of trying to get my code to work in a test suite when it works in the real world, you know?
As an update, not getting registration to work in the test suite for this challenge didn't cause me any problems with the following challenge's test suites.
tons of missing requirements in the instructions provided on the challenge. Hope it helps!
Here's a hack I did a while back https://www.freecodecamp.org/forum/t/advanced-node-and-express-registration-of-new-users/208071/10 - This is a duplicate of https://github.com/freeCodeCamp/freeCodeCamp/issues/17820 - so this can be closed.
mine isn't a hack other than the delays by #17820. The instructions were missing information about calling the req.logIn() method which returns the req.user data after serialization to populate the object which is passed to the front end. Your hack of manually entering the username freeCodeCampTester works, but is incorrect because you are bypassing the middleware authentication and serialization of the user data. This is the entire point of using the middleware and authentication packages.
To anyone else reading this: If you are stuck on this challenge, I would encourage you to seek out the proper way to utilize the requirements presented in the challenge. You may learn a thing or two about how passport and serialization works rather than trying to blast through FCC challenges to get a certificate.
Here's how to pass those tests:
Add the word "Profile" in the body of profile.pug file. it will help you bypass this test:
- text: Registering should work
testString: 'getUserInput => $.ajax({url: getUserInput(''url'')+ ''/register'',data: {username: ''freeCodeCampTester'', password: ''freeCodeCampTester''},crossDomain: true, type: ''POST'', xhrFields: { withCredentials: true }}) .then(data => { assert.match(data, /Profile/gi, ''I should be able to register and it direct me to my profile. CLEAR YOUR DATABASE if this test fails (each time until its right!)''); }, xhr => { throw new Error(xhr.statusText); })'
If you are writing express route in a different format than this:
app.route('/profile').post(
The the following test will fail:
- text: Register route and display on home
testString: 'getUserInput => $.get(getUserInput(''url'')+ ''/_api/server.js'') .then(data => { assert.match(data, /showRegistration:( |)true/gi, ''You should be passing the variable "showRegistration" as true to your render function for the homepage''); assert.match(data, /register[^]*post[^]*findOne[^]*username:( |)req.body.username/gi, ''You should have a route accepted a post request on register that querys the db with findone and the query being "username: req.body.username"''); }, xhr => { throw new Error(xhr.statusText); })'
To bypass it you need to rewrite the post express handler in the form of app.route('/profile').post(
or just add a comment in the post function that contain this /profile post
Most helpful comment
@VulcanDev
I had this same problem but found a solution in the FCC forums (and a linked, closed, GitHub issue). The test for checking if the page has redirected apparently does so by searching for the string 'Home Page' in the '/' route, so if you change your title variable to that it should work. (At least, it worked for the me and probably the other 11 people that 'liked' the post.)