Added a global `notify` method via Nuxt plugin to replace `useNotification`. Improved messaging structure by embedding progress bars and handled dynamic durations. Updated usage across composables and components for consistency. - Replaced `useNotification` with `$notify` in all applicable files. - Updated `app.config.ts` to support customizable notification positions. - Refactored affected composables for simplified notification calls. - Enhanced progress indicator display within notifications. Breaking Changes: `useNotification` is removed, requiring migration to the new `$notify` API.
79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import { useLocaleRedirect } from '@composables/languages';
|
|
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, $notify } = useNuxtApp();
|
|
|
|
const { checkAndRedirect } = useLocaleRedirect();
|
|
const { loadUserBaseData } = useUserBaseData();
|
|
|
|
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 } = useMutation<ILoginResponse>(LOGIN);
|
|
|
|
|
|
async function login(email: string, password: string, isStayLogin: boolean) {
|
|
try {
|
|
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('/'));
|
|
|
|
$notify({
|
|
message: t('popup.success.login'),
|
|
type: 'success',
|
|
});
|
|
|
|
if (authData.user.language !== cookieLocale.value) {
|
|
await checkAndRedirect(authData.user.language);
|
|
}
|
|
|
|
await loadUserBaseData(authData.user.email);
|
|
} catch (err: unknown) {
|
|
console.error('useLogin error:', err);
|
|
let message = t('popup.errors.defaultError');
|
|
if (isGraphQLError(err)) {
|
|
message = err.graphQLErrors?.[0]?.message || message;
|
|
} else {
|
|
message = err.message;
|
|
}
|
|
$notify({
|
|
message,
|
|
type: 'error',
|
|
title: t('popup.errors.main'),
|
|
});
|
|
}
|
|
}
|
|
|
|
return {
|
|
loading,
|
|
login,
|
|
};
|
|
}
|