antfu-collective / vitesse

🏕 Opinionated Vite + Vue Starter Template
https://vitesse.netlify.app/
MIT License
8.79k stars 932 forks source link

Module install order #538

Open bekjon-me opened 2 months ago

bekjon-me commented 2 months ago

Describe the bug

I have a guard file as a module

import type { UserModule } from "@/types";

// Setup Pinia
// https://pinia.vuejs.org/
export const install: UserModule = async ({ router, isClient }) => {
  if (isClient) {
    const authenticated = JSON.parse(localStorage.getItem("authenticated") ?? "");

    router.beforeEach(async (to, from, next) => {
      if (to.meta.requiresAuth && !authenticated) {
        next("/login");
      } else {
        next();
      }
    });
  }
};

but this guard working a bit late. I think the problem is it is being installed later. When I try to go private page for example /clients page and I am not authenticated, clients page being shown and then it goes to /login. I tried this in index.html and everything works properly.Here is what I wrote in index.html

<script>
      (function () {

        const authenticated = JSON.parse(localStorage.getItem("authenticated"));
        if (
          (window.location.pathname === "/leads" ||
            window.location.pathname === "/clients") &&
          !authenticated
        ) {
          window.location.pathname = "/login";
        }
      })();
    </script>

I left the website as a reproduction so you can check yourself

Reproduction

ucrm.atmu.uz

System Info

Shell
Windows

Used Package Manager

pnpm

Validations

Contributions