Edits history of script submission #22607 for ' Set User Lifecycle State (okta)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Set User Lifecycle State
     * 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.
     */
    export async function main(
      auth: RT.Okta,
      user_id: string,
      operation:
        | "activate"
        | "deactivate"
        | "suspend"
        | "unsuspend"
        | "unlock"
        | "reactivate"
        | "expire_password"
        | "reset_factors",
      send_email: boolean | undefined
    ) {
      const url = new URL(
        `${auth.org_url}/api/v1/users/${encodeURIComponent(user_id)}/lifecycle/${operation}`
      )
      if (send_email !== undefined)
        url.searchParams.append("sendEmail", String(send_email))
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `SSWS ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      if (response.status === 204) return { success: true }
      return await response.json()
    }
    

    Submitted by hugo989 6 days ago