Edits history of script submission #22687 for ' Update Role (auth0)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    async function getManagementToken(auth: RT.Auth0): Promise<string> {
      const response = await fetch(`https://${auth.domain}/oauth/token`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          grant_type: "client_credentials",
          client_id: auth.client_id,
          client_secret: auth.client_secret,
          audience: `https://${auth.domain}/api/v2/`,
        }),
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { access_token } = (await response.json()) as { access_token: string }
      return access_token
    }
    /**
     * Update Role
     * Update a role's name and/or description.
     */
    export async function main(
      auth: RT.Auth0,
      role_id: string,
      name: string | undefined,
      description: string | undefined
    ) {
      const token = await getManagementToken(auth)
      const url = new URL(`https://${auth.domain}/api/v2/roles/${role_id}`)
      const body: { [key: string]: any } = {}
      if (name !== undefined && name !== "") body.name = name
      if (description !== undefined) body.description = description
    
      const response = await fetch(url, {
        method: "PATCH",
        headers: {
          Authorization: `Bearer ${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 5 days ago