diff --git a/src/AuthManager.ts b/src/AuthManager.ts index 1c0991f..136c63b 100644 --- a/src/AuthManager.ts +++ b/src/AuthManager.ts @@ -285,70 +285,39 @@ export class AuthManager { firstName?: string, lastName?: string, email?: string, - password?: { old: string, new: string, }, + oldPassword?: string, + newPassword?: string, }): Promise { const accessToken = localStorage.getItem('access_token'); if (!accessToken) { throw new Error('Access token not found'); } - // Update name - if (update.firstName || update.lastName) { - const response = await axios.post( - `${this.authServer}auth/email/update_profile`, - { - realm_name: this.realmName, - ...(update.firstName && { first_name: update.firstName }), - ...(update.lastName && { last_name: update.lastName }), - }, - { - headers: { Authorization: `Bearer ${accessToken}` }, - }, - ); - if (response.data.error || response.data.errors) { - throw new Error(response.data.error || response.data.message); - } - } + // Build the payload conditionally + const payload: any = { + realm_name: this.realmName, + ...(update.firstName && { first_name: update.firstName }), + ...(update.lastName && { last_name: update.lastName }), + ...(update.email && { email: update.email }), + ...(update.oldPassword && update.newPassword && { + old_password: update.oldPassword, + new_password: update.newPassword, + }), + }; - // Update email - if (update.email) { - const response = await axios.post( - `${this.authServer}auth/email/change_email`, - { - realm_name: this.realmName, - email: update.email, - }, - { - headers: { Authorization: `Bearer ${accessToken}` }, - }, - ); - if (response.data.error || response.data.errors) { - throw new Error(response.data.error || response.data.message); + const response = await axios.post( + `${this.authServer}auth/email/update_account`, + payload, + { + headers: { Authorization: `Bearer ${accessToken}` }, } - } + ); - // Update password - if (update.password && update.email) { - const response = await axios.post( - `${this.authServer}auth/email/change_pass`, - { - realm_name: this.realmName, - email: update.email, - old_password: update.password.old, - new_password: update.password.new, - }, - { - headers: { Authorization: `Bearer ${accessToken}` }, - }, - ); - if (response.data.error || response.data.errors) { - throw new Error(response.data.error || response.data.message); - } - } else if (update.password && !update.email) { - throw new Error('Email is required to change password'); + if (response.data.error || response.data.errors) { + throw new Error(response.data.error || response.data.message); } - return true; + return response.status === 200; } public async changePassword(