Auth guard

This commit is contained in:
2024-03-13 23:33:29 +08:00
parent 5a966a42d7
commit f0e24f634b
2 changed files with 49 additions and 29 deletions

View File

@@ -1,4 +1,5 @@
import { createRouter, createWebHistory } from "vue-router"
import { useUserinfo } from "@/stores/userinfo"
import MasterLayout from "@/layouts/master.vue"
const router = createRouter({
@@ -7,18 +8,39 @@ const router = createRouter({
{
path: "/",
component: MasterLayout,
children: [
{ path: "/", name: "dashboard", component: () => import("@/views/dashboard.vue") },
],
children: [{ path: "/", name: "dashboard", component: () => import("@/views/dashboard.vue") }],
},
{
path: "/auth",
children: [
{ path: "sign-in", name: "auth.sign-in", component: () => import("@/views/auth/sign-in.vue") },
{ path: "sign-up", name: "auth.sign-up", component: () => import("@/views/auth/sign-up.vue") },
]
}
{
path: "sign-in",
name: "auth.sign-in",
component: () => import("@/views/auth/sign-in.vue"),
meta: { public: true },
},
{
path: "sign-up",
name: "auth.sign-up",
component: () => import("@/views/auth/sign-up.vue"),
meta: { public: true },
},
],
},
],
})
router.beforeEach(async (to, from, next) => {
const id = useUserinfo()
if (!id.isReady) {
await id.readProfiles()
}
if (!to.meta.public && !id.userinfo.isLoggedIn) {
next({ name: "auth.sign-in", query: { redirect_uri: to.fullPath } })
} else {
next()
}
})
export default router