0

Update User

by
Published 4 days ago

Partially update a user's profile and/or credentials; only the fields you pass are changed.

Script okta Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
/**
4
 * Update User
5
 * Partially update a user's profile and/or credentials. Only the fields you pass are changed (the rest are left intact). Pass `profile` as a map of profile attributes to set, and/or `credentials` (e.g. { password: { value } }).
6
 */
7
export async function main(
8
  auth: RT.Okta,
9
  user_id: string,
10
  profile: { [key: string]: any } | undefined,
11
  credentials: { [key: string]: any } | undefined
12
) {
13
  const url = new URL(
14
    `${auth.org_url}/api/v1/users/${encodeURIComponent(user_id)}`
15
  )
16

17
  const body: { [key: string]: any } = {}
18
  if (profile !== undefined) body.profile = profile
19
  if (credentials !== undefined) body.credentials = credentials
20

21
  const response = await fetch(url, {
22
    method: "POST",
23
    headers: {
24
      Authorization: `SSWS ${auth.token}`,
25
      "Content-Type": "application/json",
26
      Accept: "application/json",
27
    },
28
    body: JSON.stringify(body),
29
  })
30

31
  if (!response.ok) {
32
    throw new Error(`${response.status} ${await response.text()}`)
33
  }
34

35
  return await response.json()
36
}
37