To grant a role to an existing user, the accounts.roles.grant()
method can be utilized:
Example Usage
index.server.js
import joystick, { accounts } from '@joystick.js/node';
joystick.app({
accounts: {
events: {
on_login: ({ user }) => {
accounts.roles.grant(user?._id, 'active_account');
},
},
},
});
Above, we tap into the account event hook for the on_login()
event to grant a temporary role of active_account
to a user. When a role is granted to a user, it’s added to an array on their document/row in the database called roles
.
When calling accounts.roles.grant()
, if the specified role does not already exist in the roles
collection/table for the app, Joystick will automatically create it.
API
Definition
accounts.roles.grant(user_id: string, role: string) => void;
Parameters
- user_id string required
-
A user ID from your
users
collection/table in the database (e.g.,user._id
for MongoDB oruser.user_id
for PostgreSQL). - role string required
- A string defining the name of the role to grant to the specified user.