Edits history of script submission #23064 for ' Reset User Password (gworkspace)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Reset User Password
     * Set a new password for a user (8 to 100 characters) and, by default, force them to change it at next sign-in. Existing sessions are not signed out.
     */
    export async function main(
      auth: RT.Gworkspace,
      user_key: string,
      password: string,
      change_password_at_next_login: boolean = true,
    ) {
      const response = await fetch(
        `https://admin.googleapis.com/admin/directory/v1/users/${encodeURIComponent(user_key)}`,
        {
          method: "PATCH",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            password,
            changePasswordAtNextLogin: change_password_at_next_login,
          }),
        },
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 1 hour ago