totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

F.onAuthorize and session #650

Closed gibson closed 6 years ago

gibson commented 6 years ago

Why F.onAuthorize run before session module initialize? I have in session data from passport.js and i want use this data for set auth status, but cant - in F.onAuthorize req doesnt have session property

petersirka commented 6 years ago

Hi @gibson, because on the state from F.onAuthorize depends next processing of the request. I think that you use a global middleware, but update it in some definition file to:

F.use('session', '*', ['web']);

And the middleware with session will be executeda after F.onAuthorize. Global middleware is executed before is the request processed.

That's explation 💯

gibson commented 6 years ago

i need execute session module before F.onAuthorize

petersirka commented 6 years ago

Aha ... Then you need to use a global middleware and you can do it like this:

F.use('session');

BTW: this global middleware is for all requests, so update your middleware like that:

F.middleware('session', function(req, res, next) {
    if (req.isStaticFile)
        return next();
    // ... your code
    // ... your code
    // ... your code
    // ... your code
});

Or you can call session in F.onAuthorize directly:

F.onAuthorize = function(req, res, flags, next) {
    YOURSESSION(req, res, function(session) {
         // your logic for authorization
    });
};
gibson commented 6 years ago

thx, its working