0

Remove User from Group

by
Published 4 days ago

Remove a user from an OKTA_GROUP group.

Script okta Verified

The script

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

3
export type DynSelect_group_id = string
4

5
// Dropdown of the org's groups so users pick a group instead of typing its opaque ID.
6
export async function group_id(auth: RT.Okta) {
7
  const url = new URL(`${auth.org_url}/api/v1/groups`)
8
  url.searchParams.append("limit", "200")
9
  const response = await fetch(url, {
10
    headers: {
11
      Authorization: `SSWS ${auth.token}`,
12
      Accept: "application/json",
13
    },
14
  })
15
  if (!response.ok) {
16
    throw new Error(`${response.status} ${await response.text()}`)
17
  }
18
  const groups = (await response.json()) as {
19
    id: string
20
    profile: { name: string }
21
  }[]
22
  return groups.map((g) => ({
23
    value: g.id,
24
    label: g.profile?.name ? `${g.profile.name} (${g.id})` : g.id,
25
  }))
26
}
27

28
/**
29
 * Remove User from Group
30
 * Remove a user from a group. Only works for groups of type OKTA_GROUP. Returns no content on success.
31
 */
32
export async function main(
33
  auth: RT.Okta,
34
  group_id: DynSelect_group_id,
35
  user_id: string
36
) {
37
  const url = new URL(
38
    `${auth.org_url}/api/v1/groups/${group_id}/users/${encodeURIComponent(user_id)}`
39
  )
40

41
  const response = await fetch(url, {
42
    method: "DELETE",
43
    headers: {
44
      Authorization: `SSWS ${auth.token}`,
45
      Accept: "application/json",
46
    },
47
  })
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  if (response.status === 204) return { success: true }
54
  return await response.json()
55
}
56