Skip to content

Commit

Permalink
session cookie being set
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiernebre committed Jul 20, 2024
1 parent 232cd3d commit 64bee87
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { STATUS_CODE, StatusCode } from "@std/http";

type HtmlResponseParameters = {
status?: StatusCode;
headers?: HeadersInit;
};

export const htmlResponse = (
html: string,
status: StatusCode = STATUS_CODE.OK,
{ status = STATUS_CODE.OK, headers = {} }: HtmlResponseParameters = {},
) =>
new Response(
html,
{
status,
headers: {
"Content-Type": "text/html",
...headers,
},
},
);
Expand Down
4 changes: 3 additions & 1 deletion src/pages/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ const renderErrorPage = (error: Error) =>
layout(/*html*/ `
<p>Got error when registering: ${error.message}</p>
`),
STATUS_CODE.BadRequest,
{
status: STATUS_CODE.BadRequest,
},
);

export default httpRouter({
Expand Down
26 changes: 20 additions & 6 deletions src/pages/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { STATUS_CODE } from "@std/http";
import { setCookie, STATUS_CODE } from "@std/http";
import { sql } from "../db/mod.ts";
import { Registration } from "../domain/registration.ts";
import { Session, type SessionForm } from "../domain/session.ts";
Expand Down Expand Up @@ -45,19 +45,33 @@ const createSession = async ({ id }: Registration) => {
return session;
};

const renderLoggedIn = (session: Session) =>
htmlResponse(
const renderLoggedIn = (session: Session) => {
const headers = new Headers();
setCookie(headers, {
name: "session",
value: session.id,
httpOnly: true,
sameSite: "Strict",
});
headers.set("content-type", "text/html");
return new Response(
/*html*/ `
<div>Logged in ${session.id}</div>
`,
<div>Logged in ${session.id}</div>
`,
{
headers,
},
);
};

const renderError = (error: Error) =>
htmlResponse(
/*html*/ `
<div>Could not login due to an error: ${error}</div>
`,
STATUS_CODE.BadRequest,
{
status: STATUS_CODE.BadRequest,
},
);

const get = () =>
Expand Down
1 change: 1 addition & 0 deletions tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const post = (url: string, body: Record<string, string>) =>
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams(body),
credentials: "include",
});

export const register = async () => {
Expand Down
6 changes: 5 additions & 1 deletion tests/session.api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { STATUS_CODE } from "@std/http";
import { getSetCookies, STATUS_CODE } from "@std/http";
import { start } from "../src/server.ts";
import { assert, assertEquals } from "@std/assert";
import { SESSION_URL } from "./utils.ts";
Expand Down Expand Up @@ -66,4 +66,8 @@ Deno.test("successfully logs in", async () => {
});
assertEquals(response.status, STATUS_CODE.OK);
assert((await response.text()).includes("Logged in"));
const [sessionCookie] = getSetCookies(response.headers);
assert(sessionCookie);
assertEquals(sessionCookie.name, "session");
assertEquals(sessionCookie.value, "1");
});

0 comments on commit 64bee87

Please sign in to comment.