feat: api
This commit is contained in:
@@ -6,6 +6,7 @@ import Course from "@/pages/course.vue";
|
||||
import Index from "@/pages/index.vue";
|
||||
import Introduction from "@/pages/introduction.vue";
|
||||
import Login from "@/pages/login.vue";
|
||||
import Register from "@/pages/register.vue";
|
||||
import SelfAssessment from "@/pages/self-assessment.vue";
|
||||
import { useAppStore } from "@/stores/app";
|
||||
|
||||
@@ -31,6 +32,12 @@ const router = createRouter({
|
||||
name: "login",
|
||||
component: Login,
|
||||
},
|
||||
{
|
||||
path: "/register",
|
||||
name: "register",
|
||||
component: Register,
|
||||
meta: { title: "註冊|運動玩轉健康力" },
|
||||
},
|
||||
{
|
||||
path: "/self-assessment",
|
||||
name: "self-assessment",
|
||||
@@ -68,7 +75,10 @@ router.beforeEach((to) => {
|
||||
const store = useAppStore();
|
||||
|
||||
if (to.meta.requiresAuth && !store.isLoggedIn) {
|
||||
return { name: "login" };
|
||||
return {
|
||||
name: "login",
|
||||
query: { redirect: to.fullPath },
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -77,4 +87,20 @@ router.afterEach((to) => {
|
||||
typeof to.meta.title === "string" ? to.meta.title : "運動玩轉健康力";
|
||||
});
|
||||
|
||||
// 共用 HTTP client 收到登入後 API 的 401 時,清除前端狀態並保留原頁面供登入後返回。
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("auth-expired", () => {
|
||||
const store = useAppStore();
|
||||
const currentRoute = router.currentRoute.value;
|
||||
store.clearAuthentication();
|
||||
|
||||
if (currentRoute.name !== "login") {
|
||||
void router.replace({
|
||||
name: "login",
|
||||
query: { redirect: currentRoute.fullPath },
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user