feat: adminPath in config.json now drives actual route rewriting via server hook
This commit is contained in:
BIN
data/.config.json.swp
Normal file
BIN
data/.config.json.swp
Normal file
Binary file not shown.
@@ -71,6 +71,22 @@
|
|||||||
"symbol": "mfcktn",
|
"symbol": "mfcktn",
|
||||||
"group": "dickloads",
|
"group": "dickloads",
|
||||||
"toBase": 170.2399999998
|
"toBase": 170.2399999998
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "shitload",
|
||||||
|
"label": "Shitload",
|
||||||
|
"labelPlural": "Shitloads",
|
||||||
|
"symbol": "shtld",
|
||||||
|
"group": "dickloads",
|
||||||
|
"toBase": 12.6666666667
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "shit-ton",
|
||||||
|
"label": "Shit ton",
|
||||||
|
"labelPlural": "Shit tons",
|
||||||
|
"symbol": "shttn",
|
||||||
|
"group": "dickloads",
|
||||||
|
"toBase": 25.3333333333
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"groups": [
|
"groups": [
|
||||||
|
|||||||
36
src/hooks.server.ts
Normal file
36
src/hooks.server.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { loadConfig } from '$lib/server/data';
|
||||||
|
import type { Handle } from '@sveltejs/kit';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrites requests from the configured adminPath to the internal /admin route.
|
||||||
|
*
|
||||||
|
* Example: if adminPath = "secret-panel", then:
|
||||||
|
* /secret-panel → /admin
|
||||||
|
* /secret-panel/login → /admin/login
|
||||||
|
* /secret-panel/api/units → /admin/api/units
|
||||||
|
*
|
||||||
|
* If adminPath = "admin" (default), this is a no-op.
|
||||||
|
*/
|
||||||
|
export const handle: Handle = async ({ event, resolve }) => {
|
||||||
|
const config = loadConfig();
|
||||||
|
const adminPath = (config.adminPath ?? 'admin').replace(/^\/|\/$/g, '');
|
||||||
|
|
||||||
|
if (adminPath !== 'admin') {
|
||||||
|
const url = event.url;
|
||||||
|
const prefix = `/${adminPath}`;
|
||||||
|
|
||||||
|
if (url.pathname === prefix || url.pathname.startsWith(prefix + '/')) {
|
||||||
|
// Rewrite the URL to use /admin internally
|
||||||
|
const rewritten = '/admin' + url.pathname.slice(prefix.length);
|
||||||
|
event.url = new URL(rewritten + url.search + url.hash, url.origin);
|
||||||
|
// Also update request.url so auth helpers see the rewritten path
|
||||||
|
Object.defineProperty(event.request, 'url', {
|
||||||
|
value: event.url.toString(),
|
||||||
|
writable: false,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolve(event);
|
||||||
|
};
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { redirect } from '@sveltejs/kit';
|
import { redirect } from '@sveltejs/kit';
|
||||||
import { authRequest } from '$lib/server/auth';
|
import { authRequest } from '$lib/server/auth';
|
||||||
|
import { loadConfig } from '$lib/server/data';
|
||||||
import type { LayoutServerLoad } from './$types';
|
import type { LayoutServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: LayoutServerLoad = ({ request, url }) => {
|
export const load: LayoutServerLoad = ({ request, url }) => {
|
||||||
@@ -14,7 +15,9 @@ export const load: LayoutServerLoad = ({ request, url }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!authRequest(request)) {
|
if (!authRequest(request)) {
|
||||||
throw redirect(302, '/admin/login');
|
// Redirect to login using the configured admin path so the URL stays consistent
|
||||||
|
const adminPath = (loadConfig().adminPath ?? 'admin').replace(/^\/|\/$/g, '');
|
||||||
|
throw redirect(302, `/${adminPath}/login`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { authenticated: true };
|
return { authenticated: true };
|
||||||
|
|||||||
Reference in New Issue
Block a user