Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 22 additions & 53 deletions src/AuthManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,70 +285,39 @@ export class AuthManager {
firstName?: string,
lastName?: string,
email?: string,
password?: { old: string, new: string, },
oldPassword?: string,
newPassword?: string,
}): Promise<boolean> {
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`,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

me/update_profile

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(
Expand Down