0

Set User Lifecycle State

by
Published 4 days ago

Run a lifecycle operation (activate, deactivate, suspend, unsuspend, unlock, reactivate, expire_password, reset_factors) on a user.

Script okta Verified

The script

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

3
/**
4
 * Set User Lifecycle State
5
 * Run a lifecycle operation on a user: activate, deactivate, suspend, unsuspend, unlock, reactivate, expire_password, or reset_factors. `send_email` only applies to activate / deactivate / reactivate. Most operations return 200 with the updated state; some return an empty body.
6
 */
7
export async function main(
8
  auth: RT.Okta,
9
  user_id: string,
10
  operation:
11
    | "activate"
12
    | "deactivate"
13
    | "suspend"
14
    | "unsuspend"
15
    | "unlock"
16
    | "reactivate"
17
    | "expire_password"
18
    | "reset_factors",
19
  send_email: boolean | undefined
20
) {
21
  const url = new URL(
22
    `${auth.org_url}/api/v1/users/${encodeURIComponent(user_id)}/lifecycle/${operation}`
23
  )
24
  if (send_email !== undefined)
25
    url.searchParams.append("sendEmail", String(send_email))
26

27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      Authorization: `SSWS ${auth.token}`,
31
      Accept: "application/json",
32
    },
33
  })
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  if (response.status === 204) return { success: true }
40
  return await response.json()
41
}
42