Skip to content

Commit

Permalink
Merge pull request #5 from kcoderhtml/dev
Browse files Browse the repository at this point in the history
feat: fix middleware
  • Loading branch information
taciturnaxolotl authored May 8, 2024
2 parents 3e853a1 + a798c93 commit 472252c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 22 deletions.
10 changes: 4 additions & 6 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import db from "@astrojs/db";

// https://astro.build/config
export default defineConfig({
output: "server",
adapter: netlify({
edgeMiddleware: true
}),
integrations: [tailwind(), playformCompress(), db()]
});
output: "server",
adapter: netlify(),
integrations: [tailwind(), playformCompress(), db()],
});
4 changes: 2 additions & 2 deletions src/pages/api/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function generateSecret() {
return secret;
}

export const POST: APIRoute = async ({ params, request }) => {
export const POST: APIRoute = async ({ request }) => {
const aj = arcjet({
key: process.env.ARCJET_KEY!,
rules: [
Expand Down Expand Up @@ -94,7 +94,7 @@ export const POST: APIRoute = async ({ params, request }) => {
}
}

export const GET: APIRoute = async ({ params }) => {
export const GET: APIRoute = async () => {
return new Response(JSON.stringify({ ok: true, message: "Hello from the subscribe API" }), {
status: 200,
headers: {
Expand Down
1 change: 0 additions & 1 deletion src/pages/dashboard.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
import { date } from "astro/zod";
import Layout from "../layouts/Layout.astro";
import { db, Subscribers } from "astro:db";
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import Layout from "../layouts/Layout.astro";
import { createSession, gracePeriod, getSession } from "../utils/auth/auth";
import { createSession, getSession, gracePeriod } from "../utils/auth/auth";
import type { Session } from "../utils/auth/auth";
let session: Session | null = null;
Expand Down
14 changes: 2 additions & 12 deletions src/utils/auth/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineMiddleware } from "astro:middleware";
import type { DecodeResult, ExpirationStatus, Session } from "./auth";
import type { DecodeResult, ExpirationStatus } from "./auth";
import { decodeSession, encodeSession, checkExpirationStatus, gracePeriod } from "./auth";

const PUBLIC_ROUTES = ["/", "/favicon.ico", "/api/subscribe", "/api/unsubscribe"];
Expand Down Expand Up @@ -42,21 +42,11 @@ export const auth = defineMiddleware(async (context, next) => {
return unauthorized(`Authorization token has expired. Please create a new authorization token by logging in again.`, true);
}


let session: Session;

if (expiration === "grace") {
// Automatically renew the session and send it back with the response
const { token, expires, issued } = encodeSession(process.env.JWT_SECRET, decodedSession.session);
session = {
...decodedSession.session,
expires: expires,
issued: issued
};
const { token, expires } = encodeSession(process.env.JWT_SECRET, decodedSession.session);

context.cookies.set(cookieName, token, { expires: new Date(expires + gracePeriod) });
} else {
session = decodedSession.session;
}

// Request has a valid or renewed session. Call next to continue to the authenticated route handler
Expand Down

0 comments on commit 472252c

Please sign in to comment.