2017-05-02 36 views
0

我目前在項目上使用sean.js。一切看起來不錯,但我在註冊過程中遇到了問題。我最近實施的對我的路線後端的政策,給予權限,我的用戶根據自己的角色,這樣的事情:SEAN.js註冊後自動註冊和req.user.roles

exports.invokeRolesPolicies = function() { 
    acl.allow([{ 
    roles: ['myRole'], 
    allows: [{ 
     resources: '/some-route', 
     permissions: '*' 
    }] 
    }]); 
}; 

exports.isAllowed = function (req, res, next) { 
    var roles = (req.user) ? req.user.roles : ['guest']; 

    // If an person is being processed and the current user created it then allow any manipulation 
    if (req.people && req.user && req.people.user && req.people.user.id === req.user.id) { 
    return next(); 
    } 

    // Check for user roles 
    acl.areAnyRolesAllowed(roles, req.route.path, req.method.toLowerCase(), function (err, isAllowed) { 
    if (err) { 
     // An authorization error occurred 
     return res.status(500).send('Unexpected authorization error'); 
    } else { 
     if (isAllowed) { 
     // Access granted! Invoke next middleware 
     return next(); 
     } else { 
     return res.status(403).json({ 
      message: 'User is not authorized' 
     }); 
     } 
    } 
    }); 
}; 

我用自帶的SEAN模板的,它使用ACL設置權限。此方法在您的用戶登錄時正常工作。

問題是,當我從註冊表單創建一個新用戶時,它會自動登錄到系統上,但它沒有得到與它的角色,並且req.user.roles爲空。我有一個過程,在註冊後用戶需要完成另一個表單來設置所有內容,但是由於角色問題,策略無法識別角色並給出「訪客」角色,導致我無法訪問我的路由。如果我在註冊自動登錄後註銷,則一切正常,因爲現在角色設置正常。

我需要在redis服務器上設置其他的東西嗎?或者我需要做什麼?

回答

0

經過長時間的研究,我弄清楚瞭如何解決這個問題。由此可見,我在註冊控制器上遺失了一行代碼。我需要像這樣保存用戶後登錄req.user權利:

user.save().then(function() { 
    req.login(user, function(err) { 
     if (err) 
     res.status(400).send({ 
      message: errorHandler.getErrorMessage(err) 
     }); 
     res.json(user); 
    }); 
    }).catch(function(err) { 

第二行是我失蹤。

希望這對未來的其他人有所幫助。