✨ Auth guard
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user