Roles.userIsInRole无效

时间:2017-04-18 17:57:24

标签: meteor alanning-roles

我正在用React构建一个Meteor应用程序。我的问题是alanning:roles包似乎没有按预期工作。

没有控制台错误,但基本上以下代码无法正常工作。

const composer = (props, onData) => {
    const loggingIn = Meteor.loggingIn()
    const loggedInUserId = Meteor.userId()
    console.log(loggedInUserId)
    let authenticated
    if (Roles.userIsInRole(loggedInUserId, ['admin']))
        authenticated = 'admin';
    else if (Roles.userIsInRole(loggedInUserId, ['school']))
        authenticated = 'school'
    else if (Roles.userIsInRole(loggedInUserId, ['teacher']))
        authenticated = 'teacher'
    else
        authenticated = 'anonymous'
    console.log(authenticated)
    onData(null, {
        loggingIn,
        authenticated
    })
}

loggedInUserId变量在第一个id中成功显示登录用户的console.log()。但是,无论用户登录了什么,authenticated的值始终为anonymous

我可以确认loggedInUserId肯定匹配相对角色的用户,但结果始终保持不变。

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

我通过将composer更改为此来解决了我的问题:

const composer = (props, onData) => {
    const loggingIn = Meteor.loggingIn()
    const loggedInUserId = Meteor.userId()

    let authenticated
    if (Roles.userIsInRole(loggedInUserId, 'admin', 'default-group'))
        authenticated = 'admin';
    else if (Roles.userIsInRole(loggedInUserId, 'school', 'default-group'))
        authenticated = 'school'
    else if (Roles.userIsInRole(loggedInUserId, 'teacher', 'default-group'))
        authenticated = 'teacher'
    else
        authenticated = 'anonymous'

    onData(null, {
        loggingIn,
        authenticated
    })
}

在分配角色时未指定group时,它会自动将角色分配给default-group