Skip to content

Commit

Permalink
Merge pull request #7278 from SujanSanjula96/org-user-password-update
Browse files Browse the repository at this point in the history
Fix change password failure for suborg users via myaccount
  • Loading branch information
SujanSanjula96 authored Jan 10, 2025
2 parents 7de31d7 + dc905cb commit c277696
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-keys-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wso2is/myaccount": patch
---

Fix change password failure for suborg users via myaccount
15 changes: 12 additions & 3 deletions apps/myaccount/src/api/change-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import { store } from "../store";
* @param newPassword - newly assigned password.
* @returns axiosResponse - a promise containing the response.
*/
export const updatePassword = (currentPassword: string, newPassword: string): Promise<AxiosResponse> => {
export const updatePassword = (currentPassword: string, newPassword: string,
isSubOrgUser: boolean = false, userOrganizationId: string = null): Promise<AxiosResponse> => {

// If the `httpRequest` method from SDK is used for the request, it causes the 401 to be handled by
// the callbacks set fot the application which will log the user out. Hence, axios will be used
Expand All @@ -45,14 +46,22 @@ export const updatePassword = (currentPassword: string, newPassword: string): Pr
// See https://github.com/asgardio/asgardio-js-oidc-sdk/issues/45 for progress.
// httpRequest.disableHandler();

const tenantDomain: string = isSubOrgUser ? userOrganizationId :
store.getState().authenticationInformation.tenantDomain;
const username: string = [
store.getState().authenticationInformation?.profileInfo.userName,
"@",
store.getState().authenticationInformation.tenantDomain
tenantDomain
].join("");
// In case the password contains non-ascii characters, converting to valid ascii format.
const encoder: TextEncoder = new TextEncoder();
const encodedPassword: string = String.fromCharCode(...encoder.encode(currentPassword));
const url: string = store.getState().config.endpoints.me;
let updatedUrl: string = url;

if (isSubOrgUser) {
updatedUrl = url.replace(/\/t\/[^/]+\//, `/t/${userOrganizationId}/`);
}

const requestConfig: AxiosRequestConfig = {
data: {
Expand All @@ -71,7 +80,7 @@ export const updatePassword = (currentPassword: string, newPassword: string): Pr
"Content-Type": "application/json"
},
method: HttpMethods.PATCH,
url: store.getState().config.endpoints.me,
url: updatedUrl,
withCredentials: true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* under the License.
*/

import { OrganizationType } from "@wso2is/admin.organizations.v1/constants";
import { TestableComponentInterface } from "@wso2is/core/models";
import { Field, FormValue, Forms, useTrigger } from "@wso2is/forms";
import { PasswordValidation, ValidationStatusInterface } from "@wso2is/react-components";
Expand Down Expand Up @@ -82,6 +83,10 @@ export const ChangePassword: FunctionComponent<ChangePasswordProps> = (props: Ch

const endUserSession: () => Promise<boolean> = useEndUserSession();

const userOrganizationId: string = useSelector((state: AppState) => state?.organization?.userOrganizationId);
const organizationType: string = useSelector((state: AppState) => state?.organization?.organizationType);
const isSubOrgUser: boolean = (organizationType === OrganizationType.SUBORGANIZATION);

/**
* Get the configurations.
*/
Expand Down Expand Up @@ -177,7 +182,7 @@ export const ChangePassword: FunctionComponent<ChangePasswordProps> = (props: Ch
*/
const changePassword = () => {

updatePassword(currentPassword, newPassword)
updatePassword(currentPassword, newPassword, isSubOrgUser, userOrganizationId)
.then((response: any) => {
if (response.status && response.status === 200) {
// reset the form.
Expand Down

0 comments on commit c277696

Please sign in to comment.