0

Reset a Database User's Password or Authentication Method

by
Published Dec 20, 2024

To reset the password for a database user, send a POST request to `/v2/databases/$DATABASE_ID/users/$USERNAME/reset_auth`. For `mysql` databases, the authentication method can be specifying by including a key in the JSON body called `mysql_settings` with the `auth_plugin` value specified. The response will be a JSON object with a `user` key. This will be set to an object containing the standard database user attributes.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Reset a Database User's Password or Authentication Method
7
 * To reset the password for a database user, send a POST request to
8
`/v2/databases/$DATABASE_ID/users/$USERNAME/reset_auth`.
9

10
For `mysql` databases, the authentication method can be specifying by
11
including a key in the JSON body called `mysql_settings` with the `auth_plugin`
12
value specified.
13

14
The response will be a JSON object with a `user` key. This will be set to an
15
object containing the standard database user attributes.
16

17
 */
18
export async function main(
19
  auth: Digitalocean,
20
  database_cluster_uuid: string,
21
  username: string,
22
  body: {
23
    mysql_settings?: {
24
      auth_plugin: "mysql_native_password" | "caching_sha2_password";
25
    };
26
  },
27
) {
28
  const url = new URL(
29
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/users/${username}/reset_auth`,
30
  );
31

32
  const response = await fetch(url, {
33
    method: "POST",
34
    headers: {
35
      "Content-Type": "application/json",
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46