2025-10-06 18:38:16 +03:00

425 lines
11 KiB
JavaScript

import { createRouter, createWebHistory } from "vue-router";
import Default from "../views/dashboards/Default.vue";
import Automotive from "../views/dashboards/Automotive.vue";
import SmartHome from "../views/dashboards/SmartHome.vue";
import VRDefault from "../views/dashboards/vr/VRDefault.vue";
import VRInfo from "../views/dashboards/vr/VRInfo.vue";
import CRM from "../views/dashboards/CRM.vue";
import Overview from "../views/pages/profile/Overview.vue";
import Teams from "../views/pages/profile/Teams.vue";
import Projects from "../views/pages/profile/Projects.vue";
import General from "../views/pages/projects/General.vue";
import Timeline from "../views/pages/projects/Timeline.vue";
import NewProject from "../views/pages/projects/NewProject.vue";
import Pricing from "../views/pages/Pricing.vue";
import RTL from "../views/pages/Rtl.vue";
import Charts from "../views/pages/Charts.vue";
import SweetAlerts from "../views/pages/SweetAlerts.vue";
import Notifications from "../views/pages/Notifications.vue";
import Kanban from "../views/applications/Kanban.vue";
import Wizard from "../views/applications/wizard/Wizard.vue";
import DataTables from "../views/applications/DataTables.vue";
import Calendar from "../views/applications/Calendar.vue";
import Analytics from "../views/applications/analytics/Analytics.vue";
import EcommerceOverview from "../views/ecommerce/overview/Overview.vue";
import NewProduct from "../views/ecommerce/products/NewProduct.vue";
import EditProduct from "../views/ecommerce/EditProduct.vue";
import ProductPage from "../views/ecommerce/ProductPage.vue";
import ProductsList from "../views/ecommerce/ProductsList.vue";
import OrderDetails from "../views/ecommerce/Orders/OrderDetails";
import OrderList from "../views/ecommerce/Orders/OrderList";
import Referral from "../views/ecommerce/Referral";
import Reports from "../views/pages/Users/Reports.vue";
import NewUser from "../views/pages/Users/NewUser.vue";
import Settings from "../views/pages/Account/Settings.vue";
import Billing from "../views/pages/Account/Billing.vue";
import Invoice from "../views/pages/Account/Invoice.vue";
import Security from "../views/pages/Account/Security.vue";
import Widgets from "../views/pages/Widgets.vue";
import Basic from "../views/auth/signin/Basic.vue";
import Cover from "../views/auth/signin/Cover.vue";
import Illustration from "../views/auth/signin/Illustration.vue";
import ResetBasic from "../views/auth/reset/Basic.vue";
import ResetCover from "../views/auth/reset/Cover.vue";
import ResetIllustration from "../views/auth/reset/Illustration.vue";
import VerificationBasic from "../views/auth/verification/Basic.vue";
import VerificationCover from "../views/auth/verification/Cover.vue";
import VerificationIllustration from "../views/auth/verification/Illustration.vue";
import SignupBasic from "../views/auth/signup/Basic.vue";
import SignupCover from "../views/auth/signup/Cover.vue";
import SignupIllustration from "../views/auth/signup/Illustration.vue";
import Error404 from "../views/auth/error/Error404.vue";
import Error500 from "../views/auth/error/Error500.vue";
import lockBasic from "../views/auth/lock/Basic.vue";
import lockCover from "../views/auth/lock/Cover.vue";
import lockIllustration from "../views/auth/lock/Illustration.vue";
//ROUTE SHOULD USED
const routes = [
{
path: "/",
name: "/",
redirect: "/dashboards/dashboard-default",
},
{
path: "/dashboard",
redirect: "/dashboards/dashboard-default",
},
{
path: "/login",
name: "Login",
component: () => import("@/views/pages/Login.vue"),
meta: { public: true, guestOnly: true, guestLayout: true },
},
{
path: "/register",
name: "Register",
component: () => import("@/views/pages/Register.vue"),
meta: { public: true, guestOnly: true, guestLayout: true },
},
{
path: "/dashboards/dashboard-default",
name: "Default",
component: Default,
},
{
path: "/dashboards/automotive",
name: "Automotive",
component: Automotive,
},
{
path: "/dashboards/smart-home",
name: "Smart Home",
component: SmartHome,
},
{
path: "/dashboards/vr/vr-default",
name: "VR Default",
component: VRDefault,
},
{
path: "/dashboards/vr/vr-info",
name: "VR Info",
component: VRInfo,
},
{
path: "/dashboards/crm",
name: "CRM",
component: CRM,
},
{
path: "/pages/profile/overview",
name: "Profile Overview",
component: Overview,
},
{
path: "/pages/profile/teams",
name: "Teams",
component: Teams,
},
{
path: "/pages/profile/projects",
name: "All Projects",
component: Projects,
},
{
path: "/pages/projects/general",
name: "General",
component: General,
},
{
path: "/pages/projects/timeline",
name: "Timeline",
component: Timeline,
},
{
path: "/pages/projects/new-project",
name: "New Project",
component: NewProject,
},
{
path: "/pages/pricing-page",
name: "Pricing Page",
component: Pricing,
},
{
path: "/pages/rtl-page",
name: "RTL",
component: RTL,
},
{
path: "/pages/charts",
name: "Charts",
component: Charts,
},
{
path: "/pages/widgets",
name: "Widgets",
component: Widgets,
},
{
path: "/pages/sweet-alerts",
name: "Sweet Alerts",
component: SweetAlerts,
},
{
path: "/pages/notifications",
name: "Notifications",
component: Notifications,
},
{
path: "/applications/kanban",
name: "Kanban",
component: Kanban,
},
{
path: "/applications/wizard",
name: "Wizard",
component: Wizard,
},
{
path: "/applications/data-tables",
name: "Data Tables",
component: DataTables,
},
{
path: "/applications/calendar",
name: "Calendar",
component: Calendar,
},
{
path: "/applications/analytics",
name: "Analytics",
component: Analytics,
},
{
path: "/ecommerce/overview",
name: "Overview",
component: EcommerceOverview,
},
{
path: "/ecommerce/products/new-product",
name: "New Product",
component: NewProduct,
},
{
path: "/ecommerce/products/edit-product",
name: "Edit Product",
component: EditProduct,
},
{
path: "/ecommerce/products/product-page",
name: "Product Page",
component: ProductPage,
},
{
path: "/ecommerce/products/products-list",
name: "Products List",
component: ProductsList,
},
{
path: "/ecommerce/Orders/order-details",
name: "Order Details",
component: OrderDetails,
},
{
path: "/ecommerce/Orders/order-list",
name: "Order List",
component: OrderList,
},
{
path: "/ecommerce/referral",
name: "Referral",
component: Referral,
},
{
path: "/pages/users/reports",
name: "Reports",
component: Reports,
},
{
path: "/pages/users/new-user",
name: "New User",
component: NewUser,
},
{
path: "/pages/account/settings",
name: "Settings",
component: Settings,
},
{
path: "/pages/account/billing",
name: "Billing",
component: Billing,
},
{
path: "/pages/account/invoice",
name: "Invoice",
component: Invoice,
},
{
path: "/pages/account/Security",
name: "Security",
component: Security,
},
{
path: "/authentication/signin/basic",
name: "Signin Basic",
component: Basic,
meta: { guestLayout: true },
},
{
path: "/authentication/signin/cover",
name: "Signin Cover",
component: Cover,
meta: { guestLayout: true },
},
{
path: "/authentication/signin/illustration",
name: "Signin Illustration",
component: Illustration,
meta: { guestLayout: true },
},
{
path: "/authentication/reset/basic",
name: "Reset Basic",
component: ResetBasic,
meta: { guestLayout: true },
},
{
path: "/authentication/reset/cover",
name: "Reset Cover",
component: ResetCover,
meta: { guestLayout: true },
},
{
path: "/authentication/reset/illustration",
name: "Reset Illustration",
component: ResetIllustration,
meta: { guestLayout: true },
},
{
path: "/authentication/lock/basic",
name: "Lock Basic",
component: lockBasic,
meta: { guestLayout: true },
},
{
path: "/authentication/lock/cover",
name: "Lock Cover",
component: lockCover,
meta: { guestLayout: true },
},
{
path: "/authentication/lock/illustration",
name: "Lock Illustration",
component: lockIllustration,
meta: { guestLayout: true },
},
{
path: "/authentication/verification/basic",
name: "Verification Basic",
component: VerificationBasic,
meta: { guestLayout: true },
},
{
path: "/authentication/verification/cover",
name: "Verification Cover",
component: VerificationCover,
meta: { guestLayout: true },
},
{
path: "/authentication/verification/illustration",
name: "Verification Illustration",
component: VerificationIllustration,
meta: { guestLayout: true },
},
{
path: "/authentication/signup/basic",
name: "Signup Basic",
component: SignupBasic,
meta: { guestLayout: true },
},
{
path: "/authentication/signup/cover",
name: "Signup Cover",
component: SignupCover,
meta: { guestLayout: true },
},
{
path: "/authentication/signup/illustration",
name: "Signup Illustration",
component: SignupIllustration,
meta: { guestLayout: true },
},
{
path: "/authentication/error/error404",
name: "Error Error404",
component: Error404,
meta: { guestLayout: true },
},
{
path: "/authentication/error/error500",
name: "Error Error500",
component: Error500,
meta: { guestLayout: true },
},
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
linkActiveClass: "active",
});
// Auth guard using Pinia store and Laravel Sanctum session
import { useAuthStore } from "@/stores/auth";
import pinia from "@/plugins/pinia";
const DASHBOARD = "/dashboards/dashboard-default";
const LOGIN = "/login"; // canonical login path without redirect query
router.beforeEach(async (to, from, next) => {
const auth = useAuthStore(pinia);
const isPublic = to.matched.some((r) => r.meta && r.meta.public === true);
const isGuestOnly = to.matched.some(
(r) => r.meta && r.meta.guestOnly === true
);
const isLoginRoute = to.name === "Login" || to.name === "Signin";
// Initialize auth from localStorage token if not already done
if (!auth.checked) {
const token = localStorage.getItem("auth_token");
if (token) {
// Token exists, set it and try to fetch user
auth.token = token;
try {
await auth.fetchMe();
} catch (error) {
// If token is invalid, clear it and continue as unauthenticated
console.warn("Invalid token, clearing auth");
localStorage.removeItem("auth_token");
auth.token = null;
auth.user = null;
auth.checked = true;
}
} else {
// No token, mark as checked
auth.checked = true;
}
}
if (auth.isAuthenticated) {
// Prevent authenticated users from visiting guest-only routes (like sign-in)
if (isGuestOnly || isLoginRoute) return next(DASHBOARD);
return next();
}
// Not authenticated: allow public/guest routes (like sign-in), otherwise send to login (no redirect param)
if (isPublic || isGuestOnly || isLoginRoute) return next();
return next({ path: LOGIN, replace: true });
});
export default router;