Skip to content

Commit

Permalink
fix: fix impersonate
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-dudarev committed Dec 24, 2024
1 parent 2cd478a commit 859cca3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
14 changes: 1 addition & 13 deletions client-app/core/composables/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,6 @@ function _useAuth() {
await (getTokenRequest = getToken(true));
}

async function impersonate(userId: string): Promise<void> {
const params = new URLSearchParams({
grant_type: "impersonate",
scope: "offline_access",
user_id: userId,
});

getTokenParams.value = params;

await (getTokenRequest = getToken(true));
}

async function refresh(organizationId?: string) {
const params = new URLSearchParams({
grant_type: "refresh_token",
Expand Down Expand Up @@ -190,13 +178,13 @@ function _useAuth() {
isAuthorizing,
authorize,
externalSignInCallback,
impersonate,
refresh,
unauthorize,

setTokenType,
setAccessToken,
setExpiresAt,
setRefreshToken,
};
}

Expand Down
34 changes: 29 additions & 5 deletions client-app/core/composables/useImpersonate.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
import { createGlobalState } from "@vueuse/core";
import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { useFetch } from "@/core/api/common";
import { useAuth } from "@/core/composables/useAuth";
import { Logger } from "@/core/utilities";
import { TabsType, useBroadcast, reloadAndOpenMainPage } from "@/shared/broadcast";
import { useNotifications } from "@/shared/notification";

type ConnectTokenResponseType = {
expires_in: number;
access_token: string;
refresh_token: string;
token_type: string;
};

export function _useImpersonate() {
const { impersonate, errors } = useAuth();
const { setTokenType, setAccessToken, setExpiresAt, setRefreshToken } = useAuth();
const broadcast = useBroadcast();
const status = ref();
const notifications = useNotifications();
const { t } = useI18n();

async function _impersonate(userId: string) {
async function impersonate(userId: string) {
status.value = "loading";

try {
await impersonate(userId);
const { error, data } = await useFetch("/connect/token")
.post(
new URLSearchParams({
grant_type: "impersonate",
scope: "offline_access",
user_id: userId,
}),
"application/x-www-form-urlencoded",
)
.json<ConnectTokenResponseType>();

if (errors.value?.length) {
if (!data.value || error.value) {
status.value = "error";
} else {
const { access_token, token_type, expires_in, refresh_token } = data.value;

setAccessToken(access_token);
setExpiresAt(expires_in);
setTokenType(token_type);
setRefreshToken(refresh_token);

status.value = "success";
notifications.success({ text: t("pages.account.impersonate.success") });

Expand All @@ -37,7 +61,7 @@ export function _useImpersonate() {
}

return {
impersonate: _impersonate,
impersonate: impersonate,
status,
};
}
Expand Down

0 comments on commit 859cca3

Please sign in to comment.