Skip to content

Commit

Permalink
Implement logging out
Browse files Browse the repository at this point in the history
A link in the header is provided to logged in users. The `/logout`
endpoint instructs the browser to delete the Token cookie and redirects
the user to the root page.
  • Loading branch information
danlivings-dxw committed Oct 17, 2024
1 parent acddb34 commit 289a884
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
8 changes: 6 additions & 2 deletions base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
</head>
<body class="bg-stone-100 text-stone-800 dark:bg-stone-800 dark:text-stone-200">
<header class="bg-white dark:bg-stone-600">
<nav class="w-full flex p-6 border-b-4 border-stone-200 dark:border-stone-900">
<h1 class="text-3xl">Towtruck</h1>
<nav class="w-full flex items-baseline p-6 border-b-4 border-stone-200 dark:border-stone-900">
<h1 class="inline-block text-3xl"><a href="/" class="hover:underline">Towtruck</a></h1>
<div class="grow leading-9">&zwj;</div>
{% if loggedIn %}
<div class="leading-9"><a href="/logout" class="hover:underline">Logout</a></div>
{% endif %}
</nav>
</header>

Expand Down
12 changes: 12 additions & 0 deletions routes/handlers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nunjucks from "nunjucks";
import { cookie } from "../auth/cookie.js";
import { mapRepoFromStorageToUi } from "../utils/index.js";
import { getQueryParams } from "../utils/queryParams.js";
import { sortByType } from "../utils/sorting.js";
Expand All @@ -20,6 +21,7 @@ export const index = async (token, request, response) => {

const template = nunjucks.render("index.njk", {
orgs,
loggedIn: true
});

return response.end(template);
Expand Down Expand Up @@ -51,7 +53,17 @@ export const org = async (token, request, response, {org}) => {
org,
...reposForUi,
repos: sortByType(reposForUi.repos, sortDirection, sortBy),
loggedIn: true,
});

return response.end(template);
}

export const logout = async (token, request, response) => {
response.writeHead(302, {
"Location": "/",
...cookie("Token", "", "/", new Date(0)),
});

return response.end();
};
3 changes: 2 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { index, org } from "./handlers.js";
import { index, org, logout } from "./handlers.js";
import { match } from "./match.js";

const pathHandlers = {
"/": index,
"/{org}": org,
"/logout": logout,
};

export const handleRoutes = async (token, request, response) => {
Expand Down

0 comments on commit 289a884

Please sign in to comment.