A
A
Adel Khalitov2019-04-21 22:22:11
Angular
Adel Khalitov, 2019-04-21 22:22:11

Why is ACL(nodejs) blocking content rendering?

Hello everyone, I have my own project on angular - frontend, nodejs (express, passport, acl) - backend.
config.js

mongoose.connect(myURLtoDB, {useCreateIndex: true, useNewUrlParser: true}, function (err, db) {
    .....
};
var conn    = mongoose.connection;
exports.freshConnect = conn;

router.js
var db = require('../config/config.js');
var node_acl = require('acl');
var acl = new node_acl( new node_acl.mongodbBackend(db.freshConnect, 'acl_') );

router.use('/crm', checkForPermissions(), crmRouter);

function checkForPermissions() {
    console.log('Я работаю')
    return acl.middleware(2, getUserId);
}

function getUserId(req) {
    if (req.user) {
        return req.session.passport.user;
    }
}

Now, when the database is started, checkForPermissions() immediately runs in the console, although I do not make any requests.
When trying to display some content in angular, for example by URL: crm/newlead, the content is not displayed. With what it can be connected? What am I doing wrong?
On each /crm/* page, I have a passport.js isAuthenticated() request to the server, which returns true or false (If true means the user is authorized and has access to this page, if false it redirects to the login page), but going to The angular page sends a request to the server in order to receive a response from the isAuthenticated () function, but does not receive a response, because the request does not reach the server and the checkForPermissions () function is called at the time of server startup, on subsequent requests they do not display anything. What is it connected with?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-04-21
@SaveLolliPoP

try

var db = require('../config/config.js');
var node_acl = require('acl');
var acl = new node_acl( new node_acl.mongodbBackend(db.freshConnect, 'acl_') );

router.use('/crm', checkForPermissions, crmRouter);

function checkForPermissions(req, res, next) {
    console.log('Я работаю')
    const userId = getUserId(req);
    acl.middleware(2, userId)(req, res, next);
}

function getUserId(req) {
    if (req.user) {
        return req.session.passport.user;
    }
}

why is your getUserId(req) so strange? is that exactly how it should be?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question