ChangeUserPassword Handler + API

This commit is contained in:
al8763be 2024-04-17 22:04:23 +02:00
parent 47d4bda99b
commit 0176f78067
8 changed files with 163 additions and 7 deletions

View file

@ -283,6 +283,18 @@ interface API {
newProjectName: string,
token: string,
): Promise<APIResponse<string>>;
/**
* Changes the password of a user
* @param {string} username The username of the user
* @param {string} newPassword The new password
* @param {string} token The authentication token
*/
changeUserPassword(
username: string,
newPassword: string,
token: string,
): Promise<APIResponse<string>>;
}
/** An instance of the API */
@ -1041,4 +1053,31 @@ export const api: API = {
return { success: false, message: "Failed to change project name" };
}
},
async changeUserPassword(
username: string,
newPassword: string,
token: string,
): Promise<APIResponse<string>> {
try {
const response = await fetch(
`/api/changePassword/${username}?newPassword=${newPassword}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
},
);
if (!response.ok) {
return { success: false, message: "Failed to change password" };
} else {
return { success: true, message: "Password changed" };
}
} catch (e) {
return { success: false, message: "Failed to change password" };
}
},
};