Fixes: 1) Reorder `router.push` in `useLogout` to properly clear cookies before redirection; 2) Resolve issues with inconsistent access token handling during Apollo header configuration; Extra: 1) Cleanup comments in `useRefresh
92 lines
No EOL
2.3 KiB
TypeScript
92 lines
No EOL
2.3 KiB
TypeScript
import { REFRESH } from '@/graphql/mutations/auth';
|
|
import { useAppConfig } from '~/composables/config';
|
|
import { useLocaleRedirect } from '~/composables/languages';
|
|
import { useUserStore } from '~/stores/user';
|
|
import { isGraphQLError } from '~/utils/error';
|
|
import {DEFAULT_LOCALE} from "~/config/constants";
|
|
import {useNotification} from "~/composables/notification";
|
|
import {useUserBaseData} from "~/composables/user";
|
|
|
|
export function useRefresh() {
|
|
const { t } = useI18n();
|
|
const userStore = useUserStore();
|
|
const { COOKIES_REFRESH_TOKEN_KEY, COOKIES_LOCALE_KEY, COOKIES_ACCESS_TOKEN_KEY } = useAppConfig();
|
|
const { checkAndRedirect } = useLocaleRedirect();
|
|
|
|
const { mutate, loading, error } = useMutation(REFRESH);
|
|
|
|
async function refresh() {
|
|
const cookieRefresh = useCookie(
|
|
COOKIES_REFRESH_TOKEN_KEY,
|
|
{
|
|
default: () => '',
|
|
path: '/'
|
|
}
|
|
);
|
|
const cookieAccess = useCookie(
|
|
COOKIES_ACCESS_TOKEN_KEY,
|
|
{
|
|
default: () => '',
|
|
path: '/'
|
|
}
|
|
);
|
|
const cookieLocale = useCookie(
|
|
COOKIES_LOCALE_KEY,
|
|
{
|
|
default: () => DEFAULT_LOCALE,
|
|
path: '/'
|
|
}
|
|
);
|
|
|
|
if (!cookieRefresh.value) {
|
|
return;
|
|
}
|
|
|
|
const result = await mutate({ refreshToken: cookieRefresh.value });
|
|
const data = result?.data?.refreshJwtToken;
|
|
if (!data) {
|
|
return;
|
|
}
|
|
|
|
userStore.setUser(data.user);
|
|
cookieAccess.value = data.accessToken;
|
|
|
|
if (data.user.language !== cookieLocale.value) {
|
|
await checkAndRedirect(data.user.language);
|
|
}
|
|
|
|
cookieRefresh.value = data.refreshToken;
|
|
|
|
// await useWishlist();
|
|
// await useOrders({
|
|
// userEmail: data.user.email,
|
|
// status: "PENDING"
|
|
// });
|
|
// await usePromocodes();
|
|
|
|
await nextTick();
|
|
|
|
await useUserBaseData(data.user.email);
|
|
}
|
|
|
|
watch(error, (err) => {
|
|
if (!err) return;
|
|
console.error('useRefresh error:', err);
|
|
let message = t('popup.errors.defaultError');
|
|
if (isGraphQLError(err)) {
|
|
message = err.graphQLErrors?.[0]?.message || message;
|
|
} else {
|
|
message = err.message;
|
|
}
|
|
useNotification({
|
|
message,
|
|
type: 'error',
|
|
title: t('popup.errors.main')
|
|
});
|
|
})
|
|
|
|
return {
|
|
refresh,
|
|
loading
|
|
};
|
|
} |