Refactored i18n configuration, replacing `DEFAULT_LOCALE` with `DEFAULT_LOCALE_FALLBACK` and enhancing environment-based locale validation. Improved cookie persistence for cart and wishlist, ensuring fallback handling for unauthenticated users. Enhancements: - Added `createProjectKey` utility for consistent project key generation. - Reworked cart and wishlist composables (`useOrderOverwrite`, `useWishlistOverwrite`) to decouple product identifier and handle cookies robustly. - Centralized `DEFAULT_LOCALE` logic for better maintainability. - Refined `useOrderSync` and `useWishlistSync` for clean synchronization across auth states. - Updated SCSS in hero and header styles for alignment corrections. Breaking Changes: `DEFAULT_LOCALE` constant removed; replaced with runtime config and fallback logic. Consumers must adapt to `DEFAULT_LOCALE_FALLBACK` and `$appHelpers.DEFAULT_LOCALE`.
121 lines
2.8 KiB
TypeScript
121 lines
2.8 KiB
TypeScript
import { useLogout } from '@composables/auth';
|
|
import { useLocaleRedirect } from '@composables/languages';
|
|
import { useNotification } from '@composables/notification';
|
|
import { useUserBaseData } from '@composables/user';
|
|
import { REFRESH } from '@graphql/mutations/auth';
|
|
|
|
export function useRefresh() {
|
|
const { t } = useI18n();
|
|
const router = useRouter();
|
|
const localePath = useLocalePath();
|
|
const userStore = useUserStore();
|
|
const { $appHelpers } = useNuxtApp();
|
|
|
|
const { checkAndRedirect } = useLocaleRedirect();
|
|
const { logout } = useLogout();
|
|
|
|
const { mutate, loading, error } = useMutation(REFRESH);
|
|
|
|
function isTokenInvalidError(error: unknown): boolean {
|
|
if (isGraphQLError(error)) {
|
|
const message = error.graphQLErrors?.[0]?.message?.toLowerCase() || '';
|
|
return (
|
|
message.includes('invalid refresh token') ||
|
|
message.includes('blacklist') ||
|
|
message.includes('expired') ||
|
|
message.includes('revoked')
|
|
);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function refresh() {
|
|
const cookieRefresh = useCookie($appHelpers.COOKIES_REFRESH_TOKEN_KEY, {
|
|
default: () => '',
|
|
path: '/',
|
|
});
|
|
const cookieAccess = useCookie($appHelpers.COOKIES_ACCESS_TOKEN_KEY, {
|
|
default: () => '',
|
|
path: '/',
|
|
});
|
|
const cookieLocale = useCookie($appHelpers.COOKIES_LOCALE_KEY, {
|
|
default: () => $appHelpers.DEFAULT_LOCALE,
|
|
path: '/',
|
|
});
|
|
|
|
if (!cookieRefresh.value) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const result = await mutate({
|
|
refreshToken: cookieRefresh.value,
|
|
});
|
|
const data = result?.data?.refreshJwtToken;
|
|
|
|
if (!data) {
|
|
return;
|
|
}
|
|
|
|
userStore.setUser(data.user);
|
|
cookieRefresh.value = data.refreshToken;
|
|
cookieAccess.value = data.accessToken;
|
|
|
|
if (data.user.language !== cookieLocale.value) {
|
|
await checkAndRedirect(data.user.language);
|
|
}
|
|
|
|
await useUserBaseData(data.user.email);
|
|
} catch (err) {
|
|
if (isTokenInvalidError(err)) {
|
|
await logout();
|
|
await router.push(localePath('/'));
|
|
|
|
return;
|
|
}
|
|
|
|
let message = t('popup.errors.defaultError');
|
|
if (isGraphQLError(err)) {
|
|
message = err.graphQLErrors?.[0]?.message || message;
|
|
} else if (err instanceof Error) {
|
|
message = err.message;
|
|
} else if (typeof err === 'string') {
|
|
message = err;
|
|
}
|
|
|
|
useNotification({
|
|
message,
|
|
type: 'error',
|
|
title: t('popup.errors.main'),
|
|
});
|
|
}
|
|
}
|
|
|
|
watch(error, async (err) => {
|
|
if (!err) return;
|
|
|
|
if (isTokenInvalidError(err)) {
|
|
await logout();
|
|
await router.push(localePath('/'));
|
|
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,
|
|
};
|
|
}
|