Files
agologum/client/src/router/index.ts
Blitblank 661bb03d1d
Some checks failed
Build and Deploy Frontend / build-and-deploy (push) Failing after 4s
Build and Deploy API / build-and-deploy (push) Successful in 12s
preliminary frontend for ther auth api
2026-03-17 22:30:59 -05:00

50 lines
1.5 KiB
TypeScript

// the router creates front-end endpoints and serves pages to them
import { createRouter, createWebHistory } from "vue-router";
import LoginForm from "../pages/LoginForm.vue";
import RegisterForm from "../pages/RegisterForm.vue";
import UsersList from "../pages/UsersList.vue";
import UserForm from "../pages/UserForm.vue";
import index from "../pages/index.vue";
// link path to the page component
const routes = [
{ path: "/", component: index },
{ path: "/login", component: LoginForm },
{ path: "/register", component: RegisterForm },
{ path: "/users", component: UsersList },
{ path: "/user/new", component: UserForm, meta: { requiresAuth: true } },
{ path: "/user/:id", component: UserForm, meta: { requiresAuth: true } }
]; // I really like this
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: routes,
});
// intercept before routing
router.beforeEach((to, from, next) => {
const token = localStorage.getItem("token");
if(to.meta.requiresAuth && !token) { // if the page requires use to be signed in, they must have at least a token set
next("/login");
} else {
next();
}
// TODO: if they have a token, but invalid, it will still send them to the page (the api will catch non-authorized though)
// maybe have a "validate token" from the api and refresh it if valid
/*
} else {
bool authorizedUser = authApi.refreshToken(token);
if(authorizedUser) {
next();
} else {
next("/login");
}
}
*/
});
export default router;