feat: redirection
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
DATABASE_URL=sqlite.db
|
||||
ADMIN_USERNAME=admin
|
||||
ADMIN_PASSWORD=strong_password
|
||||
REDIRECT_DOMAIN=pihka.al
|
||||
|
||||
NUXT_SESSION_PASSWORD=strong_password
|
||||
|
||||
@@ -10,10 +10,6 @@ export default defineNuxtConfig({
|
||||
}
|
||||
},
|
||||
|
||||
routeRules: {
|
||||
'/': { redirect: '/dashboard' },
|
||||
},
|
||||
|
||||
devtools: {
|
||||
enabled: true
|
||||
},
|
||||
|
||||
@@ -5,6 +5,7 @@ const schema = z.object({
|
||||
DATABASE_URL: z.string().min(1),
|
||||
ADMIN_USERNAME: z.string().min(1).default("admin"),
|
||||
ADMIN_PASSWORD: z.string().min(1),
|
||||
REDIRECT_DOMAIN: z.string().min(1),
|
||||
})
|
||||
|
||||
export const env = schema.parse(process.env)
|
||||
|
||||
31
server/middleware/redirect.ts
Normal file
31
server/middleware/redirect.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { db } from "#server/db";
|
||||
import * as tables from "#server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { env } from "#server/env";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const host = getRequestHost(event, { xForwardedHost: true }).split(":")[0];
|
||||
|
||||
if (host !== env.REDIRECT_DOMAIN) {
|
||||
if (getRequestURL(event).pathname === "/") {
|
||||
return sendRedirect(event, "/dashboard", 302);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const path = getRequestURL(event).pathname;
|
||||
|
||||
const link = await db.query.links.findFirst({
|
||||
where: eq(tables.links.path, path),
|
||||
});
|
||||
|
||||
if (!link) {
|
||||
throw createError({ statusCode: 404, message: "Not found" });
|
||||
}
|
||||
|
||||
if (link.disabled) {
|
||||
throw createError({ statusCode: 410, message: "This link has been disabled" });
|
||||
}
|
||||
|
||||
return sendRedirect(event, link.url, 302);
|
||||
});
|
||||
Reference in New Issue
Block a user