Routes file issues with Passport - node.js

I'm using NodeJS, Express, and PassportJS to build a web application. I'm having a problem with one of my routes which I can't make any sense out of. When I have:
...
app.get('/auth/facebook', passport.authenticate('facebook'));
...
Everything seems to work fine. But when I change that to:
...
app.get('/auth/facebook',
function(req, res) {
passport.authenticate('facebook');
});
...
It hangs? Am I missing something on the app.get function? I want to be able to do this because I want to make the path a little more dynamic where I determine what passport authenticates. For example:
...
app.get('/auth/:provider',
function(req, res) {
passport.authenticate(req.params.provider);
});
...
Where provider could be facebook, twitter, or google...

passport.authenticate is middleware, take a gander at the source: https://github.com/jaredhanson/passport/blob/master/lib/passport/middleware/authenticate.js
passport.authenticate('facebook') returns a function that takes the req, res, next parameters from express and handles them.
so:
app.get('/auth/:provider',
function(req, res, next) {
passport.authenticate(req.params.provider)(req, res, next);
});
is what you need.

Related

How to pass one of multiple middleware nodejs

I have a router like this
router.post("/roomplayers",[authjwt.verifyTokenAdmin,authjwt.finishedRoomManagement,authjwt.activeRoomManagement],
RoomController.findPlayers)
and I would to get the controller RoomController.findPlayers if the admin have this cofinishedRoomManagementdepermission
OR this activeRoomManagement
How can I do that
If your question is how to pass through many middleware's in your route the solution is below.
const tokenMiddleWare = (req, res, next) =>{
//Your code here
next();
}
const isAdminMiddleWare = (req, res, next)=>{
//Your code here
next();
}
So now that we have two middlewares and one controller(I omitted it though) now you can work on route and pass those middlewares and controller but before I start I want point something important
So with the next you want to push the user down out of the middleware driving them closer to the route that they want to hit only when they meet all you validation that's when you want to push them down
router.post('/api/login', tokenMiddleWare, isAdminMiddleWare, (req, res)=>{
authController.login(req, res);
})
Now this would be how you pass down multiple middlewares and using your controller

How to process passport authenticate function in as request handler function

I am creating a web application based on Node.js and Express 4. I am also using Passportjs and Google OAuth 2 Startegy for authentication.
I am trying to configure my routes in order to process requests.
I have learned that this line work well:
router.get('/signin/google/callback', passport.authenticate('google', {failureRedirect: '/signin'}));
but when I decided to handle the route in the function, application stopped responding:
router.get('/signin/google/callback', function (req, res) {
passport.authenticate('google', {failureRedirect: '/signin'});
});
Do I miss something? thanks in advance
The callback of the Google OAuth function should be something like this:
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
The passport.authenticate() is middleware with arguments request,response, next. You could also define your own middlewares or the last request handler.

Routing in Express.js

I am currently express router in Node.js and having a problem like below. Let's say I have two url; one is to get a user info and one is to register users to an application.
http://example.com/users/:idUser (this will give a information of a user)
http://example.com/users/registration (this will allow a user registration)
The problem I have facing is when I call registration, the router is working with idUser; so I had to edit like user/registration instead of users. If I want to use as users/registration, which kinds of work do I have to do. I am still a newbie in Node.js.
Thanks.
You need to order the routes appropriately so that the registration route comes before.
app.get('/users/registration', function(req, res, next) {
....
});
app.get('/users/:userId', function(req, res, next) {
....
});
Just invert the order, like this:
app.get('/users/registration', function(req, res, next) {
...
});
app.get('/users/:userId', function(req, res, next) {
...
});

in express how multiple callback works in app.get

I am newbie in node so please forgive me if i am not getting obvious.
In node.js express application for app.get function we typically pass route and view as parameters
e.g.
app.get('/users', user.list);
but in passport-google example I found that they are calling it as
app.get('/users', ensureAuthenticated, user.list);
where ensureAuthenticated is a function
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
In short this means there are multiple callbacks which while running are called in series. i tried adding couple of more functions to make it look like
app.get('/users', ensureAuthenticated, dummy1, dummy2, user.list);
and i found ensureAuthenticated, dummy1, dummy2, user.list is getting called in series.
for my specific requirement i find calling functions sequentially in above form is quite elegant solution rather that using async series. can somebody explain me how it really works and how i can implement similar functionality in general.
In Express, each argument after the path is called in sequence. Usually, this is a way of implementing middleware (as you can see in the example you provided).
app.get('/users', middleware1, middleware2, middleware3, processRequest);
function middleware1(req, res, next){
// perform middleware function e.g. check if user is authenticated
next(); // move on to the next middleware
// or
next(err); // trigger error handler, usually to serve error page e.g. 403, 501 etc
}

Can I pass data through the next function in Express?

In using Express, I have a route like:
app.get('*', function (req, res, next) {
// no route is matched
// so call next() to pass to the static middleware
next();
});
There's another route that is something like app.get('/myroute', function(req, res, next)...
Can I pass information through to that route from the first one via next?
Thanks #amakhrov. I can use res.locals and store information.

Resources