Skip to content

Commit

Permalink
Added logout button
Browse files Browse the repository at this point in the history
  • Loading branch information
hopperelec committed Dec 4, 2023
1 parent 0882625 commit 2b432fe
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Options } from "html-minifier-terser";
import { minify } from "html-minifier-terser";
import prisma from "$lib/prisma";
import { toBuffer } from "uuid-buffer";
import { SESSION_COOKIE_KEY } from "$lib/constants";

const minification_options: Options = {
collapseInlineTagWhitespace: true,
Expand All @@ -18,7 +19,7 @@ async function isAuthorized(event: RequestEvent): Promise<boolean> {
if (event.url.pathname.startsWith("/login")) {
return true;
}
const sessionUUID = event.cookies.get("session");
const sessionUUID = event.cookies.get(SESSION_COOKIE_KEY);
if (!sessionUUID) {
return false;
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib/LogoutButton.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<a href="/logout">Logout</a>

<style>
a {
color: white;
background-color: dimgray;
border: 3px solid black;
text-decoration: none;
text-transform: uppercase;
padding: 10px;
border-radius: 5px;
font-family: "Arial Black", "Arial Bold", Gadget, sans-serif;
}
</style>
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SESSION_COOKIE_KEY = "session";
export const SESSION_DURATION_DAYS = 7;
4 changes: 3 additions & 1 deletion src/routes/(app)/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script lang="ts">
import type { LayoutData } from "./$types";
import LogoutButton from "$lib/LogoutButton.svelte";
export let data: LayoutData;
</script>

You are currently logged in as {data.user.name}
<p>You are currently logged in as {data.user.name}</p>
<LogoutButton />
5 changes: 2 additions & 3 deletions src/routes/login/callback/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import prisma from "$lib/prisma";
import type { School, User } from "@prisma/client";
import { toBuffer } from "uuid-buffer";
import { dev } from "$app/environment";

const SESSION_DURATION_DAYS = 7;
import { SESSION_COOKIE_KEY, SESSION_DURATION_DAYS } from "$lib/constants";

export const POST: RequestHandler = async ({ request, cookies }) => {
const params = new URLSearchParams(await request.text());
Expand Down Expand Up @@ -73,7 +72,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
},
});

cookies.set("session", session_uuid, {
cookies.set(SESSION_COOKIE_KEY, session_uuid, {
path: "/",
httpOnly: true,
secure: !dev,
Expand Down
23 changes: 23 additions & 0 deletions src/routes/logout/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SESSION_COOKIE_KEY } from "$lib/constants";
import prisma from "$lib/prisma";
import { toBuffer } from "uuid-buffer";
import type { RequestHandler } from "@sveltejs/kit";

export const GET: RequestHandler = async ({ cookies }) => {
const session_uuid = cookies.get(SESSION_COOKIE_KEY);
if (session_uuid) {
await prisma.session.update({
data: {
expires: new Date(),
},
where: {
uuid_bin: toBuffer(session_uuid),
},
});
cookies.delete(SESSION_COOKIE_KEY);
}
return new Response("Redirect", {
status: 303,
headers: { Location: "/login" },
});
};

0 comments on commit 2b432fe

Please sign in to comment.