0

I have end-points like below:

app.post(api + '/channels/:channel_id/albums', user.ensureAuthenticated, album.create);
app.get(api + '/channels/:channel_id/albums/published', user.ensureAuthenticated,album.publishedAlbums);
app.get(api + '/channels/:channel_id/albums', user.ensureAuthenticated, album.findAll);

It all works fine, but it's annoying for me to put user.ensureAuthenticated in each of the end-points, are there any method that you can once put the user.ensureAuthentication at once for all?

For ex: Laravel has such options like beforeAuth you make an if and inside that u put all the end-points that you want to make protected.

like for ex:

if(user.ensureAuthenticated){
  // endpoints declariations
}else{
  // redirect to login
}

Thanx

Lulzim
  • 547
  • 4
  • 9
  • 22

1 Answers1

1

You will find an answer here : Only allow passportjs authenticated users to visit protected page

Especially this kind of code :

//checks to be sure users are authenticated
app.all("*", function(req, res, next){
if (!req.user) 
    res.send(403);
else
    next();
});

All endpoints will be secured.

Community
  • 1
  • 1
Spawnrider
  • 1,727
  • 1
  • 19
  • 32