mirror of
https://origin.cursor.com/mrdevmx/panels.git
synced 2026-10-09 12:43:18 +00:00
37 lines
1 KiB
TypeScript
37 lines
1 KiB
TypeScript
export default defineNuxtRouteMiddleware(async (to) => {
|
|
if (to.path === "/login") return;
|
|
const { user, fetchMe } = useAuth();
|
|
const { api } = useApi();
|
|
try {
|
|
if (!user.value) {
|
|
await Promise.race([
|
|
fetchMe(),
|
|
new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), 4000)),
|
|
]);
|
|
}
|
|
} catch {
|
|
user.value = null;
|
|
}
|
|
if (!user.value) return navigateTo("/login");
|
|
|
|
if (user.value.role === "platform_admin" && user.value.realm === "platform") {
|
|
user.value = null;
|
|
try {
|
|
await api("/v1/auth/logout", { method: "POST" });
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
if (import.meta.client) {
|
|
window.location.href = "http://localhost:3001/login";
|
|
return abortNavigation();
|
|
}
|
|
return navigateTo("/login");
|
|
}
|
|
|
|
if (user.value.must_change_password && to.path !== "/cambiar-password") {
|
|
return navigateTo("/cambiar-password");
|
|
}
|
|
if (!user.value.must_change_password && to.path === "/cambiar-password") {
|
|
return navigateTo("/");
|
|
}
|
|
});
|