schon/storefront/composables/auth/useLogin.ts
Alexandr SaVBaD Waltz 761fecf67f Features: 1) Add useWishlistOverwrite composable for wishlist mutations, including adding, removing, and bulk actions; 2) Introduce new localized UI texts for cart and wishlist operations; 3) Enhance filtering logic with parseAttributesString and route query synchronization;
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.
2025-07-06 19:49:26 +03:00

96 lines
No EOL
2.4 KiB
TypeScript

import { LOGIN } from '~/graphql/mutations/auth';
import type { ILoginResponse } from '~/types/api/auth';
import { isGraphQLError } from '~/utils/error';
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 { useAppStore } from '~/stores/app';
import {DEFAULT_LOCALE} from "~/config/constants";
import {useNotification} from "~/composables/notification";
export function useLogin() {
const { t } = useI18n();
const userStore = useUserStore();
const appStore = useAppStore();
const { COOKIES_LOCALE_KEY, COOKIES_REFRESH_TOKEN_KEY, COOKIES_ACCESS_TOKEN_KEY } = useAppConfig();
const { checkAndRedirect } = useLocaleRedirect();
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: '/'
}
);
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
useNotification(
t('popup.success.login'),
'success'
);
if (authData.user.language !== cookieLocale.value) {
await checkAndRedirect(authData.user.language);
}
await useWishlist();
await usePendingOrder(authData.user.email);
appStore.unsetActiveState();
}
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,
'error',
t('popup.errors.main')
);
});
return {
loading,
login
};
}