nickredmark / ooth

User identity/authentication/accounts management microservice for node.js
https://nmaro.github.io/ooth/
MIT License
605 stars 65 forks source link

Accessing users' roles through withUser HOC #70

Closed braytonstafford closed 6 years ago

braytonstafford commented 6 years ago

I was hoping to be able to access the currently authenticated user's roles via this.props.user inside a react component wrapped with withUser HOC.

I've followed the example in the docs on implementing user roles.

I manually added the admin role to my user and verified in mongodb that roles on the user is an array.

db.users.update(
  {
    _id: ObjectId("5ba46cac9a370b50b10ae4f4")
  },
  {
    $set: {
      roles: ["admin"]
    }
  }
);

Then inside the withUser wrapped component if I console.log('user ', this.props.user.roles) it prints

user  { _id: '5ba46cac9a370b50b10ae4f4',
  local: { email: 'no-reply@gmail.com' },
  profile:
   { clientId: '87e6a3ac-5891-4322-a7ca-0df325e1fddd',
     firstName: 'First',
     lastName: 'Last',
     eulaAccepted: true },
  roles: {} }

I was expecting roles to be an array of strings?

Sidenote: After manually updating my role to admin I was able to use oothClient to successfully update a different user's roles as the docs' explain.

await oothClient.method("roles", "set", {
  userId: "XXX",
  roles: ["editor", "author"] // will overwrite all previous roles the user had
});
nickredmark commented 6 years ago

Hey sorry I never answered. The reason is that the user object is structured like this:

{ [strategyname]: { [key]: value } }

so to set the role correctly with mongodb the query should be

db.users.update(
  {
    _id: ObjectId("5ba46cac9a370b50b10ae4f4")
  },
  {
    $set: {
      roles: {roles:["admin"]}
    }
  }
);