Files
stpweb/vue/src/router/index.js
T
adamfandClaude Opus 5.5 acf21c69c2
Test and deploy / test (push) Successful in 1m57s
Test and deploy / deploy (push) Successful in 1m42s
Let non-admin accounts be granted single services, starting with /receipt/
Access was admin or nothing, so letting a friend print receipts meant making
them a full admin. Users now carry a list of permissions (User.Permissions,
a JSON column), each a key from the registry in services/permissions.go;
admins pass every check. The only key so far is `receipt`.

GET /auth/validate/:permission answers like /auth/validate-admin (200/403/401,
with the same refresh-token fallback, now shared) plus 404 for an unknown key,
and reads the user row rather than the token, so a grant or revoke applies on
the next request. On 200 it returns the username in X-Auth-User.

nginx gets a permission gate beside the admin gate: a location sets
$auth_permission and auth_requests /internal/auth/validate. /receipt/ moves
onto it, forwarding X-Remote-User to the receipt server. 403 now goes to
/no-access rather than back to the login page. The variable must not be given
a server-level default: the subrequest re-runs the server rewrite phase and
would blank it.

The admin panel's Manage Users tab ticks permissions per user
(setUserPermissions, permissionTypes). The login page is no longer
admin-only in wording or destination: a non-admin lands on /account, which
lists the services they can open; /no-access is the same view with a notice.

/auth/validate-admin and every other admin-gated service are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-23 22:04:56 +01:00

137 lines
5.4 KiB
JavaScript

/**
* Route table and the single sign-in/admin route guard.
*
* Structure: two layout shells. `/` mounts DefaultLayout (navbar + footer, dark
* theme) and holds most routes; `/cv` mounts CVLayout (light print theme).
* `/stp2` is deliberately outside both — it is a bare full-bleed mock.
*
* Pages made in the browser (views/canvas/) have no entries here: the
* catch-all at the end looks the path up and renders the page if one exists.
*
* Only the landing page is statically imported; every other view is a dynamic
* import so Vite code-splits it into its own chunk.
*
* Route `name`s are public contracts (used by RouterLink/`router.push`) — don't
* rename them.
*/
import { createRouter, createWebHistory } from "vue-router";
import DefaultLayout from "@/layouts/DefaultLayout.vue";
import CVLayout from "@/layouts/CVLayout.vue";
import Landing from "@/views/landing/Landing.vue";
import { useAuthStore } from "@/stores/auth";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
component: DefaultLayout,
children: [
{
path: "",
name: "landing",
component: Landing,
},
{
path: "admin/login",
name: "admin-login",
component: () => import("@/views/admin/Login.vue"),
},
{
path: "admin",
name: "admin",
component: () => import("@/views/admin/Admin.vue"),
meta: { requiresAdmin: true },
},
// A signed-in user's own page: the gated services they have
// been granted. /no-access is the same page with a notice,
// where nginx's permission gate sends a refused visitor.
{
path: "account",
name: "account",
component: () => import("@/views/account/Account.vue"),
meta: { requiresSignIn: true },
},
{
path: "no-access",
name: "no-access",
component: () => import("@/views/account/Account.vue"),
meta: { requiresSignIn: true },
},
{
path: "automata",
name: "mobile-automata",
component: () =>
import("@/views/automata/MobileAutomata.vue"),
},
{
path: "hotels",
name: "cheap-hotels",
component: () => import("@/views/hotels/CheapHotels.vue"),
},
{
path: "architecture",
name: "architecture",
component: () =>
import("@/views/architecture/Architecture.vue"),
},
// /stp, /shrines and the shrines under it are canvas pages,
// made from the admin panel's Pages tab and served by the
// catch-all below. /stp's old route (views/home3/Home3.vue)
// and the hand-written shrines (views/home/shrines/) are
// unrouted.
// Anything else is an admin-created canvas page if one exists
// at that address (the Page GraphQL type), else the 404.
{
path: ":pathMatch(.*)*",
name: "404",
component: () => import("@/views/canvas/CanvasPage.vue"),
},
],
},
{
path: "/stp2",
name: "home2",
component: () => import("@/views/home2/Home2.vue"),
},
{
path: "/cv",
component: CVLayout,
children: [
{
path: "",
name: "cv",
component: () => import("@/views/CV/CV.vue"),
},
// /cv/jobs and /cv/leads moved to the job tracker app at
// /jobs/; nginx redirects the old paths.
],
},
],
});
// Guard for routes marked `meta: { requiresAdmin: true }` or
// `meta: { requiresSignIn: true }` (any account, admin or not).
//
// The admin flag arrives asynchronously, so navigating straight to /admin on a
// cold load would otherwise read an empty user and bounce a legitimate admin to
// the login page. The guard therefore awaits auth.ensureLoaded(), which issues
// a standalone `me` query unless the user is already known.
//
// It deliberately does not wait on the home query (stores/homeData.js): that
// one fans out to Spotify, Steam and Gitea, and a guard that waited on it hung
// forever when it failed. ensureLoaded() always settles — a failed `me` reads
// as "not signed in" — and `user` is re-read on every navigation, so a login or
// logout during the session is honoured.
router.beforeEach(async (to) => {
if (!to.meta.requiresAdmin && !to.meta.requiresSignIn) return;
const auth = useAuthStore();
await auth.ensureLoaded();
const allowed = to.meta.requiresAdmin ? auth.user.admin : auth.loggedIn;
if (!allowed)
return { path: "/admin/login", query: { redirect: to.fullPath } };
});
export default router;