schon/storefront/app/composables/auth/useLogin.ts
Alexandr SaVBaD Waltz 2ea18eb8a6 feat(storefront): refactor i18n and cart/wishlist handling for improved user experience
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`.
2026-02-28 22:38:45 +03:00

79 lines
1.9 KiB
TypeScript

import { useLocaleRedirect } from '@composables/languages';
import { useNotification } from '@composables/notification';
import { useUserBaseData } from '@composables/user';
import { LOGIN } from '@graphql/mutations/auth';
import type { ILoginResponse } from '@types';
export function useLogin() {
const { t } = useI18n();
const userStore = useUserStore();
const localePath = useLocalePath();
const { $appHelpers } = useNuxtApp();
const { checkAndRedirect } = useLocaleRedirect();
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: '/',
});
const { mutate, loading, error } = useMutation<ILoginResponse>(LOGIN);
async function login(email: string, password: string, isStayLogin: boolean) {
const result = await mutate({
email,
password,
});
const authData = result?.data?.obtainJwtToken;
if (!authData) return;
if (isStayLogin && authData.refreshToken) {
cookieRefresh.value = authData.refreshToken;
}
userStore.setUser(authData.user);
cookieAccess.value = authData.accessToken;
navigateTo(localePath('/'));
useNotification({
message: t('popup.success.login'),
type: 'success',
});
if (authData.user.language !== cookieLocale.value) {
await checkAndRedirect(authData.user.language);
}
await useUserBaseData(authData.user.email);
}
watch(error, (err) => {
if (!err) return;
console.error('useLogin 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 {
loading,
login,
};
}