Fixes: None; Extra: 1) Create Pinia stores for app, user, category, and company management; 2) Add utility functions for error handling and category slug lookups; 3) Include German locale file and robots.txt for improved SEO and accessibility; 4) Add SVG assets and improve general folder structure for better maintainability.
92 lines
No EOL
2.4 KiB
TypeScript
92 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";
|
|
|
|
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
|
|
|
|
ElNotification({ message: t('popup.success.login'), type: '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;
|
|
}
|
|
ElNotification({
|
|
title: t('popup.errors.main'),
|
|
message,
|
|
type: 'error'
|
|
});
|
|
});
|
|
|
|
return {
|
|
loading,
|
|
login
|
|
};
|
|
} |