In the process of learning the MEAN stack I came across an issue.
Whenever I try to use passport authenticate method it never returns any response. I always get "localhost didn’t send any data. ERR_EMPTY_RESPONSE".
Here is the exact isolated code snippet that isn't working:
app.post("/login", passport.authenticate("local",
{successRedirect: "/campgrounds", failureRedirect: "/login"}),
function(req, res){
});
If you want to see the whole code, you can find it: HERE
Does anyone have any solutions?
this line needs parens after authenticate
according to the docs here
Related
I'm trying to reinforce some concepts learned in Colt Steele's Web Development Bootcamp and I have built a small website. At this time, I'm just trying to get the authentication working properly.
When a user signs up, I can see that they are added to my Mongo DB so I'm assuming that is working correctly but when I try to log back in it always redirects to the error page.
I have tried debugging this a number of ways and have compared it to the work we did in the course a hundred times but I cannot spot the bug with this. If anyone could give me some guidance I would really appreciate it.
Here's my project repo:
https://github.com/mcarre93/Depa
Ciao, according ti passport docs, when you post /login you should do:
app.post('/login', passport.authenticate('local'), function(req, res) {
// If this function gets called, authentication was successful.
// `req.user` contains the authenticated user.
res.redirect('/account');
});
I read your code and you are doing something different. Try to modify your code as documentation suggest and let me know.
I am using a res.redirect('page.ejs'); and on my browser I get the message:
Cannot GET /page.ejs
I have not declared this in my routes file in the style of :
app.get('/page', function(req, res) {
res.render('page.ejs');
});
Should this be included in order for the res.redirect() to work?
When I do not use res.redirect() but res.render(), even if I have not the app.get() code, it still works.
so to understand this, let's look at what each of these methods do.
res.redirect('page.ejs');
// or, more correctly, you're redirecting to an *endpoint*
// (not a page. the endpoint will render a *page*) so it should be:
res.redirect('/page');
this will tell express to redirect your request to the GET /page.ejs endpoint. An endpoint is the express method you described above:
app.get('/page', function(req, res) {
res.render('page.ejs');
});
since you don't have that endpoint defined it will not work. If you do have it defined, it will execute the function, and the res.render('page.ejs') line will run, which will return the page.ejs file. You could return whatever you want though, it can be someOtherPage.ejs or you can even return json res.json({ message: 'hi' });
res.render('page.ejs');
this will just respond to the client (the front-end / js / whatever you want to call it) with the page.ejs template, it doesn't need to know whether the other endpoint is present or not, it's returning the page.ejs template itself.
so then, it's really up to you what you want to use depending on the scenario. Sometimes, one endpoint can't handle the request so it defers the request to another endpoint, which theoretically, knows how to handle the request. In that case redirect is used.
hope that makes sense and clarifies your confusion
(I'm not an expert on the inner-workings of express, but this is a high-level idea of what it's doing)
You should do res.redirect('/page')
If a form in my website is emitting a POST, and I was the data to be processed and then have them redirected back to "/", how would I handle this?
My current code is
app.post('/contact', function(req, res) {
console.log(req.body.Name)
console.log(req.body.Email)
console.log(req.body.TextArea)
res.end(???)
})
This accurately outputs the given variables but I cannot find any information online on how to return a webpage or redirect to another web page.
You can use res.redirect().
My bad, http.get/request belongs to node, not express. And the way to relocate to a page in node is not even .get(), it's .setHeader('/link') tho, res.end() is mandatory.
res.redirect('/');
Simple as that.
I'm working on a Node.js app. Everytime I think I understand JavaScript, a curve ball is thrown at me that I don't understand. Currently, I'm trying to use passport.js to authenticate a user. I have the following code (which works):
passport.authenticate('google')(req, res, function() {
// TODO: Handle a user not authenticating
// This is the happy path. It will always execute. However, it should only execute if a user authenticated with google.
logger.info('user authenticated');
res.redirect(307, '/auth-confirm');
});
My challenge is, I need to detect when a user fails to authenticate with Google. If they succeed, I need to get the access token. However, I do not know how to do either of these because I do not understand the syntax of this call. Specifically, I don't understand what (req, res, function() {...}) is.
Is that line saying that passport.authenticate returns a function. So, req, res, and function() { ... } are passed as parameters to the function that passport.authenticate returns? If that's the case, I still don't know how to get the access token returned by google.
I am using passport-local-mongoose and trying to setup a simple working example using the login example in the repository. But I'm running into problems and I get an error message saying "Error: failed to serialize user into session".
I have reproduced the problem in a gist. (To run the gist, you will need to replace the mongodb server IP and database name in the user.js file and do a POST to the /register endpoint with username and password values.)
In it you will see the endpoints '/setval' and '/getval' which set values in the session and retrieves it, showing that session support is working. The endpoint '/authtest' gives an Unauthorized response, even after doing a POST to '/login'. The endpoint '/authdebug' gives more information - the error mentioned above.
Anyone have any ideas on what is going on? I'm running out of things to try.
I think that passport.authenticate should only be used on routes which are actually used for authenticating the user; in your case, that would be the /login route.
For routes for which you want to make sure a user has previously authenticated, like /authtest, you need a middleware which checks if a user is authenticated:
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login'); // or your login page
}
// and
app.get('/authtest', ensureAuthenticated, function(req, res) {
...
});
An alternative for that middleware would be connect-ensure-login.