Skip to content

Commit

Permalink
feat: add astro middleware route blocker (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsp45 authored Apr 4, 2024
1 parent 9a43b56 commit 650bb66
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ SES_REGION=<REGION OF YOU SES CONFIG>
SMTP_USER=<YOUR SMTP USER>
SMTP_PASS=<YOUR SMTP PASSWORD>
SMTP_HOST=<YOUR SMTP HOST>
BLOCKED_URLS=<BLOCKED URL's SEPARATED BY COMMAS>
12 changes: 3 additions & 9 deletions src/components/header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ const headerClass = sticky
))
}
<li>
<a
href="/register"
<button
type="button"
class="rounded-full px-2.5 py-1 text-xs font-semibold text-white shadow-sm ring-1 ring-inset ring-white hover:ring-secondary"
>Register</a
class="rounded-full px-2.5 py-1 text-xs font-semibold text-white shadow-sm ring-1 ring-inset ring-white cursor-not-allowed"
>Register</button
>
</li>
</ul>
Expand Down Expand Up @@ -72,11 +71,6 @@ const headerClass = sticky
</a>
<nav>
<ul class="flex flex-col">
<li>
<a class="block py-4 text-center text-xl" href="/register">
Register
</a>
</li>
{
navItems.map(({ title, url }) => (
<li>
Expand Down
19 changes: 19 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const host_url = import.meta.env.HOST_URL;
const blocked_urls = import.meta.env.BLOCKED_URLS.split(",");

export function onRequest({ locals, request }, next) {
for (const blockedPath of blocked_urls) {
let path = host_url + blockedPath;
if (normalizePath(request.url) === path) {
return Response.redirect(`${host_url}/404`, 302);
}
}

return next();
}

// Utility function to remove trailing slash
function normalizePath(path) {
// This removes the trailing slash if it exists
return path.endsWith("/") ? path.slice(0, -1) : path;
}

0 comments on commit 650bb66

Please sign in to comment.