✨ Auth guard
This commit is contained in:
parent
5a966a42d7
commit
f0e24f634b
@ -1,4 +1,5 @@
|
|||||||
import { createRouter, createWebHistory } from "vue-router"
|
import { createRouter, createWebHistory } from "vue-router"
|
||||||
|
import { useUserinfo } from "@/stores/userinfo"
|
||||||
import MasterLayout from "@/layouts/master.vue"
|
import MasterLayout from "@/layouts/master.vue"
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
@ -7,18 +8,39 @@ const router = createRouter({
|
|||||||
{
|
{
|
||||||
path: "/",
|
path: "/",
|
||||||
component: MasterLayout,
|
component: MasterLayout,
|
||||||
children: [
|
children: [{ path: "/", name: "dashboard", component: () => import("@/views/dashboard.vue") }],
|
||||||
{ path: "/", name: "dashboard", component: () => import("@/views/dashboard.vue") },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/auth",
|
path: "/auth",
|
||||||
children: [
|
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
|
export default router
|
||||||
|
@ -4,17 +4,15 @@ import { ref } from "vue"
|
|||||||
import { request } from "@/scripts/request"
|
import { request } from "@/scripts/request"
|
||||||
|
|
||||||
export interface Userinfo {
|
export interface Userinfo {
|
||||||
isReady: boolean
|
|
||||||
isLoggedIn: boolean
|
isLoggedIn: boolean
|
||||||
displayName: string
|
displayName: string
|
||||||
data: any
|
data: any
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultUserinfo: Userinfo = {
|
const defaultUserinfo: Userinfo = {
|
||||||
isReady: false,
|
|
||||||
isLoggedIn: false,
|
isLoggedIn: false,
|
||||||
displayName: "Citizen",
|
displayName: "Citizen",
|
||||||
data: null
|
data: null,
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAtk(): string {
|
export function getAtk(): string {
|
||||||
@ -31,25 +29,25 @@ export const useUserinfo = defineStore("userinfo", () => {
|
|||||||
|
|
||||||
async function readProfiles() {
|
async function readProfiles() {
|
||||||
if (!checkLoggedIn()) {
|
if (!checkLoggedIn()) {
|
||||||
isReady.value = true;
|
isReady.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await request("/api/users/me", {
|
const res = await request("/api/users/me", {
|
||||||
headers: { "Authorization": `Bearer ${getAtk()}` }
|
headers: { Authorization: `Bearer ${getAtk()}` },
|
||||||
});
|
})
|
||||||
|
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json()
|
||||||
|
|
||||||
|
isReady.value = true
|
||||||
userinfo.value = {
|
userinfo.value = {
|
||||||
isReady: true,
|
|
||||||
isLoggedIn: true,
|
isLoggedIn: true,
|
||||||
displayName: data["nick"],
|
displayName: data["nick"],
|
||||||
data: data
|
data: data,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { userinfo, isReady, readProfiles }
|
return { userinfo, isReady, readProfiles }
|
||||||
|
Loading…
Reference in New Issue
Block a user