Skip to content

Updating

To update a users data, you must pass a user_id and an update table with the values you wish to update. On a successful update, you will recieve the newly updated record in the event response.

Note

You can update multiple values in the update table of the method.

The following keys can be updated on the users record:

  • username (string)
  • password (string)
  • email (string)
  • group (string)
  • extra (table)
  • active (boolean)

OAuth Users

To update a users OAuth provider information, see the updateAuthProvider method.

Example

local function onUserUpdate( evt )
  if evt.error then
    print(evt.error)
  else
    print(evt.result.user_id) --result contains updated object
  end
end

core.users.update({
  user_id = "289bc36e-0df7-44df-99b1-a6543c6f45eb",
  update = {
    email = "app@email.com"
  }
}, onUserUpdate)

Username

To update a users name, pass the username key to the update table. If a user with the same name already exists in the same application scope, the record will not be updated and an error will be returned with a status of 600.

local function onUserUpdate( evt )
  if evt.error then
    if evt.status == 600 then
      --user exists, handle appropriately
    else
      print(evt.error)
    end
  else
    print(evt.result.user_id)
  end
end

core.users.update({
  user_id = "289bc36e-0df7-44df-99b1-a6543c6f45eb",
  update = {
    username = "Sammy"
  }
}, onUserUpdate)

Password

To update a users password, pass the password key to the update table.

Important

Passwords are automatically hashed, do not hash the password yourself.

local function onUserUpdate( evt )
  if evt.error then
    print(evt.error)
  else
    print(evt.result.user_id)
  end
end

core.users.update({
  user_id = "289bc36e-0df7-44df-99b1-a6543c6f45eb",
  update = {
    password = "abcd"
  }
}, onUserUpdate)

Extra Metadata

To add, update, or remove a users "extra" metadata, pass the extra table to the update table.

Tip

You can add, update, and remove multiple keys in the same call.

Update/Add Key

local function onUserUpdate( evt )
  if evt.error then
    print(evt.error)
  else
    print(evt.result.user_id)
  end
end

core.users.update({
  user_id = "289bc36e-0df7-44df-99b1-a6543c6f45eb",
  update = {
    extra = {
      color = "Red",
      winner = true
    }
  }
}, onUserUpdate)

Removing a Key

To clear and remove a key from the users "extra" metadata, you must pass the core.users.NULL constant to the key.

local function onUserUpdate( evt )
  if evt.error then
    print(evt.error)
  else
    print(evt.result.user_id)
  end
end

core.users.update({
  user_id = "289bc36e-0df7-44df-99b1-a6543c6f45eb",
  update = {
    extra = {
      winner = core.users.NULL --remove 'winner' key
    }
  }
}, onUserUpdate)

Active State

To change the "active" status of a user, pass the active key as a boolean value.

local function onUserUpdate( evt )
  if evt.error then
    print(evt.error)
  else
    print(evt.result)
  end
end

core.users.update({
  user_id = "289bc36e-0df7-44df-99b1-a6543c6f45eb",
  update = {
    active = false
  }
}, onUserUpdate)