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.
121 lines
2.8 KiB
TypeScript
121 lines
2.8 KiB
TypeScript
import { useLogout } from '@composables/auth';
|
|
import { useLocaleRedirect } from '@composables/languages';
|
|
import { useUserBaseData } from '@composables/user';
|
|
import { REFRESH } from '@graphql/mutations/auth';
|
|
|
|
export function useRefresh() {
|
|
const { t } = useI18n();
|
|
const router = useRouter();
|
|
const localePath = useLocalePath();
|
|
const userStore = useUserStore();
|
|
const { $appHelpers, $notify } = useNuxtApp();
|
|
|
|
const { checkAndRedirect } = useLocaleRedirect();
|
|
const { loadUserBaseData } = useUserBaseData();
|
|
const { logout } = useLogout();
|
|
|
|
const { mutate, loading, error } = useMutation(REFRESH);
|
|
|
|
function isTokenInvalidError(error: unknown): boolean {
|
|
if (isGraphQLError(error)) {
|
|
const message = error.graphQLErrors?.[0]?.message?.toLowerCase() || '';
|
|
return (
|
|
message.includes('invalid refresh token') ||
|
|
message.includes('blacklist') ||
|
|
message.includes('expired') ||
|
|
message.includes('revoked')
|
|
);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function refresh() {
|
|
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: '/',
|
|
});
|
|
|
|
if (!cookieRefresh.value) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const result = await mutate({
|
|
refreshToken: cookieRefresh.value,
|
|
});
|
|
const data = result?.data?.refreshJwtToken;
|
|
|
|
if (!data) {
|
|
return;
|
|
}
|
|
|
|
userStore.setUser(data.user);
|
|
cookieRefresh.value = data.refreshToken;
|
|
cookieAccess.value = data.accessToken;
|
|
|
|
if (data.user.language !== cookieLocale.value) {
|
|
await checkAndRedirect(data.user.language);
|
|
}
|
|
|
|
await loadUserBaseData(data.user.email);
|
|
} catch (err) {
|
|
if (isTokenInvalidError(err)) {
|
|
await logout();
|
|
await router.push(localePath('/'));
|
|
|
|
return;
|
|
}
|
|
|
|
let message = t('popup.errors.defaultError');
|
|
if (isGraphQLError(err)) {
|
|
message = err.graphQLErrors?.[0]?.message || message;
|
|
} else if (err instanceof Error) {
|
|
message = err.message;
|
|
} else if (typeof err === 'string') {
|
|
message = err;
|
|
}
|
|
|
|
$notify({
|
|
message,
|
|
type: 'error',
|
|
title: t('popup.errors.main'),
|
|
});
|
|
}
|
|
}
|
|
|
|
watch(error, async (err) => {
|
|
if (!err) return;
|
|
|
|
if (isTokenInvalidError(err)) {
|
|
await logout();
|
|
await router.push(localePath('/'));
|
|
return;
|
|
}
|
|
|
|
console.error('useRefresh 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 {
|
|
refresh,
|
|
loading,
|
|
};
|
|
}
|