|
@@ -43,7 +43,6 @@ router.beforeEach(async (to) => {
|
|
|
const { useAuthStore } = await import("@/stores/auth");
|
|
const { useAuthStore } = await import("@/stores/auth");
|
|
|
const authStore = useAuthStore();
|
|
const authStore = useAuthStore();
|
|
|
|
|
|
|
|
- // 1. Handle Language Prefix
|
|
|
|
|
let lang = to.params.lang as string;
|
|
let lang = to.params.lang as string;
|
|
|
const savedLang = localStorage.getItem('locale') || 'en';
|
|
const savedLang = localStorage.getItem('locale') || 'en';
|
|
|
|
|
|
|
@@ -51,34 +50,26 @@ router.beforeEach(async (to) => {
|
|
|
const segments = to.path.split('/').filter(Boolean);
|
|
const segments = to.path.split('/').filter(Boolean);
|
|
|
const firstSegment = segments[0];
|
|
const firstSegment = segments[0];
|
|
|
|
|
|
|
|
- // Check if first segment looks like a language code but isn't supported
|
|
|
|
|
if (firstSegment && firstSegment.length === 2 && !supportedLangs.includes(firstSegment)) {
|
|
if (firstSegment && firstSegment.length === 2 && !supportedLangs.includes(firstSegment)) {
|
|
|
- // It's a fake lang like /fr/auth -> redirect to /ru/auth
|
|
|
|
|
const remainingPath = segments.slice(1).join('/');
|
|
const remainingPath = segments.slice(1).join('/');
|
|
|
return { path: `/${savedLang}/${remainingPath}`, query: to.query, hash: to.hash };
|
|
return { path: `/${savedLang}/${remainingPath}`, query: to.query, hash: to.hash };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Skip redirect during prerendering to allow capturing the root page or specific routes
|
|
|
|
|
if ((window as any).__PRERENDER_INJECTED) {
|
|
if ((window as any).__PRERENDER_INJECTED) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Simple path like /auth -> /ru/auth
|
|
|
|
|
return { path: `/${savedLang}${to.fullPath}` };
|
|
return { path: `/${savedLang}${to.fullPath}` };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 2. Sync i18n
|
|
|
|
|
if (supportedLangs.includes(lang)) {
|
|
if (supportedLangs.includes(lang)) {
|
|
|
if (i18n.global.locale.value !== lang) {
|
|
if (i18n.global.locale.value !== lang) {
|
|
|
await setLanguage(lang);
|
|
await setLanguage(lang);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- // This part should technically not be reached with the current regex
|
|
|
|
|
- // but as a safety measure:
|
|
|
|
|
return { path: `/${savedLang}/`, query: to.query };
|
|
return { path: `/${savedLang}/`, query: to.query };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 3. Auth Guards
|
|
|
|
|
const isPrerendering = (window as any).__PRERENDER_INJECTED !== undefined;
|
|
const isPrerendering = (window as any).__PRERENDER_INJECTED !== undefined;
|
|
|
if (!isPrerendering && !authStore.user && localStorage.getItem("token")) {
|
|
if (!isPrerendering && !authStore.user && localStorage.getItem("token")) {
|
|
|
await authStore.init();
|
|
await authStore.init();
|
|
@@ -95,5 +86,4 @@ router.beforeEach(async (to) => {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-
|
|
|
|
|
export default router;
|
|
export default router;
|