Roles.userIsInRole 不工作

Roles.userIsInRole not working

我正在使用 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 变量在第一个 console.log() 中成功显示登录用户的 id。但是,authenticated 的值始终是 anonymous,无论登录的是哪个用户。

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

这是怎么回事?

我通过将 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