schon/storefront/app/composables/auth/useLogin.ts
Alexandr SaVBaD Waltz c36135d78d feat(storefront): add wishlist and guest cart support with cookie persistence
Enhancements:
- Introduced `wishlist.vue` for displaying and managing the wishlist.
- Added guest cart and wishlist handling via cookies for unauthenticated users.
- Implemented synchronization logic for wishlist and cart (`useOrderSync` and `useWishlistSync`) upon user login.
- Updated `cart.vue` layout with a bulk 'add all to cart' button for wishlist items.
- Enhanced `post.vue` prop handling for improved type safety.

Fixes:
- Fixed breadcrumbs console log removal in `useBreadcrumbs.ts`.
- Corrected and unified translations in `en-gb.json` for cart and wishlist descriptions.
- Fixed stale routes in footer (`terms-and-condition` -> `terms-and-conditions`, etc.).

Extras:
- Refactored composables `useWishlistOverwrite` and `useOrderOverwrite` for cookie-based fallback.
- Applied code styling improvements, organized imports, and optimized API requests in Apollo plugin.
2026-02-28 17:39:17 +03:00

82 lines
2 KiB
TypeScript

import { DEFAULT_LOCALE } from '@appConstants';
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: () => 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;
await nextTick();
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,
};
}