From dfc9632110e74833f1efcfbba14f73f06281f5db Mon Sep 17 00:00:00 2001 From: SujanSanjula96 Date: Wed, 8 Jan 2025 17:45:33 +0530 Subject: [PATCH 1/3] Fix change password failure for suborg users via myaccount --- apps/myaccount/src/api/change-password.ts | 12 +++++++++--- .../components/change-password/change-password.tsx | 7 ++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apps/myaccount/src/api/change-password.ts b/apps/myaccount/src/api/change-password.ts index 1908fe512d9..36c30a5bf9b 100644 --- a/apps/myaccount/src/api/change-password.ts +++ b/apps/myaccount/src/api/change-password.ts @@ -36,7 +36,7 @@ import { store } from "../store"; * @param newPassword - newly assigned password. * @returns axiosResponse - a promise containing the response. */ -export const updatePassword = (currentPassword: string, newPassword: string): Promise => { +export const updatePassword = (currentPassword: string, newPassword: string, isSubOrgUser: boolean = false, userOrganizationId: string = null): Promise => { // 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 @@ -45,14 +45,20 @@ 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 = 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 = url; + if (isSubOrgUser) { + updatedUrl = url.replace(/\/t\/[^/]+\//, `/t/${userOrganizationId}/`) + } const requestConfig: AxiosRequestConfig = { data: { @@ -71,7 +77,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 }; diff --git a/apps/myaccount/src/components/change-password/change-password.tsx b/apps/myaccount/src/components/change-password/change-password.tsx index 558f907fc8e..1b114e4c28d 100644 --- a/apps/myaccount/src/components/change-password/change-password.tsx +++ b/apps/myaccount/src/components/change-password/change-password.tsx @@ -19,6 +19,7 @@ import { TestableComponentInterface } from "@wso2is/core/models"; import { Field, FormValue, Forms, useTrigger } from "@wso2is/forms"; import { PasswordValidation, ValidationStatusInterface } from "@wso2is/react-components"; +import { OrganizationType } from "@wso2is/admin.organizations.v1/constants"; import React, { Dispatch, FunctionComponent, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useDispatch, useSelector } from "react-redux"; @@ -82,6 +83,10 @@ export const ChangePassword: FunctionComponent = (props: Ch const endUserSession: () => Promise = 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. */ @@ -177,7 +182,7 @@ export const ChangePassword: FunctionComponent = (props: Ch */ const changePassword = () => { - updatePassword(currentPassword, newPassword) + updatePassword(currentPassword, newPassword, isSubOrgUser, userOrganizationId) .then((response: any) => { if (response.status && response.status === 200) { // reset the form. From bfcb9224e2768bb3bf79bf63d821c0c544c1af95 Mon Sep 17 00:00:00 2001 From: SujanSanjula96 Date: Thu, 9 Jan 2025 05:08:35 +0530 Subject: [PATCH 2/3] Fix es lint issues --- apps/myaccount/src/api/change-password.ts | 11 +++++++---- .../components/change-password/change-password.tsx | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/myaccount/src/api/change-password.ts b/apps/myaccount/src/api/change-password.ts index 36c30a5bf9b..499f63223ea 100644 --- a/apps/myaccount/src/api/change-password.ts +++ b/apps/myaccount/src/api/change-password.ts @@ -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, isSubOrgUser: boolean = false, userOrganizationId: string = null): Promise => { +export const updatePassword = (currentPassword: string, newPassword: string, + isSubOrgUser: boolean = false, userOrganizationId: string = null): Promise => { // 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 @@ -45,7 +46,8 @@ export const updatePassword = (currentPassword: string, newPassword: string, isS // See https://github.com/asgardio/asgardio-js-oidc-sdk/issues/45 for progress. // httpRequest.disableHandler(); - const tenantDomain = isSubOrgUser ? userOrganizationId : store.getState().authenticationInformation.tenantDomain; + const tenantDomain: string = isSubOrgUser ? userOrganizationId : + store.getState().authenticationInformation.tenantDomain; const username: string = [ store.getState().authenticationInformation?.profileInfo.userName, "@", @@ -55,9 +57,10 @@ export const updatePassword = (currentPassword: string, newPassword: string, isS const encoder: TextEncoder = new TextEncoder(); const encodedPassword: string = String.fromCharCode(...encoder.encode(currentPassword)); const url: string = store.getState().config.endpoints.me; - let updatedUrl = url; + let updatedUrl: string = url; + if (isSubOrgUser) { - updatedUrl = url.replace(/\/t\/[^/]+\//, `/t/${userOrganizationId}/`) + updatedUrl = url.replace(/\/t\/[^/]+\//, `/t/${userOrganizationId}/`); } const requestConfig: AxiosRequestConfig = { diff --git a/apps/myaccount/src/components/change-password/change-password.tsx b/apps/myaccount/src/components/change-password/change-password.tsx index 1b114e4c28d..a9dfa984407 100644 --- a/apps/myaccount/src/components/change-password/change-password.tsx +++ b/apps/myaccount/src/components/change-password/change-password.tsx @@ -16,10 +16,10 @@ * 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"; -import { OrganizationType } from "@wso2is/admin.organizations.v1/constants"; import React, { Dispatch, FunctionComponent, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useDispatch, useSelector } from "react-redux"; From dc905cbbb85b59b2c0a88c3f3044192b2548c3ed Mon Sep 17 00:00:00 2001 From: SujanSanjula96 Date: Thu, 9 Jan 2025 06:26:25 +0530 Subject: [PATCH 3/3] =?UTF-8?q?Add=20changeset=20=F0=9F=A6=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/real-keys-move.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/real-keys-move.md diff --git a/.changeset/real-keys-move.md b/.changeset/real-keys-move.md new file mode 100644 index 00000000000..da07d0df993 --- /dev/null +++ b/.changeset/real-keys-move.md @@ -0,0 +1,5 @@ +--- +"@wso2is/myaccount": patch +--- + +Fix change password failure for suborg users via myaccount