{
    "componentChunkName": "component---src-pages-blog-markdown-remark-fields-slug-js",
    "path": "/blog/session-based-authentication",
    "result": {"data":{"markdownRemark":{"html":"<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of Contents</h2>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"3301577042576720000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`tight: true\ntoHeading: 3`, `3301577042576720000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"table-of-contents\">\n<ul>\n<li><a href=\"#what-is-session-based-authentication\">What is Session-Based Authentication?</a></li>\n<li><a href=\"#how-does-session-based-authentication-work\">How Does Session-Based Authentication Work?</a></li>\n<li><a href=\"#advantages-of-session-based-authentication\">Advantages of Session-Based Authentication</a>\n<ul>\n<li><a href=\"#security-on-easy-mode\">Security on easy mode</a></li>\n<li><a href=\"#easy-implementation\">Easy implementation</a></li>\n</ul>\n</li>\n<li><a href=\"#implementing-session-based-authentication-in-practice\">Implementing Session-Based Authentication in Practice</a>\n<ul>\n<li><a href=\"#setting-up-the-environment\">Setting Up the Environment</a></li>\n<li><a href=\"#server-configuration\">Server Configuration</a></li>\n<li><a href=\"#creating-routes-and-handlers\">Creating Routes and Handlers</a></li>\n</ul>\n</li>\n<li><a href=\"#risks-challenges-and-considerations\">Risks, Challenges, and Considerations</a>\n<ul>\n<li><a href=\"#potential-security-vulnerabilities\">Potential Security Vulnerabilities</a></li>\n<li><a href=\"#scalability-concerns\">Scalability Concerns</a></li>\n</ul>\n</li>\n<li><a href=\"#when-to-choose-session-based-authentication\">When to Choose Session-Based Authentication?</a></li>\n<li><a href=\"#alternatives-to-session-based-authentication\">Alternatives to Session-Based Authentication</a>\n<ul>\n<li><a href=\"#where-supertokens-fits\">Where SuperTokens fits</a></li>\n</ul>\n</li>\n<li><a href=\"#conclusion\">Conclusion</a></li>\n</ul>\n</div>\n<p>The moment you open your browser, you’re likely to already have at least a few open. Sessions - one of the most fundamental building blocks of auth, is considered simple, yet that’s only surface level. Today, we’re taking that complexity apart, with the goal of understanding what makes it tick better.</p>\n<hr>\n<h2 id=\"what-is-session-based-authentication\" style=\"position:relative;\"><a href=\"#what-is-session-based-authentication\" aria-label=\"what is session based authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is Session-Based Authentication?</h2>\n<p>You’ve probably heard that the web is stateless. That’s an obvious problem when you’re trying to build software that needs to remember who’s logged-in. Put in layman’s terms, session-based authentication is a way of saying “I know who you are” between requests. To sprinkle a bit of tech speak on top of that definition, sessions introduce statefulness by keeping session data on the server. Every piece of session data is isolated to the user it belongs to.</p>\n<p>Looking under the proverbial hood, key aspects of session-based authentication include:</p>\n<ul>\n<li><strong>Session Storage</strong>: Information about the user session is stored on the server in memory, a file system, or a database.</li>\n<li><strong>Session Cookie</strong>: The client holds a session identifier in the form of a cookie, which is sent with each subsequent request to the server. Pick a random tab from the ones you have open - you’re likely to see a few of these in your devtools.</li>\n<li><strong>User Authentication</strong>: By validating the session ID against server-side session data, the server verifies the user’s identity.</li>\n</ul>\n<p>But, we need to go deeper.</p>\n<hr>\n<h2 id=\"how-does-session-based-authentication-work\" style=\"position:relative;\"><a href=\"#how-does-session-based-authentication-work\" aria-label=\"how does session based authentication work permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How Does Session-Based Authentication Work?</h2>\n<p>The process involves the following steps:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/450ac10842804c2bb012e609bd70e592/8b936/auth-flow.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 100.63291139240506%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC9ElEQVQ4y3VTy3LbMAz0D9TimyL1thzLVvxInCbHTqfXHjrT//+WRQeUZDvt9ABBJEBwF1iuhNGQ3pIqc1JNJNVXMLuG7NCQ37cU9i0VQ0PlrqZ6W1HbF9R1gTatR1ca2rcBb+MG26Cw8RmthJKkncW3H9/hqgIiOlJVgKoj6SaSaSLZJpKrA7kqJ196yqOjEC1ZI3Acd/T710+qgyWv1lgJIUgbg9PlDJt7CKNJeptQy9xB3cxCewvtDLTVpJmZFFQVBV7OJzitoAQjFIKUUrDWQCoJISUJ9pNRpgRli5eCY8R5UgpIBqMVvPdQUqZ1KiilhPc1tLJQSpNSBkoZ0toizyO8D8n4n/fuORpKWeL1UmcuqNA0z3CuhDGBrI0wJpL3Bbq2R9dt0TSb5J2bYlNOIOeqtCdEBiklrbiqEBlZm5NzJRkTYO10QOtAMVao6xZN04H/eY9jnDMVLpjJgjAV5AVTYRokpZ6pcNM1hVCgLGuEUMK5PO09xpmu1vpecBlK37+mPi6UJwQBm80TjsdLoq6Ug7XFjYExMZ3h1twoc8H1ek3G5BBC0eNQGK21DjFEhBATwiXGnhHyGTaukYbCn67rsN8PyTNavoTbMHvKsiwZ93reT303xmC3e8I4HlBVVcpfcYHj8YiPj3ecz2cYo/Fw8FZo8mIx8No5i8vlgvf3r+j7Pu3dhmKM+0Rn0SGLdjGtzT85TJtlx8VuPeSNvr9+GgpLwrkCw27EYX/Efv+MGFP8YSgs+IqcC4nFLGxJ/IwWhFx8koRKKLzP4ZxPngf0IJeUM7EIqa+Pwoa1SfWLZO4IhzHZ4XBEnhczwuL2UkLosNm8LJRxe8sxdgtCekTBUsnnd8xI/kY493Me1sNLqesR/C6n24vkGWFVdWjn91zXHZbY7NMZ1nCWrSfKLJtxHHG9vmAYdon+JJvJZ9k6iXa9/pL8EuN1UURcr694e7vidDrBWnsX9jAMaJqWb0k3zf6/xhS5AJ/dbrdJ2AzuDywq+A9t4dJ6AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"auth flow\"\n        title=\"auth flow\"\n        src=\"/static/450ac10842804c2bb012e609bd70e592/f058b/auth-flow.png\"\n        srcset=\"/static/450ac10842804c2bb012e609bd70e592/c26ae/auth-flow.png 158w,\n/static/450ac10842804c2bb012e609bd70e592/6bdcf/auth-flow.png 315w,\n/static/450ac10842804c2bb012e609bd70e592/f058b/auth-flow.png 630w,\n/static/450ac10842804c2bb012e609bd70e592/40601/auth-flow.png 945w,\n/static/450ac10842804c2bb012e609bd70e592/78612/auth-flow.png 1260w,\n/static/450ac10842804c2bb012e609bd70e592/8b936/auth-flow.png 1368w\"\n        sizes=\"(max-width: 630px) 100vw, 630px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<ol>\n<li><strong>User logs-in</strong>: The user sends their credentials (e.g., username and password) via a login request.</li>\n<li><strong>Server creates a session</strong>: The server validates the credentials. If valid, a session is initiated and associated with a unique session ID. That session is remembered by the server and stored in the client for reference inside a cookie (an HTTP-only cookie).</li>\n<li><strong>On every followup request</strong>: The client includes the session cookie (with the session ID), allowing the server to retrieve session information.</li>\n<li><strong>Session Validation</strong>: The server goes “Ah I remember you”, and allows you to access a protected route.</li>\n</ol>\n<hr>\n<h2 id=\"advantages-of-session-based-authentication\" style=\"position:relative;\"><a href=\"#advantages-of-session-based-authentication\" aria-label=\"advantages of session based authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Advantages of Session-Based Authentication</h2>\n<p>Okay, so the general idea isn’t that complex. But let’s take a short detour and see what makes session-based auth a good choice, especially in a world where token-based auth is also a thing.</p>\n<h3 id=\"security-on-easy-mode\" style=\"position:relative;\"><a href=\"#security-on-easy-mode\" aria-label=\"security on easy mode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Security on easy mode</h3>\n<p>Session data is stored on the server, making it less susceptible to tampering compared to client-side storage methods like local storage. The only data the client holds related to the session lives inside the HTTP-only cookie the server sets as reference (and that one can’t be tampered with).</p>\n<p>Additionally, the method you use to store the session data is largely up to you, the software author. Depending on what you’re trying to build, you might go for a database, and in-memory store or even the filesystem.</p>\n<h3 id=\"easy-implementation\" style=\"position:relative;\"><a href=\"#easy-implementation\" aria-label=\"easy implementation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Easy implementation</h3>\n<p>We have plenty of choice when it comes to integrating a session mechanism in our apps - frameworks and libraries, such as <code class=\"language-text\">express-session</code> for Node.js, for one. Just searching for <a href=\"\">session</a> on NPM, yields 1000+ packages:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/d40a2e2cca3424cb30da53d060a182f4/1e2b7/npm.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 58.86075949367089%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAABKElEQVQoz41Si06EMBDkuzV+lvoZ3kfAgbRAW97PMbO6BC/iXZMJpbuZzsw2uiZXdF2Huq7l+wjatsW6rnh/fcPL0zOKosCyLOj7HlEdaoQQkOc5yrLAOI6Y5xnTNP0LEiRxjMvHBdZaeO/lPAKApmngKg/vWmzrhm37jb/W8ZxEFMIlhLThnUdVObEzDIOATWy+vUBB2wT37OVXCOm9LEtRqiRsvFX0CLEQfmYZsiwTdccMlZz/Z6RczJMud0IGmqapTJpFQm893n5P5U7IKTv3nR9t8zZVdrR/ppB1Zr4TOh9gjBGlLLBBlRKPqGPfPuWkCDDWikrapjJVp41nZJohB7srbLtOHqf9IWVRc7w3Ya2rEyG0SQxrjBBpnrTP/XHCZ4/7mOEXYTGrIkaVD94AAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"npm package\"\n        title=\"npm package\"\n        src=\"/static/d40a2e2cca3424cb30da53d060a182f4/f058b/npm.png\"\n        srcset=\"/static/d40a2e2cca3424cb30da53d060a182f4/c26ae/npm.png 158w,\n/static/d40a2e2cca3424cb30da53d060a182f4/6bdcf/npm.png 315w,\n/static/d40a2e2cca3424cb30da53d060a182f4/f058b/npm.png 630w,\n/static/d40a2e2cca3424cb30da53d060a182f4/40601/npm.png 945w,\n/static/d40a2e2cca3424cb30da53d060a182f4/78612/npm.png 1260w,\n/static/d40a2e2cca3424cb30da53d060a182f4/1e2b7/npm.png 1483w\"\n        sizes=\"(max-width: 630px) 100vw, 630px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Statistically speaking, there’s a high change that there’s one for your specific stack that’s ready to be used out of the box. Or, you know, check out <a href=\"https://supertokens.com\" target=\"_blank\" rel=\"nofollow\">supertokens</a> 😉</p>\n<hr>\n<h2 id=\"implementing-session-based-authentication-in-practice\" style=\"position:relative;\"><a href=\"#implementing-session-based-authentication-in-practice\" aria-label=\"implementing session based authentication in practice permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Implementing Session-Based Authentication in Practice</h2>\n<p>Let’s have a look at a basic example, in node (using express), on how we can implement session auth from scratch. If “by scratch”, we mean with well-tested building blocks available on NPM. Otherwise, we might just end up having to resort to write an entire framework ourselves, which we can probably agree is out of the scope of this article.</p>\n<h3 id=\"setting-up-the-environment\" style=\"position:relative;\"><a href=\"#setting-up-the-environment\" aria-label=\"setting up the environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting Up the Environment</h3>\n<ol>\n<li>\n<p><strong>Initialize the Project</strong></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"20154867864778514000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`mkdir session-auth\ncd session-auth\nnpm init -y`, `20154867864778514000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">mkdir</span> session-auth\n<span class=\"token builtin class-name\">cd</span> session-auth\n<span class=\"token function\">npm</span> init -y</code></pre></div>\n</li>\n<li>\n<p><strong>Install Dependencies</strong></p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"8830935767480574000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`npm install express express-session`, `8830935767480574000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> express express-session</code></pre></div>\n</li>\n</ol>\n<h3 id=\"server-configuration\" style=\"position:relative;\"><a href=\"#server-configuration\" aria-label=\"server configuration permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Server Configuration</h3>\n<p>Create an <code class=\"language-text\">index.js</code> (or .ts) file and set up your server with session support, via the express-session package:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"21206057969954075000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`const express = require('express');\nconst sessions = require('express-session');\n\nconst app = express();\n\napp.use(\n  sessions({\n    secret: 'super-secret-key', // but, really, REALLY, keep this in one in ENV\n    saveUninitialized: false, // don't create session until something stored\n    resave: false, // don't save session if unmodified\n  })\n);\n\napp.use(express.json());\napp.use(express.urlencoded({ extended: true }));\n\napp.listen(3000, () => console.log('Server is running on port 3000'));`, `21206057969954075000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> express <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> sessions <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'express-session'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> app <span class=\"token operator\">=</span> <span class=\"token function\">express</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>\n  <span class=\"token function\">sessions</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">secret</span><span class=\"token operator\">:</span> <span class=\"token string\">'super-secret-key'</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// but, really, REALLY, keep this in one in ENV</span>\n    <span class=\"token literal-property property\">saveUninitialized</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// don't create session until something stored</span>\n    <span class=\"token literal-property property\">resave</span><span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// don't save session if unmodified</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>express<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">use</span><span class=\"token punctuation\">(</span>express<span class=\"token punctuation\">.</span><span class=\"token function\">urlencoded</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">extended</span><span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\napp<span class=\"token punctuation\">.</span><span class=\"token function\">listen</span><span class=\"token punctuation\">(</span><span class=\"token number\">3000</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Server is running on port 3000'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<hr>\n<h3 id=\"creating-routes-and-handlers\" style=\"position:relative;\"><a href=\"#creating-routes-and-handlers\" aria-label=\"creating routes and handlers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Creating Routes and Handlers</h3>\n<ol>\n<li><strong>Home Route</strong> Redirect logged-out users to the login page or display a personalized message for logged-in users:</li>\n</ol>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"24429573041710250000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`module.exports = function homeHandler(req, res) {\n  if (!req.session.userId) {\n    return res.redirect('/login'); // Redirect to login if not authenticated\n  }\n\n  res.setHeader('Content-Type', 'text/html');\n  res.write(\\`\n    <h1>Welcome back, \\${req.session.userId}!</h1>\n    <a href=&quot;/logout&quot;>Logout</a>\n  \\`);\n  res.end();\n};\n`, `24429573041710250000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\">module<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">exports</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span> <span class=\"token function\">homeHandler</span><span class=\"token punctuation\">(</span>req<span class=\"token punctuation\">,</span> res<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>req<span class=\"token punctuation\">.</span>session<span class=\"token punctuation\">.</span>userId<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">redirect</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Redirect to login if not authenticated</span>\n  <span class=\"token punctuation\">}</span>\n\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">setHeader</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Content-Type'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'text/html'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">write</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">\n    &lt;h1>Welcome back, </span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>req<span class=\"token punctuation\">.</span>session<span class=\"token punctuation\">.</span>userId<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">!&lt;/h1>\n    &lt;a href=\"/logout\">Logout&lt;/a>\n  </span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">end</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n</code></pre></div>\n<ol start=\"2\">\n<li><strong>Login Route</strong> Serve the login form and process authentication using hardcoded credentials for simplicity.</li>\n</ol>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"68437131119072500000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// handlers/login.js\nmodule.exports = function loginHandler(req, res) {\n  if (req.session.userId) {\n    return res.redirect('/'); // If already logged in, redirect to home\n  }\n\n  res.setHeader('Content-Type', 'text/html');\n  res.write(\\`\n    <h1>Login</h1>\n    <form method=&quot;post&quot; action=&quot;/login&quot;>\n      <input type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;Username&quot; required /> <br>\n      <input type=&quot;password&quot; name=&quot;password&quot; placeholder=&quot;Password&quot; required /> <br>\n      <button type=&quot;submit&quot;>Login</button>\n    </form>\n  \\`);\n  res.end();\n};\n`, `68437131119072500000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// handlers/login.js</span>\nmodule<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">exports</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span> <span class=\"token function\">loginHandler</span><span class=\"token punctuation\">(</span>req<span class=\"token punctuation\">,</span> res<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>req<span class=\"token punctuation\">.</span>session<span class=\"token punctuation\">.</span>userId<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">redirect</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// If already logged in, redirect to home</span>\n  <span class=\"token punctuation\">}</span>\n\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">setHeader</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Content-Type'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'text/html'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">write</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">\n    &lt;h1>Login&lt;/h1>\n    &lt;form method=\"post\" action=\"/login\">\n      &lt;input type=\"text\" name=\"username\" placeholder=\"Username\" required /> &lt;br>\n      &lt;input type=\"password\" name=\"password\" placeholder=\"Password\" required /> &lt;br>\n      &lt;button type=\"submit\">Login&lt;/button>\n    &lt;/form>\n  </span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">end</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n</code></pre></div>\n<ol start=\"3\">\n<li><strong>Login route handler</strong> Destroy the session and redirect users to the homepage.</li>\n</ol>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"66625931636269750000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// handlers/process-login.js\nmodule.exports = function processLoginHandler(req, res) {\n  const { username, password } = req.body;\n\n  // Simple authentication logic\n  if (username === 'example-user' && password === 'password123') {\n    req.session.userId = username; // Store userId in session, or any other info, for that matter\n    return res.redirect('/'); // Redirect to home on successful login\n  }\n\n  res.status(401).send('Invalid username or password');\n};\n`, `66625931636269750000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// handlers/process-login.js</span>\nmodule<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">exports</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span> <span class=\"token function\">processLoginHandler</span><span class=\"token punctuation\">(</span>req<span class=\"token punctuation\">,</span> res<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> username<span class=\"token punctuation\">,</span> password <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> req<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Simple authentication logic</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>username <span class=\"token operator\">===</span> <span class=\"token string\">'example-user'</span> <span class=\"token operator\">&amp;&amp;</span> password <span class=\"token operator\">===</span> <span class=\"token string\">'password123'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    req<span class=\"token punctuation\">.</span>session<span class=\"token punctuation\">.</span>userId <span class=\"token operator\">=</span> username<span class=\"token punctuation\">;</span> <span class=\"token comment\">// Store userId in session, or any other info, for that matter</span>\n    <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">redirect</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Redirect to home on successful login</span>\n  <span class=\"token punctuation\">}</span>\n\n  res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">401</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Invalid username or password'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n</code></pre></div>\n<ol start=\"4\">\n<li><strong>Logout route handler</strong> logs the user out, deleting the session and the cookie.</li>\n</ol>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"70907100300361960000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// handlers/logout.js\nmodule.exports = function logoutHandler(req, res) {\n  req.session.destroy((err) => {\n    if (err) {\n      return res.status(500).send('Failed to logout');\n    }\n    res.redirect('/login'); // Redirect to login after logout\n  });\n};\n`, `70907100300361960000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// handlers/logout.js</span>\nmodule<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">exports</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span> <span class=\"token function\">logoutHandler</span><span class=\"token punctuation\">(</span>req<span class=\"token punctuation\">,</span> res<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  req<span class=\"token punctuation\">.</span>session<span class=\"token punctuation\">.</span><span class=\"token function\">destroy</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> res<span class=\"token punctuation\">.</span><span class=\"token function\">status</span><span class=\"token punctuation\">(</span><span class=\"token number\">500</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to logout'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    res<span class=\"token punctuation\">.</span><span class=\"token function\">redirect</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Redirect to login after logout</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n</code></pre></div>\n<ol start=\"5\">\n<li><strong>Routes</strong> finally, we need the routes in the index file.</li>\n</ol>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"95264517372350630000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`...\napp.get('/', homeHandler);\napp.get('/login', loginHandler);\napp.post('/login', processLoginHandler);\napp.get('/logout', logoutHandler);\n...`, `95264517372350630000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token operator\">...</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/'</span><span class=\"token punctuation\">,</span> homeHandler<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">,</span> loginHandler<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">post</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/login'</span><span class=\"token punctuation\">,</span> processLoginHandler<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\napp<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/logout'</span><span class=\"token punctuation\">,</span> logoutHandler<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token operator\">...</span></code></pre></div>\n<hr>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">\n### Running the Application\n\nAdd a `start` script to `package.json` and launch your server:\n\n```bash\nnpm start</code></pre></div>\n<p>This example isn’t going to do much by itself. Also, hardcoding usernames and passwords isn’t exactly the best practice ever, but it illustrates how sessions work.</p>\n<p>Now, we also have this via the <a href=\"https://supertokens.com/docs/thirdpartyemailpassword/quickstart/frontend-setup\" target=\"_blank\" rel=\"nofollow\">SuperTokens SDK</a> too - and you can set it up in less than 5 minutes :)</p>\n<hr>\n<h2 id=\"risks-challenges-and-considerations\" style=\"position:relative;\"><a href=\"#risks-challenges-and-considerations\" aria-label=\"risks challenges and considerations permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Risks, Challenges, and Considerations</h2>\n<p>Of course, anyone claiming they have a silver bullet for something is probably selling silver bullets. Around here, we’re more of the “right tool for the right job” mentality. So, sessions are not a silver bullet, and let’s have a look at some of their tradeoffs.</p>\n<h3 id=\"potential-security-vulnerabilities\" style=\"position:relative;\"><a href=\"#potential-security-vulnerabilities\" aria-label=\"potential security vulnerabilities permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Potential Security Vulnerabilities</h3>\n<ul>\n<li><strong>CSRF Attacks</strong>: These are still a thing. Protect session cookies using the <code class=\"language-text\">SameSite</code> and <code class=\"language-text\">HttpOnly</code> attributes.</li>\n<li><strong>Session Hijacking</strong>: While not as common anymore, make sure to use <code class=\"language-text\">https</code>. Which you probably should be doing anyway.</li>\n<li><strong>Session Expiration</strong>: Sessions should expire, not last until the next holiday season.</li>\n</ul>\n<h3 id=\"scalability-concerns\" style=\"position:relative;\"><a href=\"#scalability-concerns\" aria-label=\"scalability concerns permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Scalability Concerns</h3>\n<p>Here’s the thing - storing stuff in memory (as in the example above), runs the risk of overwhelming said memory. Having a lot of users is a good problem to have, but make sure to use something like Redis (or similar) to make sure that their sessions actually remain accessible.</p>\n<hr>\n<h2 id=\"when-to-choose-session-based-authentication\" style=\"position:relative;\"><a href=\"#when-to-choose-session-based-authentication\" aria-label=\"when to choose session based authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>When to Choose Session-Based Authentication?</h2>\n<p>The bottom line is, there’s a place and time to use session-based auth. Going the session way is probably a good idea if your app:</p>\n<ul>\n<li>Has frequent interaction between the client and server.</li>\n<li>Requires centralized control over user sessions.</li>\n<li>Doesn’t have to heavily rely on client-side security mechanisms.</li>\n</ul>\n<p>Cool. Now let’s have a look at the alternatives.</p>\n<hr>\n<h2 id=\"alternatives-to-session-based-authentication\" style=\"position:relative;\"><a href=\"#alternatives-to-session-based-authentication\" aria-label=\"alternatives to session based authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Alternatives to Session-Based Authentication</h2>\n<p>If what your building:</p>\n<ol>\n<li>Is purely an API-based service</li>\n<li>Needs to scale statelessly</li>\n<li>Has mobile (or desktop) apps as primary clients</li>\n</ol>\n<p>it’s probably time to look elsewhere:</p>\n<ol>\n<li><strong>Token-Based Authentication</strong>\n<ul>\n<li>Stateless and suitable for APIs.</li>\n<li>Relies on JSON Web Tokens (JWTs) for transmitting user identity.</li>\n</ul>\n</li>\n<li><strong>OAuth</strong>\n<ul>\n<li>Delegated access control mechanism for third-party applications (think Google, Facebook, etc.)</li>\n</ul>\n</li>\n</ol>\n<hr>\n<h3 id=\"where-supertokens-fits\" style=\"position:relative;\"><a href=\"#where-supertokens-fits\" aria-label=\"where supertokens fits permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Where SuperTokens fits</h3>\n<p>We believe in giving you the tools to build the auth solution you need. We support both session-based auth and token-based auth. You can mix and match according to the requirements of your app.</p>\n<p>Check it out <a href=\"https://supertokens.com/product\" target=\"_blank\" rel=\"nofollow\">here</a>.</p>\n<hr>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>Session-based authentication remains an important tool in web application security. It wins on simplicity simplicity and control, where token-based approached win on distribution and scalability. Luckily, it doesn’t have to be either/or.  Try <a href=\"https://supertokens.com/product\" target=\"_blank\" rel=\"nofollow\">SuperTokens</a> today!</p>\n<p>And remember: security is a journey, not a destination. Keep learning, keep updating your knowledge, and never trust user inputs. 😉</p>","frontmatter":{"date":"November 18, 2024","title":"Session-Based Authentication: A Detailed Guide [2024]","cover":"session-based-authentication.png","author":"Darko Bozhinovski","description":"Session-based authentication is a cornerstone of web security, providing a simple and controlled method to manage user sessions. This guide delves into its workings, advantages, and implementation, while addressing challenges like security vulnerabilities and scalability concerns."},"fields":{"slug":"/session-based-authentication/"}},"site":{"siteMetadata":{"title":"SuperTokens Blog"}}},"pageContext":{"id":"60d78c46-d186-598f-b195-274408e4210d","fields__slug":"/session-based-authentication/","__params":{"fields__slug":"session-based-authentication"}}},
    "staticQueryHashes": []}