Edits history of script submission #22595 for ' Create User (okta)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create User
     * Create a user in your org. `profile` holds the Okta user profile (firstName, lastName, email, login, ...). Optionally set `credentials` (e.g. { password: { value } }), assign a user type via `type_id`, and add the user to groups via `group_ids`. When `activate` is true (the default) the user is activated on creation.
     */
    export async function main(
      auth: RT.Okta,
      profile: { [key: string]: any },
      activate: boolean | undefined,
      credentials: { [key: string]: any } | undefined,
      type_id: string | undefined,
      group_ids: string[] | undefined
    ) {
      const url = new URL(`${auth.org_url}/api/v1/users`)
      if (activate !== undefined)
        url.searchParams.append("activate", String(activate))
    
      const body: { [key: string]: any } = { profile }
      if (credentials !== undefined) body.credentials = credentials
      if (type_id !== undefined && type_id !== "") body.type = { id: type_id }
      if (group_ids !== undefined && group_ids.length > 0) body.groupIds = group_ids
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `SSWS ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 days ago