Skip to content

Commit

Permalink
feat: refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
NaMax66 committed Dec 26, 2024
1 parent 87039a4 commit 7e2f6b2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
23 changes: 4 additions & 19 deletions client-app/core/composables/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import { useLocalStorage, createGlobalState } from "@vueuse/core";
import type { AfterFetchContext } from "@vueuse/core";
import { createGlobalState, useLocalStorage } from "@vueuse/core";
import { computed, ref } from "vue";
import { useFetch } from "@/core/api/common";
import { errorHandler, toServerError } from "@/core/api/common/utils";
import { globals } from "@/core/globals";
import { TabsType, unauthorizedErrorEvent, useBroadcast, userBeforeUnauthorizeEvent } from "@/shared/broadcast";
import type { AfterFetchContext } from "@vueuse/core";

type IdentityErrorType = {
code?: string;
description?: string;
};

type ConnectTokenResponseType = {
expires_in?: number;
access_token?: string;
refresh_token?: string;
errors?: Array<IdentityErrorType>;
error: string;
token_type?: string;
};
import type { ConnectTokenResponseType } from "../types";

function _useAuth() {
const broadcast = useBroadcast();
Expand Down Expand Up @@ -107,13 +94,11 @@ function _useAuth() {
}

async function externalSignInCallback(): Promise<void> {
const params = new URLSearchParams({
getTokenParams.value = new URLSearchParams({
grant_type: "external_sign_in",
scope: "offline_access",
});

getTokenParams.value = params;

await (getTokenRequest = getToken(true));
}

Expand Down
36 changes: 17 additions & 19 deletions client-app/core/composables/useImpersonate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ 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;
};
import type { ConnectTokenResponseType } from "../types";

export function _useImpersonate() {
const { setTokenType, setAccessToken, setExpiresAt, setRefreshToken } = useAuth();
Expand All @@ -39,20 +33,24 @@ export function _useImpersonate() {
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);
const { access_token, token_type, expires_in, refresh_token, errors } = data.value;

status.value = "success";
notifications.success({ text: t("pages.account.impersonate.success") });
if (access_token && token_type && expires_in && refresh_token) {
setAccessToken(access_token);
setExpiresAt(expires_in);
setTokenType(token_type);
setRefreshToken(refresh_token);
status.value = "success";
notifications.success({ text: t("pages.account.impersonate.success") });

// reload all tabs to renew state
setTimeout(() => {
void broadcast.emit(reloadAndOpenMainPage, null, TabsType.ALL);
}, 1000);
// reload all tabs to renew state
setTimeout(() => {
void broadcast.emit(reloadAndOpenMainPage, null, TabsType.ALL);
}, 1000);
} else {
Logger.error(impersonate.name, errors);
status.value = "error";
}
}
} catch (e) {
Logger.error(impersonate.name, e);
Expand Down
14 changes: 10 additions & 4 deletions client-app/core/types/connect-token-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
type IdentityErrorType = {
code?: string;
description?: string;
};

export type ConnectTokenResponseType = {
expires_in: number;
access_token: string;
token_type: string;
error?: string;
expires_in?: number;
access_token?: string;
refresh_token?: string;
errors?: Array<IdentityErrorType>;
token_type?: string;
};

0 comments on commit 7e2f6b2

Please sign in to comment.