Fixes: 1) Replace `ElNotification` calls with `useNotification` utility across all authentication and user-related composables; 2) Add missing semicolons in multiple index exports and styled components; 3) Resolve issues with reactivity in `useStore` composable by renaming and restructuring product variables; Extra: 1) Refactor localized strings and translations for better readability and maintenance; 2) Tweak styles including scoped styles, z-index adjustments, and SCSS mixins; 3) Remove unused components and imports to streamline storefront layout.
77 lines
No EOL
1.9 KiB
TypeScript
77 lines
No EOL
1.9 KiB
TypeScript
import { REFRESH } from '@/graphql/mutations/auth';
|
|
import { useAppConfig } from '~/composables/config';
|
|
import { useLocaleRedirect } from '~/composables/languages';
|
|
import { useWishlist } from '~/composables/wishlist';
|
|
import { usePendingOrder } from '~/composables/orders';
|
|
import { useUserStore } from '~/stores/user';
|
|
import { isGraphQLError } from '~/utils/error';
|
|
import {DEFAULT_LOCALE} from "~/config/constants";
|
|
import {useNotification} from "~/composables/notification";
|
|
|
|
export function useRefresh() {
|
|
const { t } = useI18n();
|
|
const userStore = useUserStore();
|
|
const { COOKIES_REFRESH_TOKEN_KEY, COOKIES_LOCALE_KEY } = useAppConfig();
|
|
const { checkAndRedirect } = useLocaleRedirect();
|
|
|
|
const { mutate, loading, error } = useMutation(REFRESH);
|
|
|
|
async function refresh() {
|
|
const cookieRefresh = useCookie(
|
|
COOKIES_REFRESH_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);
|
|
|
|
if (data.user.language !== cookieLocale.value) {
|
|
await checkAndRedirect(data.user.language);
|
|
}
|
|
|
|
cookieRefresh.value = data.refreshToken
|
|
|
|
await useWishlist();
|
|
await usePendingOrder(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,
|
|
'error',
|
|
t('popup.errors.main')
|
|
);
|
|
})
|
|
|
|
return {
|
|
refresh,
|
|
loading
|
|
};
|
|
} |