Modulation in nodejs / hapi application with passport

I use Hapi as the api framework on top of node.js. I also use a passport for session management and authentication.

I am struggling to find a way to separate the logic of these different components. So far I have a separate file for most request handlers - I would like to get all the processing logic from the main application file. What is holding me back is the passport dependencies some handlers have.

Problem initialization code:

//init server
var server = new Hapi.Server(config.hostname, config.port);
server.pack.allow({ ext: true }).require(plugins, function (err) { 

    if (err) {
        throw err;
    }
});

//setup auth
var Passport = server.plugins.travelogue.passport;
Passport.use(new LocalStrategy( handlers.authUser ) );

While most routes are defined in a separate file (something like { method: 'POST', path: '/logout', handler: handlers.logout}). The problem is that there are several routes that depend on Passport, and I'm not sure how to access the Passport variable in the handler file.

:

{ method: 'POST', path: '/login', config: {
            handler: function (request, reply) {
                Passport.authenticate('local')(request, function (err) {
                    console.log("successful authentication?");
                    if (err && err.isBoom) {}
                    reply({message: "logged in"});
                }); }}}

, - ?

.

: pre v8 api, .

+4
1

. , request, , . , Passport, :

// ...
handler: function(request, reply) {
  var passport = request.server.plugins.passport;
}

request, , . , - Travelogue Passport, request.user.

+2

Source: https://habr.com/ru/post/1524120/


All Articles