fix: adminPath reroute — move to hooks.ts universal hook, read config.json directly
This commit is contained in:
@@ -2,35 +2,9 @@ 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.
|
||||
* handle: no URL rewriting here (that's reroute in hooks.ts).
|
||||
* Kept as a placeholder for future server-only middleware.
|
||||
*/
|
||||
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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user