{"id":3898,"date":"2026-11-17T12:15:44","date_gmt":"2026-11-17T09:15:44","guid":{"rendered":"https:\/\/niteducation.com\/resources\/?p=3898"},"modified":"2026-11-17T12:16:12","modified_gmt":"2026-11-17T09:16:12","slug":"build-a-secure-login-system","status":"publish","type":"post","link":"https:\/\/niteducation.com\/resources\/build-a-secure-login-system\/","title":{"rendered":"Build a Secure Login System: Cookies, Sessions &amp; Reset"},"content":{"rendered":"\n<article aria-labelledby=\"nit-article-title\" class=\"nit-article nit-article--pathway-guide nit-article--software\" itemscope=\"\" itemtype=\"https:\/\/schema.org\/Article\">\n <meta content=\"en-KE\" itemprop=\"inLanguage\">\n <meta content=\"Newton Institute of Technology Editorial Team\" itemprop=\"author\">\n <div aria-labelledby=\"nit-article-title\" class=\"nit-pg-hero\">\n  <div>\n   <p class=\"nit-pg-kicker\">\n    Software Development Guide\n    \n   <\/p>\n   <p class=\"nit-hero__title\" id=\"nit-article-title\" itemprop=\"headline\">\n    Build a Secure Login System:\n    <br>\n    Sessions, Cookies and Password Reset Explained\n   <\/p>\n   <p class=\"nit-pg-hero__line\">\n    Register\n    <i>\n     \u00b7\n    <\/i>\n    Sign In\n    <i>\n     \u00b7\n    <\/i>\n    Protect\n    <i>\n     \u00b7\n    <\/i>\n    Recover\n   <\/p>\n   <p class=\"nit-pg-hero__summary\" itemprop=\"description\">\n    Understand passwords, session cookies, protected requests and recovery by examining one working local application\u2014and verifying its failure cases.\n   <\/p>\n   <div class=\"nit-pg-hero__actions\">\n    <a class=\"nit-pg-button nit-pg-button--gold\" href=\"#setup\">\n     Start the practical build\n    <\/a>\n    <a class=\"nit-pg-button nit-pg-button--outline\" href=\"#testing\">\n     Run the security tests\n    <\/a>\n   <\/div>\n  <\/div>\n  <div aria-label=\"Authentication learning pathway\" class=\"nit-pg-hero__art\">\n   <p>\n    Authentication lab\n   <\/p>\n   <div aria-hidden=\"true\" class=\"nit-pg-signpost\">\n    <span class=\"nit-pg-signpost__top\">\n     LOGIN\n    <\/span>\n    <span class=\"nit-pg-signpost__centre\">\n     AUTH\n    <\/span>\n    <span class=\"nit-pg-signpost__bottom\">\n     RESET\n    <\/span>\n   <\/div>\n   <strong>\n    Browser \u2192 Server \u2192 Database\n   <\/strong>\n  <\/div>\n  <div class=\"nit-pg-hero__meta\">\n   <p>\n    <span>\n     Project\n    <\/span>\n    Login learning lab\n   <\/p>\n   <p>\n    <span>\n     Skills\n    <\/span>\n    Cookies \u00b7 Sessions \u00b7 Reset\n   <\/p>\n   <p>\n    <span>\n     Level\n    <\/span>\n    Beginner to intermediate\n   <\/p>\n   <p>\n    <span>\n     Updated\n    <\/span>\n    16 November 2026\n   <\/p>\n  <\/div>\n <\/div>\n <div class=\"nit-article__body\">\n  <section aria-labelledby=\"quick-answer-title\" class=\"nit-quick-answer\" id=\"quick-answer\">\n   <div class=\"nit-quick-answer__head\">\n    <span aria-hidden=\"true\" class=\"nit-quick-answer__mark\">\n     \u2713\n    <\/span>\n    <div>\n     <p class=\"nit-quick-answer__label\">\n      Quick answer\n     <\/p>\n     <p class=\"nit-quick-answer__kicker\" id=\"quick-answer-title\">\n      How do you build a secure login system?\n     <\/p>\n    <\/div>\n   <\/div>\n   <div class=\"nit-quick-answer__body\">\n    <p class=\"nit-quick-answer__text\">\n     <strong>\n      Verify passwords with slow, salted hashes; rotate an unpredictable session ID after login; put server-managed identity behind a restricted cookie; authorise every protected request; and make recovery tokens single-use and time-limited.\n     <\/strong>\n     Confirm the failure cases through reproducible tests.\n    <\/p>\n    <ul class=\"nit-quick-answer__signals\">\n     <li>\n      Hash\n     <\/li>\n     <li>\n      Rotate\n     <\/li>\n     <li>\n      Authorise\n     <\/li>\n     <li>\n      Recover\n     <\/li>\n     <li>\n      Test\n     <\/li>\n    <\/ul>\n   <\/div>\n  <\/section>\n  <div aria-label=\"Authentication fundamentals\" class=\"nit-facts-grid nit-software-facts\">\n   <div class=\"nit-fact-card\">\n    <span>\n     Browser\n    <\/span>\n    <strong>\n     Session cookie\n    <\/strong>\n   <\/div>\n   <div class=\"nit-fact-card\">\n    <span>\n     Server\n    <\/span>\n    <strong>\n     Checks identity\n    <\/strong>\n   <\/div>\n   <div class=\"nit-fact-card\">\n    <span>\n     Passwords\n    <\/span>\n    <strong>\n     Salted hashes\n    <\/strong>\n   <\/div>\n   <div class=\"nit-fact-card\">\n    <span>\n     Recovery\n    <\/span>\n    <strong>\n     Single-use link\n    <\/strong>\n   <\/div>\n   <div class=\"nit-fact-card\">\n    <span>\n     Permissions\n    <\/span>\n    <strong>\n     Owner-only records\n    <\/strong>\n   <\/div>\n   <div class=\"nit-fact-card\">\n    <span>\n     Verification\n    <\/span>\n    <strong>\n     Negative tests\n    <\/strong>\n   <\/div>\n  <\/div>\n  <nav aria-labelledby=\"toc-title\" class=\"nit-toc\">\n   <h2 id=\"toc-title\">\n    On this page\n   <\/h2>\n   <ol>\n    <li>\n     <a href=\"#security-basics\">\n      Secure login basics\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#architecture\">\n      Cookies and sessions\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#setup\">\n      Project setup\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#passwords\">\n      Password protection\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#login\">\n      Login and rotation\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#protect\">\n      Protected routes\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#recovery\">\n      Password reset\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#testing\">\n      Tests and debugging\n     <\/a>\n    <\/li>\n    <li>\n     <a href=\"#faq\">\n      FAQs and sources\n     <\/a>\n    <\/li>\n   <\/ol>\n  <\/nav>\n  <section aria-label=\"Article introduction\" class=\"nit-section-panel\" id=\"introduction\">\n   <p>\n    A secure login system must\n    <strong>\n     verify a user&#8217;s credentials, store passwords as slow salted hashes, create a new server-side session after sign-in, send the session ID in a restricted cookie, and enforce permissions on every protected request\n    <\/strong>\n    . It also needs reliable logout, abuse controls and a single-use password-reset flow. The way to know whether these features work is to test what the server\n    <em>\n     rejects\n    <\/em>\n    as carefully as what it accepts.\n   <\/p>\n   <p>\n    In this guide, we will follow one small Node.js + SQLite learning project\u2014from registration through password recovery\u2014and inspect the requests and database records that make it work. The examples use fictional accounts such as\n    <strong>\n     mira@example.test\n    <\/strong>\n    ; do not enter real credentials or records in this local demonstration.\n    <a href=\"#ref-1\">\n     [1]\n    <\/a>\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n    <a href=\"#ref-5\">\n     [5]\n    <\/a>\n    <a href=\"#ref-7\">\n     [7]\n    <\/a>\n   <\/p>\n   <p>\n    <strong>\n     What you will finish with:\n    <\/strong>\n    a clear model of cookies versus sessions, a working localhost practice application, and repeatable checks for authentication, authorisation and recovery failures. The companion practice project is supplied separately for editors and students; the public download should be added only after its permanent link is verified.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"security-basics-title\" class=\"nit-section-panel\" id=\"security-basics\">\n   <h2 id=\"security-basics-title\">\n    A login form is not the entire security system\n   <\/h2>\n   <p>\n    Think of a shared personal-notes application with two fictional members, Mira and Joel. They each have a valid account and password. After signing in, Mira must be able to open her private note, but Joel must not gain access by guessing its numeric ID. That is the difference between recognising an account and permitting a particular action.\n   <\/p>\n   <ul>\n    <li>\n     <strong>\n      Authentication\n     <\/strong>\n     verifies the claimed account, for example by checking a password.\n    <\/li>\n    <li>\n     <strong>\n      Session management\n     <\/strong>\n     lets the server recognise a browser on later requests, subject to expiry and revocation.\n    <\/li>\n    <li>\n     <strong>\n      Authorisation\n     <\/strong>\n     checks what the signed-in account may read or change. A successful login is\n     <em>\n      not\n     <\/em>\n     permission to access every record.\n    <\/li>\n    <li>\n     <strong>\n      Account recovery\n     <\/strong>\n     restores access without letting a stolen or guessed recovery link become a new login route.\n    <\/li>\n   <\/ul>\n   <p>\n    The browser displays forms and carries requests, but it must not decide whether a student owns a confidential record. That check belongs on the backend. Passwords also never belong in page URLs, ordinary application logs or browser\n    <strong>\n     localStorage\n    <\/strong>\n    . The underlying requirements are set out in OWASP&#8217;s authentication and session guidance.\n    <a href=\"#ref-1\">\n     [1]\n    <\/a>\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"threat-model-title\" class=\"nit-section-panel\" id=\"threat-model\">\n   <h2 id=\"threat-model-title\">\n    Decide what an attacker must not be able to do\n   <\/h2>\n   <p>\n    A useful security plan starts with concrete failure cases rather than abstract promises. In our fictional notes application, imagine somebody who does\n    <strong>\n     not\n    <\/strong>\n    know Mira&#8217;s password, somebody who knows Joel&#8217;s password but wants Mira&#8217;s notes, and somebody who found an old password-reset link. These are three different problems; one login check cannot solve all of them.\n   <\/p>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        Attempt\n       <\/th>\n       <th scope=\"col\">\n        Control and observable result\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        Guess Mira&#8217;s password repeatedly\n       <\/th>\n       <td>\n        Slow salted password verification plus monitored, proportionate throttling; repeated attempts are limited.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Copy an old anonymous session ID and wait for Mira to log in\n       <\/th>\n       <td>\n        Session ID regeneration after successful login; the previous ID cannot access Mira&#8217;s account.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Sign in as Joel, then change the requested note ID to Mira&#8217;s\n       <\/th>\n       <td>\n        Backend ownership check using the authenticated session&#8217;s user ID; the other person&#8217;s note is not returned.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Reuse an old reset link\n       <\/th>\n       <td>\n        Server checks expiry and single-use status; replay fails.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Submit an unwanted cross-origin update\n       <\/th>\n       <td>\n        Application-specific CSRF and Origin checks reject the request.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Steal an unexpired cookie\n       <\/th>\n       <td>\n        This remains a serious risk; HttpOnly helps against direct JavaScript cookie reads but does not eliminate XSS, malware or session theft.\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <p>\n    This is a small\n    <strong>\n     threat model\n    <\/strong>\n    : a list of assets, plausible misuse and testable protections. It also reveals the article&#8217;s most important distinction: authentication identifies Joel, but authorisation must still prevent him from accessing Mira&#8217;s information.\n    <a href=\"#ref-1\">\n     [1]\n    <\/a>\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n    <a href=\"#ref-6\">\n     [6]\n    <\/a>\n    <a href=\"#ref-10\">\n     [10]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"architecture-title\" class=\"nit-section-panel\" id=\"architecture\">\n   <h2 id=\"architecture-title\">\n    Cookies and sessions: follow one real request\n   <\/h2>\n   <p>\n    HTTP requests are independent: the server does not automatically remember that a person signed in earlier. In a server-managed session design, a cookie lets the browser carry an unpredictable session identifier. The corresponding authenticated state stays on the server.\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n    <a href=\"#ref-3\">\n     [3]\n    <\/a>\n   <\/p>\n   <ol>\n    <li>\n     <strong>\n      Before login:\n     <\/strong>\n     Mira opens the site. The app may create an anonymous session and issue a CSRF token.\n    <\/li>\n    <li>\n     <strong>\n      Sign in:\n     <\/strong>\n     The browser submits the credentials. The server looks up the account and verifies its password against a slow salted hash.\n    <\/li>\n    <li>\n     <strong>\n      Rotate:\n     <\/strong>\n     On success, the server invalidates the previous session ID, creates a new one and associates it with Mira&#8217;s account.\n    <\/li>\n    <li>\n     <strong>\n      Remember:\n     <\/strong>\n     The server returns a\n     <strong>\n      Set-Cookie\n     <\/strong>\n     header. The browser sends that cookie on eligible later requests.\n    <\/li>\n    <li>\n     <strong>\n      Authorise:\n     <\/strong>\n     The server finds the session record, checks its expiry, and applies the relevant permission or ownership rule before returning data.\n    <\/li>\n    <li>\n     <strong>\n      End:\n     <\/strong>\n     Logout, expiry or a password reset makes old server-side session records unusable.\n    <\/li>\n   <\/ol>\n   <p>\n    <strong>\n     Look at the two sides of the exchange:\n    <\/strong>\n   <\/p>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        Browser holds\n       <\/th>\n       <th scope=\"col\">\n        Backend holds and checks\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        An opaque, unpredictable session ID in an HTTP cookie\n       <\/th>\n       <td>\n        A session record linked to a user ID, with expiry and revocation rules\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Cookie rules including\n        <strong>\n         HttpOnly\n        <\/strong>\n        and\n        <strong>\n         SameSite\n        <\/strong>\n       <\/th>\n       <td>\n        Password hashes, account records and access decisions\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        A short-lived CSRF token used for state-changing requests in this lab\n       <\/th>\n       <td>\n        A verifier for that CSRF token, tied to the session\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        A reset token only while completing a local recovery flow\n       <\/th>\n       <td>\n        A hash of the reset token and its expiry, until consumed\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <p>\n    <strong>\n     Do not confuse the cookie with the session.\n    <\/strong>\n    Clearing the cookie in a browser only removes the local reference. The server must also invalidate the record so a copied old identifier will no longer work. A session-based design is a good learning choice, although other architectures\u2014including tokens signed and carried by clients\u2014exist.\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n    <a href=\"#ref-3\">\n     [3]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"setup-title\" class=\"nit-section-panel\" id=\"setup\">\n   <h2 id=\"setup-title\">\n    Prepare the Node.js authentication project\n   <\/h2>\n   <p>\n    The companion lab uses Node.js built-in HTTP and cryptography modules plus a SQLite database. Unlike a typical Express tutorial, this demonstration deliberately exposes the session logic so you can trace it. It is\n    <strong>\n     not a template for public deployment\n    <\/strong>\n    ; production projects should use reviewed authentication and session libraries. You need Node.js\n    <strong>\n     22.13 or later\n    <\/strong>\n    , a terminal, and sufficient memory for the sample\n    <strong>\n     scrypt\n    <\/strong>\n    configuration. The SQLite interface in Node 22 is experimental.\n   <\/p>\n   <p>\n    <strong>\n     Project structure\n    <\/strong>\n    (once you extract the companion lab ZIP):\n   <\/p>\n  <div aria-label=\"text example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">File tree<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">login-learning-lab\/<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;package.json<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;server.mjs<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;tests.mjs<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;public\/<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;&nbsp;&nbsp;index.html<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;&nbsp;&nbsp;app.js<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;&nbsp;&nbsp;reset.html<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;&nbsp;&nbsp;style.css<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    Open a terminal\n    <strong>\n     inside the extracted folder containing\n     <strong>\n      package.json\n     <\/strong>\n    <\/strong>\n    . Run these commands in order:\n   <\/p>\n  <div aria-label=\"bash example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">Terminal<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">npm start<\/span>\n      <span class=\"nit-code-line\"># Then visit http:\/\/127.0.0.1:3000<\/span>\n      <span class=\"nit-code-line\"># In a second terminal, from the same folder:<\/span>\n      <span class=\"nit-code-line\">npm test<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    No package installation from npm is needed for this particular lab. The tests use a separate disposable database, so the test suite will not erase practice accounts in the normal lab database.\n   <\/p>\n   <p>\n    <strong>\n     Follow one complete workflow:\n    <\/strong>\n    register\n    <strong>\n     mira@example.test\n    <\/strong>\n    with a new practice-only password of 15\u2013128 characters; sign in; click\n    <em>\n     Check protected endpoint\n    <\/em>\n    ; sign out; then request a reset using the same fictional address. The server prints a local-only recovery link to the terminal. Paste it into your own browser, choose a different practice password and try using the link twice. The second attempt should fail.\n   <\/p>\n   <p>\n    <strong>\n     What the browser will call:\n    <\/strong>\n   <\/p>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        Endpoint\n       <\/th>\n       <th scope=\"col\">\n        Expected purpose\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         GET \/api\/csrf\n        <\/strong>\n       <\/th>\n       <td>\n        Create or refresh an anonymous\/authenticated CSRF token\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         POST \/api\/register\n        <\/strong>\n       <\/th>\n       <td>\n        Validate and create a practice account\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         POST \/api\/login\n        <\/strong>\n       <\/th>\n       <td>\n        Check credentials and issue a new authenticated session\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         GET \/api\/me\n        <\/strong>\n       <\/th>\n       <td>\n        Return the current account only when signed in\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         POST \/api\/notes\n        <\/strong>\n       <\/th>\n       <td>\n        Create a private practice note for the authenticated account\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         GET \/api\/notes\/:id\n        <\/strong>\n       <\/th>\n       <td>\n        Return the practice note only if the authenticated account owns it\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         POST \/api\/logout\n        <\/strong>\n       <\/th>\n       <td>\n        Revoke the current session and expire its cookie\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         POST \/api\/forgot\n        <\/strong>\n       <\/th>\n       <td>\n        Start recovery using a generic public response\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         POST \/api\/reset\n        <\/strong>\n       <\/th>\n       <td>\n        Validate and consume a one-use reset token\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <p>\n    The lab binds to\n    <strong>\n     127.0.0.1\n    <\/strong>\n    and uses plain HTTP exclusively for local learning. Never expose it to the public internet or use real student, customer or financial data. An actual HTTPS site must use\n    <strong>\n     Secure\n    <\/strong>\n    cookies and further deployment controls.\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n    <a href=\"#ref-3\">\n     [3]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"database-title\" class=\"nit-section-panel\" id=\"database\">\n   <h2 id=\"database-title\">\n    Understand the four database tables before writing a route\n   <\/h2>\n   <p>\n    Many beginners understand the login form but cannot explain how login stays valid. The lab uses four SQLite tables to make that answer visible:\n   <\/p>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        Table\n       <\/th>\n       <th scope=\"col\">\n        What is stored (and what is deliberately excluded)\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         users\n        <\/strong>\n       <\/th>\n       <td>\n        Account ID, email, salted password hash, creation time; never plaintext passwords\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         sessions\n        <\/strong>\n       <\/th>\n       <td>\n        Hashed random session ID, user ID, CSRF-token verifier, activity and expiry times; never the reusable raw session ID\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         reset_tokens\n        <\/strong>\n       <\/th>\n       <td>\n        Hashed single-use recovery token, associated user ID and expiration time; never the raw reset token\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         practice_notes\n        <\/strong>\n       <\/th>\n       <td>\n        Note ID, note text and the account ID of the owner; ownership is enforced in the server-side query\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <p>\n    The data flow is\n    <strong>\n     POST \/api\/login\n    <\/strong>\n    \u2192 verify stored password hash \u2192 create a random session ID \u2192\n    <strong>\n     store only its hash\n    <\/strong>\n    with the associated user ID \u2192 send the raw opaque ID in a cookie. On a subsequent request, the server hashes the cookie ID and looks up the matching session. That lets the server revoke the record without placing account details inside the cookie.\n   <\/p>\n   <p>\n    <strong>\n     Teaching distinction:\n    <\/strong>\n    Passwords use deliberately slow, salted\n    <strong>\n     scrypt\n    <\/strong>\n    ; random session IDs and reset tokens are already high-entropy secrets, so the lab stores their SHA-256 digests for database lookup.\n    <strong>\n     Do not use fast SHA-256 as a replacement for password hashing.\n    <\/strong>\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n    <a href=\"#ref-5\">\n     [5]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"registration-title\" class=\"nit-section-panel\" id=\"registration\">\n   <h2 id=\"registration-title\">\n    Step 1 \u2014 Register a user without storing plaintext passwords\n   <\/h2>\n   <p>\n    A registration endpoint should accept only the data it needs, validate it server-side, and save a password hash rather than the password itself. In the lab, the users table records a numeric ID, normalised email, password hash and creation time. The browser never decides what an account is authorised to do.\n   <\/p>\n   <p>\n    The practice application uses a\n    <strong>\n     15\u2013128-Unicode-code-point password\n    <\/strong>\n    rule and a deliberately tiny illustrative blocklist. These are demonstration settings, not a complete leaked-password screening system. NIST SP 800-63B-4 (July 2025) requires at least 15 characters for a single-factor password, recommends permitting at least 64, and counts each Unicode\n    <strong>\n     code point\n    <\/strong>\n    as one character. It discourages composition tricks and routine forced changes, and supports password managers and pasting. A real service should check proposed passwords against a maintained list of commonly used or compromised values.\n    <a href=\"#ref-4\">\n     [4]\n    <\/a>\n   <\/p>\n   <p>\n    <strong>\n     A subtle JavaScript bug to avoid:\n    <\/strong>\n    <strong>\n     somePassword.length\n    <\/strong>\n    counts UTF-16 code units, not Unicode code points. For example, eight lock emojis contain 16 JavaScript code units but only eight Unicode code points. A naive\n    <strong>\n     length &gt;= 15\n    <\/strong>\n    check could incorrectly accept them. The companion lab was corrected to use\n    <strong>\n     [&#8230;value].length\n    <\/strong>\n    for this particular length rule:\n   <\/p>\n  <div aria-label=\"javascript example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">server.mjs<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">const passwordOK = value =&gt;<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;typeof value === &#8216;string&#8217; &amp;&amp;<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;[&#8230;value].length &gt;= 15 &amp;&amp;<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;[&#8230;value].length &lt;= 128 &amp;&amp;<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;!BLOCKED_PASSWORDS.has(value.toLowerCase());<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    This counts Unicode code points rather than what a person visually perceives as grapheme clusters; it is the counting distinction in NIST&#8217;s verifier guidance. The server is the policy authority: HTML field length validation may use UTF-16-based measurements and must not replace it.\n    <a href=\"#ref-4\">\n     [4]\n    <\/a>\n   <\/p>\n   <p>\n    <strong>\n     Try it:\n    <\/strong>\n    Register Mira, then try the same email twice. The registration handler uses the same generic response text instead of a revealing \u201caccount already exists\u201d message; timing and other side channels still require assessment in a public application. Use a SQLite viewer on the local\n    <strong>\n     learning-lab.sqlite\n    <\/strong>\n    file while the application is stopped, and inspect the\n    <strong>\n     users\n    <\/strong>\n    table:\n    <strong>\n     password_hash\n    <\/strong>\n    must contain a derived value, not the practice password. Account verification and real confirmation email delivery are deliberately outside this demo. Avoid opening or editing the live database while it is being written.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"passwords-title\" class=\"nit-section-panel\" id=\"passwords\">\n   <h2 id=\"passwords-title\">\n    Step 2 \u2014 Hash passwords using a slow, salted algorithm\n   <\/h2>\n   <p>\n    A hash is a one-way derived value. When a user signs in, the application derives a value from the submitted password and compares it with the stored reference.\n    <strong>\n     Do not store plaintext passwords and do not use ordinary SHA-256 as a password hashing algorithm.\n    <\/strong>\n   <\/p>\n   <p>\n    This project uses Node&#8217;s memory-hard\n    <strong>\n     scrypt\n    <\/strong>\n    , a fresh 16-byte salt per password and explicit work settings (\n    <strong>\n     N=2^17\n    <\/strong>\n    ,\n    <strong>\n     r=8\n    <\/strong>\n    ,\n    <strong>\n     p=1\n    <\/strong>\n    ). These match an OWASP-listed scrypt configuration. OWASP generally prefers Argon2id for new applications when it is available; tune password-hash cost against the service&#8217;s hardware and abuse limits.\n    <a href=\"#ref-5\">\n     [5]\n    <\/a>\n   <\/p>\n  <div aria-label=\"javascript example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">server.mjs<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">async function hashPassword(password) {<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;const salt = randomBytes(16);<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;const derived = await scrypt(password.normalize(&#8216;NFC&#8217;), salt, 64, SCRYPT_OPTIONS);<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;return `scrypt$131072$8$1$${salt.toString(&#8216;hex&#8217;)}$${derived.toString(&#8216;hex&#8217;)}`;<\/span>\n      <span class=\"nit-code-line\">}<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    A random salt means the same password will generally produce a different stored result for each account. The lab applies Unicode\n    <strong>\n     NFC normalisation\n    <\/strong>\n    before deriving the hash and before verifying it. This follows NIST&#8217;s recommendation for accepted Unicode passwords and prevents otherwise equivalent composed and decomposed spellings from producing different derived values. The server then compares the resulting fixed-length hash bytes with\n    <strong>\n     timingSafeEqual\n    <\/strong>\n    ; this does not eliminate all application-level timing differences.\n    <a href=\"#ref-4\">\n     [4]\n    <\/a>\n    <a href=\"#ref-5\">\n     [5]\n    <\/a>\n   <\/p>\n   <p>\n    The salt is not a secret; the stored hashes still require careful database access, backups and an upgrade strategy when hashing parameters change.\n    <strong>\n     scrypt\n    <\/strong>\n    with the shown configuration uses substantial memory by design, making concurrent guessing more expensive but also making uncontrolled signup and verification traffic a capacity risk. Production services need resource-aware throttling and load tests, not simply a stronger hash setting. The revised tests verify both canonical Unicode equivalence and that the old password fails after a reset.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"login-title\" class=\"nit-section-panel\" id=\"login\">\n   <h2 id=\"login-title\">\n    Step 3 \u2014 Verify login and replace the old session ID\n   <\/h2>\n   <p>\n    A browser may have an anonymous session before signing in. The server must not simply attach the new user ID to an identifier that might already be known to someone else. A successful login should replace the old session ID; this is the key defence against\n    <strong>\n     session fixation\n    <\/strong>\n    .\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n   <\/p>\n   <p>\n    The lab checks the account, validates the password hash, returns a generic\n    <strong>\n     401\n    <\/strong>\n    for invalid credentials and applies a small demonstration rate limit. For unknown users it performs a dummy password-hash comparison to reduce an obvious timing signal, although equal response timing is not guaranteed. The relevant success path is:\n   <\/p>\n  <div aria-label=\"javascript example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">server.mjs<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">\/\/ Executed only after the supplied credentials are verified:<\/span>\n      <span class=\"nit-code-line\">db.prepare(&#8216;DELETE FROM sessions WHERE id_hash=?&#8217;).run(hash(session.id));<\/span>\n      <span class=\"nit-code-line\">const fresh = createSession(user.id);<\/span>\n      <span class=\"nit-code-line\">return send(res, 200,<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;{ message: &#8216;Signed in.&#8217;, csrf: fresh.csrf },<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;{ &#8216;Set-Cookie&#8217;: cookie(fresh.id) }<\/span>\n      <span class=\"nit-code-line\">);<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    <strong>\n     Verify it in the browser:\n    <\/strong>\n    use a wrong password (expect\n    <strong>\n     401\n    <\/strong>\n    ), then the correct one (expect\n    <strong>\n     200\n    <\/strong>\n    ). In DevTools \u2192 Application\/Storage \u2192 Cookies, compare the session ID before and after sign-in; the value must change. Inspect it privately without copying it into screenshots or messages.\n   <\/p>\n   <p>\n    <strong>\n     Why limit login attempts?\n    <\/strong>\n    Without abuse controls, repeated guessing is possible. The lab&#8217;s small\n    <strong>\n     Map\n    <\/strong>\n    -based, per-address limiter is an observable teaching example only: it resets when the process restarts, is not shared across multiple servers, and can itself be used to block legitimate sign-in attempts. Real services need shared, risk-aware throttling, monitoring, resource limits and breach-password screening. Registration and recovery also need abuse protection.\n    <a href=\"#ref-1\">\n     [1]\n    <\/a>\n   <\/p>\n   <h3>\n    How the example creates a session\n   <\/h3>\n   <p>\n    This is the corresponding simplified excerpt from\n    <strong>\n     server.mjs\n    <\/strong>\n    .\n    <strong>\n     newToken()\n    <\/strong>\n    generates 32 random bytes;\n    <strong>\n     hash()\n    <\/strong>\n    computes a SHA-256 digest. The database stores\n    <strong>\n     hash(id)\n    <\/strong>\n    , while the browser receives the opaque\n    <strong>\n     id\n    <\/strong>\n    in its cookie.\n   <\/p>\n  <div aria-label=\"javascript example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">server.mjs<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">function createSession(userId = null) {<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;const id = newToken();<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;const csrf = newToken();<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;const now = Date.now();<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;db.prepare(&#8216;INSERT INTO sessions VALUES (?,?,?,?,?,?,?)&#8217;).run(<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;&nbsp;&nbsp;hash(id), userId, hash(csrf),<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;&nbsp;&nbsp;now, now, now + IDLE_MS, now + ABSOLUTE_MS<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;);<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;return { id, csrf };<\/span>\n      <span class=\"nit-code-line\">}<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    To inspect it, open the local SQLite database while the app is stopped. The session row contains a digest rather than the raw cookie value. That is not permission to share either value: both account databases and browser sessions are security-sensitive.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"cookies-title\" class=\"nit-section-panel\" id=\"cookies\">\n   <h2 id=\"cookies-title\">\n    Step 4 \u2014 Configure and understand session cookies\n   <\/h2>\n   <p>\n    A cookie is an HTTP mechanism for carrying small values between a browser and server. In this tutorial it carries the session ID. Cookie restrictions affect where that ID can travel and which browser interfaces can access it. The following is an\n    <strong>\n     illustrative HTTPS deployment configuration\n    <\/strong>\n    , not the exact local HTTP cookie from the lab.\n   <\/p>\n  <div aria-label=\"http example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">HTTP response<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">Set-Cookie: __Host-sid=opaque-random-value; Path=\/; Secure; HttpOnly; SameSite=Lax<\/span>\n    <\/div>\n  <\/div>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        Cookie setting\n       <\/th>\n       <th scope=\"col\">\n        Why it matters\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        Secure\n       <\/th>\n       <td>\n        Restricts the cookie to HTTPS requests (with browser-specific localhost exceptions).\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        HttpOnly\n       <\/th>\n       <td>\n        Prevents ordinary client-side JavaScript from reading the cookie through document.cookie.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        SameSite=Lax\n       <\/th>\n       <td>\n        Restricts many cross-site requests; not a complete CSRF defence.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Path=\/\n       <\/th>\n       <td>\n        Makes the cookie available for the intended site-wide path.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        No Domain\n       <\/th>\n       <td>\n        Keeps the cookie host-only rather than sharing it across subdomains.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        __Host- prefix\n       <\/th>\n       <td>\n        Requires Secure, Path=\/ and no Domain in supporting browsers.\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <p>\n    <strong>\n     Important:\n    <\/strong>\n    Setting Secure on an application that you are serving over ordinary remote HTTP can prevent the cookie from being sent. The lab uses a different non-production cookie name and an HTTP localhost connection. Any actual public deployment needs correctly configured HTTPS, a Secure cookie and carefully evaluated proxy trust settings. MDN documents the Set-Cookie attributes and host-only prefixes\n    <a href=\"#ref-3\">\n     [3]\n    <\/a>\n    .\n   <\/p>\n   <p>\n    <strong>\n     Read the actual local header:\n    <\/strong>\n    <strong>\n     nit_learning_sid=&lt;opaque-id&gt;; Path=\/; HttpOnly; SameSite=Lax\n    <\/strong>\n    . Unlike the illustrative HTTPS response above, this lab header does not use a\n    <strong>\n     __Host-\n    <\/strong>\n    prefix or a\n    <strong>\n     Secure\n    <\/strong>\n    attribute. In Chrome DevTools, compare that distinction directly. On a real HTTPS site, configure the host-only\n    <strong>\n     __Host-\n    <\/strong>\n    pattern where appropriate and validate that the deployed reverse proxy reports a trusted secure connection.\n    <a href=\"#ref-3\">\n     [3]\n    <\/a>\n    <a href=\"#ref-8\">\n     [8]\n    <\/a>\n   <\/p>\n   <h3>\n    Browser cookie expiry is not the same as server expiry\n   <\/h3>\n   <p>\n    The sample database enforces an\n    <strong>\n     idle limit of 20 minutes\n    <\/strong>\n    and an\n    <strong>\n     absolute limit of eight hours\n    <\/strong>\n    . These are examples for the practice application, not mandatory values for every site. A user active every few minutes may extend the idle timer, but cannot extend the absolute limit. Expiry decisions happen server-side; even if an old browser cookie remains, its expired session must be rejected. A production design should select timeouts according to the risks of its users and data.\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"protect-title\" class=\"nit-section-panel\" id=\"protect\">\n   <h2 id=\"protect-title\">\n    Step 5 \u2014 Protect API routes and state-changing requests\n   <\/h2>\n   <p>\n    A hidden dashboard button is not an access control. The backend must check the session for each protected endpoint and then check what that user is permitted to see. For example, viewing Mira&#8217;s private note must use the authenticated identity and a server-side ownership rule, not a note ID sent by an untrusted page.\n   <\/p>\n   <p>\n    The local lab demonstrates that its protected account endpoint returns a denial before login and succeeds after login. It also checks a synchroniser-style CSRF token on state-changing requests and verifies their Origin. Real applications must adapt the defensive checks to their architecture and protect against XSS as well.\n   <\/p>\n  <div aria-label=\"javascript example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">server.mjs<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">if (req.method === &#8216;GET&#8217; &amp;&amp; url.pathname === &#8216;\/api\/me&#8217;) {<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;if (!requireUser(session, res)) return;<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;const user = db.prepare(&#8216;SELECT id,email FROM users WHERE id=?&#8217;)<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;&nbsp;&nbsp;.get(session.user_id);<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;return send(res, 200, { user });<\/span>\n      <span class=\"nit-code-line\">}<\/span>\n    <\/div>\n  <\/div>\n   <h3>\n    Prove that two different users cannot access each other&#8217;s records\n   <\/h3>\n   <p>\n    The revised companion lab now includes a minimal\n    <strong>\n     practice_notes\n    <\/strong>\n    table,\n    <strong>\n     POST \/api\/notes\n    <\/strong>\n    to create a note, and\n    <strong>\n     GET \/api\/notes\/:id\n    <\/strong>\n    to read one. Only a signed-in user may create a note, and the server\n    <strong>\n     ignores any attempted\n     <strong>\n      owner_user_id\n     <\/strong>\n     in the request body\n    <\/strong>\n    . The owner comes from the authenticated session. The note lookup includes the authenticated account ID directly in its database query:\n   <\/p>\n  <div aria-label=\"sql example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">SQLite<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">&#8212; Used by the revised lab&#8217;s private-note lookup.<\/span>\n      <span class=\"nit-code-line\">SELECT id, note_text<\/span>\n      <span class=\"nit-code-line\">FROM practice_notes<\/span>\n      <span class=\"nit-code-line\">WHERE id = ? AND owner_user_id = ?;<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    The revised\n    <strong>\n     npm test\n    <\/strong>\n    sequence does something more rigorous than checking whether\n    <strong>\n     \/api\/me\n    <\/strong>\n    returns success. First Mira signs in and creates a note. Then Joel signs in successfully and requests\n    <strong>\n     Mira&#8217;s exact note ID\n    <\/strong>\n    . The server returns\n    <strong>\n     404\n    <\/strong>\n    because the note does not belong to Joel, even though Joel&#8217;s login is valid. After signing back in as Mira, the test confirms that she can read her own note but not Joel&#8217;s. An anonymous request returns\n    <strong>\n     401\n    <\/strong>\n    . These are distinct expected results:\n    <strong>\n     401\n    <\/strong>\n    identifies missing authentication, while a privacy-preserving\n    <strong>\n     404\n    <\/strong>\n    here avoids disclosing the existence of another account&#8217;s record.\n    <a href=\"#ref-10\">\n     [10]\n    <\/a>\n   <\/p>\n   <p>\n    This experiment demonstrates object-level authorisation without creating a full multi-user management system. Run\n    <strong>\n     npm test\n    <\/strong>\n    and read the assertions in\n    <strong>\n     tests.mjs\n    <\/strong>\n    ; change the ownership predicate deliberately in a disposable local copy only if you are studying how the negative test detects a mistake. Never disable ownership checks in a real application. OWASP recommends validating permissions on every request and creating regression tests for that logic.\n    <a href=\"#ref-10\">\n     [10]\n    <\/a>\n   <\/p>\n   <h3>\n    How to interpret the three most useful denial statuses\n   <\/h3>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        HTTP response\n       <\/th>\n       <th scope=\"col\">\n        What it means in this practice project\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         401 Unauthorized\n        <\/strong>\n       <\/th>\n       <td>\n        No valid authenticated session accompanies a protected request. Despite its name, this status concerns authentication.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         403 Forbidden\n        <\/strong>\n       <\/th>\n       <td>\n        The server rejects a state-changing request that fails the demo&#8217;s Origin or CSRF checks.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        <strong>\n         404 Not Found\n        <\/strong>\n       <\/th>\n       <td>\n        A private note is absent\n        <strong>\n         or\n        <\/strong>\n        belongs to another account; the response does not confirm that the other person&#8217;s note exists.\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <p>\n    The number alone cannot prove security: inspect which account made the request, which backend rule executed and what information the response contains.\n    <a href=\"#ref-10\">\n     [10]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"csrf-title\" class=\"nit-section-panel\" id=\"csrf\">\n   <h2 id=\"csrf-title\">\n    What prevents CSRF and session fixation?\n   <\/h2>\n   <p>\n    <strong>\n     Cross-site request forgery (CSRF)\n    <\/strong>\n    occurs when an attacker induces a browser to submit an unwanted action using credentials that the browser automatically attaches. For cookie-based sessions, a state-changing request should carry a server-validated protection such as an appropriate CSRF token, along with origin checks and other controls suited to the application.\n   <\/p>\n   <p>\n    A\n    <strong>\n     session fixation\n    <\/strong>\n    weakness occurs when an attacker can cause the victim to authenticate using a session identifier that the attacker already knows or can influence. Issuing a fresh identifier after login reduces this risk.\n   <\/p>\n   <p>\n    In this project, the browser first fetches a CSRF token from\n    <strong>\n     \/api\/csrf\n    <\/strong>\n    , then sends it in\n    <strong>\n     X-CSRF-Token\n    <\/strong>\n    for POST requests. The server also checks\n    <strong>\n     Origin\n    <\/strong>\n    . The token verifier is tied to the current server-side session.\n    <strong>\n     One lab-specific behaviour matters:\n    <\/strong>\n    each fresh request to\n    <strong>\n     \/api\/csrf\n    <\/strong>\n    replaces the earlier token for that session. With two tabs open, the token in one tab may become stale and yield a\n    <strong>\n     403\n    <\/strong>\n    ; refresh that tab&#8217;s token rather than disabling CSRF validation.\n   <\/p>\n   <p>\n    A CSRF token and\n    <strong>\n     SameSite=Lax\n    <\/strong>\n    address different parts of the problem. Neither prevents XSS, so the application must still treat user-controlled output safely and limit script capabilities. For public systems, prefer reviewed framework support and test all allowed origins and cross-origin configurations.\n    <a href=\"#ref-6\">\n     [6]\n    <\/a>\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"logout-title\" class=\"nit-section-panel\" id=\"logout\">\n   <h2 id=\"logout-title\">\n    Step 6 \u2014 Make logout actually end the session\n   <\/h2>\n   <p>\n    A visual\n    <strong>\n     Sign out\n    <\/strong>\n    button is insufficient if the server continues accepting the old session. Proper logout invalidates the server-side record and sends a response telling the browser to expire its cookie. The next protected request using the previous identifier must be denied.\n   <\/p>\n   <p>\n    Follow this sequence in DevTools: sign in, successfully request the protected endpoint, sign out, then request the protected endpoint again. The last request should fail. Closing a tab or browser is not a reliable replacement for server-side session expiration because mobile browsers and sessions may behave differently\n    <a href=\"#ref-2\">\n     [2]\n    <\/a>\n    .\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"recovery-title\" class=\"nit-section-panel\" id=\"recovery\">\n   <h2 id=\"recovery-title\">\n    Step 7 \u2014 Build a password-reset process that does not reveal accounts\n   <\/h2>\n   <p>\n    Password recovery is a separate security-sensitive workflow. The service must let legitimate users regain access without allowing attackers to guess accounts, forge recovery links or keep using an old session after a password change.\n   <\/p>\n   <h3>\n    1. Request\n   <\/h3>\n   <p>\n    User enters their address. Always return a generic message such as \u201cIf that account exists, recovery instructions will be sent.\u201d\n   <\/p>\n   <h3>\n    2. Create\n   <\/h3>\n   <p>\n    Generate an unpredictable random reset token, store a hash of it with an expiration time and associate it with one user.\n   <\/p>\n   <h3>\n    3. Deliver\n   <\/h3>\n   <p>\n    Send the reset link through an appropriate private channel. The localhost lab prints the link to its terminal for demonstration only.\n   <\/p>\n   <h3>\n    4. Consume\n   <\/h3>\n   <p>\n    Verify expiry and single-use status, store the newly hashed password, revoke old sessions and do not log the user in automatically.\n   <\/p>\n   <p>\n    The lab&#8217;s token lasts\n    <strong>\n     15 minutes\n    <\/strong>\n    , is stored as a hash, and is consumed at most once within a database transaction. This duration is a design choice, not a universal standard. The reset link contains the token in a URL\n    <strong>\n     fragment\n    <\/strong>\n    (\n    <strong>\n     #token=&#8230;\n    <\/strong>\n    ), which a browser does not send in the initial HTTP request. The revised local page reads the token into temporary JavaScript memory, immediately removes the fragment from the visible address using\n    <strong>\n     history.replaceState\n    <\/strong>\n    , and then submits it in the reset request. The token is still sensitive while in page memory; this approach is not a substitute for removing untrusted scripts and using HTTPS. The application deletes the user&#8217;s old sessions when the password changes, and it does not silently log them back in.\n    <a href=\"#ref-7\">\n     [7]\n    <\/a>\n   <\/p>\n  <div aria-label=\"javascript example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">server.mjs<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">const token = newToken();<\/span>\n      <span class=\"nit-code-line\">db.prepare(&#8216;DELETE FROM reset_tokens WHERE user_id=?&#8217;).run(user.id);<\/span>\n      <span class=\"nit-code-line\">db.prepare(&#8216;INSERT INTO reset_tokens VALUES(?,?,?)&#8217;)<\/span>\n      <span class=\"nit-code-line\">&nbsp;&nbsp;.run(hash(token), user.id, Date.now() + RESET_MS);<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    <strong>\n     Exercise:\n    <\/strong>\n    request a recovery link for a practice address, copy the local-only link from your terminal, choose a new practice password, and try the same link twice. The second use must fail. Then try the old password: it must also fail.\n   <\/p>\n   <p>\n    A recovery link is a sensitive credential\n   <\/p>\n   <p>\n    Never include a reset token in analytics, third-party scripts, routine application logs or screenshots. Build recovery URLs from a trusted configured origin, use HTTPS for actual delivery, and set a restrictive Referrer-Policy. A successful reset should not silently sign in the user. See OWASP Forgot Password guidance\n    <a href=\"#ref-7\">\n     [7]\n    <\/a>\n    .\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"testing-title\" class=\"nit-section-panel\" id=\"testing\">\n   <h2 id=\"testing-title\">\n    Step 8 \u2014 Run the acceptance tests before trusting the feature\n   <\/h2>\n   <p>\n    Successful login is only one test. You must also prove that incorrect credentials, missing sessions, invalid recovery tokens and cross-origin requests are denied. The companion project includes an automated Node.js integration test you can run with the following command.\n   <\/p>\n  <div aria-label=\"bash example\" class=\"nit-code-window\">\n    <div class=\"nit-code-window__bar\">\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__dot\"><\/span>\n      <span class=\"nit-code-window__file\">Terminal<\/span>\n    <\/div>\n    <div class=\"nit-code-window__body\">\n      <span class=\"nit-code-line\">npm test<\/span>\n    <\/div>\n  <\/div>\n   <p>\n    The expanded companion project integration test was run on 16 November 2026 with Node.js 22.16.0 and passed. It checks the behaviours listed below; it does not claim to cover every possible security scenario. Run it again on your own computer after changing the code. The suite starts a separate server with a temporary SQLite database, then removes its test data.\n   <\/p>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        What to test\n       <\/th>\n       <th scope=\"col\">\n        Expected result\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        Register a fictional account\n       <\/th>\n       <td>\n        User record saved with a salted password hash.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Submit a too-short Unicode password\n       <\/th>\n       <td>\n        HTTP 400; eight supplementary characters must not be mistaken for 16 code points.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Sign in with canonically equivalent Unicode password forms\n       <\/th>\n       <td>\n        Succeeds after consistent NFC normalisation.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Request protected data before login\n       <\/th>\n       <td>\n        HTTP 401: no authorised session.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Request Mira&#8217;s note while signed in as Joel\n       <\/th>\n       <td>\n        HTTP 404: another user&#8217;s record remains unavailable.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Supply a fake owner ID while creating Joel&#8217;s note\n       <\/th>\n       <td>\n        Server uses Joel&#8217;s authenticated session as the owner.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Enter wrong password\n       <\/th>\n       <td>\n        HTTP 401 with a generic failure response.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Sign in correctly\n       <\/th>\n       <td>\n        Authenticated session; session ID rotated.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Sign out\n       <\/th>\n       <td>\n        Former session no longer authorises requests.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Submit without CSRF token\n       <\/th>\n       <td>\n        HTTP 403; action rejected.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Reuse a previous CSRF token after\n        <strong>\n         \/api\/csrf\n        <\/strong>\n        rotation\n       <\/th>\n       <td>\n        HTTP 403; an outdated token is rejected in this lab.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Submit from the wrong origin\n       <\/th>\n       <td>\n        HTTP 403; action rejected.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Request recovery for unknown address\n       <\/th>\n       <td>\n        Same public message as for an existing address.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Use incorrect or reused reset token\n       <\/th>\n       <td>\n        Password change rejected.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Reset password correctly\n       <\/th>\n       <td>\n        Old password and existing sessions invalidated.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Expire the session in the database\n       <\/th>\n       <td>\n        Old session denied even if browser has cookie.\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <h3>\n    Inspect the evidence yourself\n   <\/h3>\n   <p>\n    Open the application in your browser and launch DevTools \u2192\n    <strong>\n     Network\n    <\/strong>\n    . Clear the log, sign in and select the\n    <strong>\n     \/api\/login\n    <\/strong>\n    request. Check that the response is\n    <strong>\n     200\n    <\/strong>\n    and a new\n    <strong>\n     Set-Cookie\n    <\/strong>\n    header was issued. Then open\n    <strong>\n     Application\/Storage \u2192 Cookies\n    <\/strong>\n    to see\n    <strong>\n     HttpOnly\n    <\/strong>\n    and\n    <strong>\n     SameSite=Lax\n    <\/strong>\n    . The\n    <em>\n     local HTTP\n    <\/em>\n    cookie deliberately does not carry\n    <strong>\n     Secure\n    <\/strong>\n    ; do not mistake that for a recommendation for public sites.\n   <\/p>\n   <p>\n    Next click\n    <strong>\n     Sign out\n    <\/strong>\n    , then\n    <strong>\n     Check protected endpoint\n    <\/strong>\n    . You should see\n    <strong>\n     401\n    <\/strong>\n    or the equivalent not-signed-in outcome, even if you manually retain a previous cookie. Finally, test missing CSRF tokens, expired\/reset-used links and an invalid origin using the automated suite. On narrow screens, check the app&#8217;s labels, buttons and keyboard focus\u2014not just whether the page happens to fit. Never share raw cookie IDs or recovery URLs.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"troubleshooting-title\" class=\"nit-section-panel\" id=\"troubleshooting\">\n   <h2 id=\"troubleshooting-title\">\n    Why does a login system fail? A debugging map\n   <\/h2>\n   <p>\n    Use evidence from the browser&#8217;s Network panel, the application&#8217;s own development logs and database state. Do not repeatedly change security settings until a failed login \u201cstarts working\u201d without understanding what changed.\n   <\/p>\n   <div aria-label=\"Scrollable comparison table\" class=\"nit-table-wrap\" role=\"region\" tabindex=\"0\">\n    <table class=\"nit-table\">\n     <thead>\n      <tr>\n       <th scope=\"col\">\n        Symptom\n       <\/th>\n       <th scope=\"col\">\n        What to check first\n       <\/th>\n      <\/tr>\n     <\/thead>\n     <tbody>\n      <tr>\n       <th scope=\"row\">\n        Login succeeds but next page says signed out\n       <\/th>\n       <td>\n        Was the cookie set, retained and returned? Check SameSite, domain, path and HTTPS mode.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Cookie disappears in deployment\n       <\/th>\n       <td>\n        Check Secure and the actual HTTPS\/proxy configuration.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        403 on a legitimate form\n       <\/th>\n       <td>\n        Confirm that a second tab has not rotated the lab&#8217;s CSRF token; re-fetch\n        <strong>\n         \/api\/csrf\n        <\/strong>\n        and check\n        <strong>\n         Origin\n        <\/strong>\n        .\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        User sees another user\u2019s data\n       <\/th>\n       <td>\n        Inspect the server-side authorisation rule, never merely the frontend visibility.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Valid login but missing notes\n       <\/th>\n       <td>\n        Check whether the note belongs to the signed-in account; do not treat a 404 as proof of missing authentication.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Reset link immediately fails\n       <\/th>\n       <td>\n        Check that the entire\n        <strong>\n         #token=\n        <\/strong>\n        fragment was copied, that the token is unused, and that the database expiry has not passed.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Reset works but old device remains signed in\n       <\/th>\n       <td>\n        Confirm all relevant server-side sessions were revoked.\n       <\/td>\n      <\/tr>\n      <tr>\n       <th scope=\"row\">\n        Password appears in logs\n       <\/th>\n       <td>\n        Remove credential logging and investigate whether logs or telemetry need protection.\n       <\/td>\n      <\/tr>\n     <\/tbody>\n    <\/table>\n   <\/div>\n   <p class=\"nit-table-note\">\n    On a phone, swipe the table sideways to view both columns.\n   <\/p>\n   <p>\n    Do not \u201cfix\u201d cookies by disabling security\n   <\/p>\n   <p>\n    If a Secure cookie fails under local HTTP, use the intentional local-only configuration. For public traffic, restore HTTPS and Secure cookie settings, then test the proxy and origin correctly. Turning off CSRF or HttpOnly to silence an error hides the underlying defect.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"real-world-title\" class=\"nit-section-panel\" id=\"real-world\">\n   <h2 id=\"real-world-title\">\n    Where would you use the same authentication design?\n   <\/h2>\n   <p>\n    The general architecture applies to many different applications, but each also needs its own authorisation rules:\n   <\/p>\n   <ol>\n    <li>\n     <strong>\n      School portal:\n     <\/strong>\n     Mira signs in to view her results. Only her own results may be returned, while authorised teachers have different permissions.\n    <\/li>\n    <li>\n     <strong>\n      Salon appointment system:\n     <\/strong>\n     A stylist signs in to see permitted bookings. A customer cannot access another customer&#8217;s private appointment history.\n    <\/li>\n    <li>\n     <strong>\n      Plant-equipment maintenance app:\n     <\/strong>\n     A technician records work done on a machine. Only permitted supervisors can approve certain changes.\n    <\/li>\n    <li>\n     <strong>\n      Online shop administration:\n     <\/strong>\n     A shop owner manages stock and orders; ordinary buyers must not be able to use administrative endpoints.\n    <\/li>\n   <\/ol>\n   <p>\n    The login mechanism identifies the account.\n    <strong>\n     It does not replace application-specific roles, permissions, ownership rules, audit trails or transaction controls.\n    <\/strong>\n    Design those rules before connecting confidential records.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"production-title\" class=\"nit-section-panel\" id=\"production\">\n   <h2 id=\"production-title\">\n    Is the example ready for production?\n   <\/h2>\n   <p>\n    <strong>\n     No.\n    <\/strong>\n    The local project demonstrates the required moving parts and has automated acceptance checks, but a real login service requires additional design, infrastructure, monitoring and independent security validation. Do not deploy the provided raw HTTP teaching server for public users.\n   <\/p>\n   <h3>\n    Transport and cookies\n   <\/h3>\n   <p>\n    HTTPS, HSTS, Secure cookies, correct reverse-proxy trust and reliable production session storage.\n   <\/p>\n   <h3>\n    Account protection\n   <\/h3>\n   <p>\n    MFA or passkeys where appropriate, real compromised-password screening, risk-aware rate limiting and secure email verification.\n   <\/p>\n   <h3>\n    Recovery and operations\n   <\/h3>\n   <p>\n    Reliable email delivery, audit logs that avoid secrets, abuse monitoring, backup protection and incident procedures.\n   <\/p>\n   <h3>\n    Review and testing\n   <\/h3>\n   <p>\n    Maintained dependencies, framework security updates, CSRF\/XSS assessment, authorisation tests and independent security review.\n   <\/p>\n   <p>\n    The project deliberately uses Node&#8217;s built-in HTTP server to make authentication logic visible, and its attempt limiter is an in-memory teaching control. Real applications should normally build upon a maintained authentication framework and session library rather than hand-writing every security primitive. The\n    <a href=\"https:\/\/expressjs.com\/en\/resources\/middleware\/session\/\" target=\"_blank\" rel=\"noopener\">\n     Express session documentation\n    <\/a>\n    warns that its default MemoryStore is not designed for production\n    <a href=\"#ref-8\">\n     [8]\n    <\/a>\n    .\n   <\/p>\n   <p>\n    What does \u201csecure enough\u201d mean?\n   <\/p>\n   <p>\n    It depends on the application, its data, its threat model and its operational environment. Passing the included local checks confirms selected behaviours; it cannot certify the complete system against every attack.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"faq-title\" class=\"nit-section-panel\" id=\"faq\">\n   <h2 id=\"faq-title\">\n    Frequently asked questions about login, cookies and sessions\n   <\/h2>\n   <h3>\n    What is the difference between a cookie and a session?\n   <\/h3>\n   <p>\n    A cookie is a browser mechanism that carries a small value with eligible HTTP requests. In this server-side design, the cookie carries an opaque ID; a server-side session record stores the authenticated state and expiry controls.\n   <\/p>\n   <h3>\n    Is a password hash the same as encryption?\n   <\/h3>\n   <p>\n    No. Encryption is reversible with an appropriate key. Password hashing uses a one-way, deliberately expensive process with a unique salt so the verifier can check a password without storing it in plaintext.\n   <\/p>\n   <h3>\n    Should I store login tokens in localStorage?\n   <\/h3>\n   <p>\n    For this cookie-session design, no. An appropriately restricted HttpOnly cookie helps prevent JavaScript from directly reading the session identifier. You still need XSS prevention and CSRF defences.\n   <\/p>\n   <h3>\n    Are HttpOnly, Secure and SameSite enough to stop every attack?\n   <\/h3>\n   <p>\n    No. They reduce specific risks. They do not replace server-side authorisation, CSRF validation, XSS prevention, TLS configuration, secure recovery, logging discipline or monitoring.\n   <\/p>\n   <h3>\n    Does every login system need JWT?\n   <\/h3>\n   <p>\n    No. Many web applications work well with server-managed sessions and cookies. JWTs may suit some architectures but introduce their own storage, expiry, validation and revocation considerations.\n   <\/p>\n   <h3>\n    Does clicking Logout remove every browser and device session?\n   <\/h3>\n   <p>\n    Not necessarily. A normal logout may revoke only the current session. An explicit \u201csign out all devices\u201d action or password-recovery policy should revoke other sessions when required.\n   <\/p>\n   <h3>\n    Why do password-reset tokens expire?\n   <\/h3>\n   <p>\n    Expiry reduces the window during which a captured recovery link could be used. Tokens must also be unpredictable and single-use; expiration alone is insufficient.\n   <\/p>\n   <h3>\n    Can I publish the teaching application to a live website?\n   <\/h3>\n   <p>\n    Do not publish this particular demo. It runs under deliberate localhost restrictions and lacks a complete production security, deployment and operations programme.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"related-title\" class=\"nit-section-panel\" id=\"related\">\n   <h2 id=\"related-title\">\n    Keep learning: from web requests to real authentication\n   <\/h2>\n   <p>\n    For the foundations, start with NIT&#8217;s\n    <a href=\"https:\/\/niteducation.com\/resources\/how-to-build-your-first-web-app\/\">\n     first web application guide\n    <\/a>\n    , then read\n    <a href=\"https:\/\/niteducation.com\/resources\/what-is-an-api-in-web-development\/\">\n     how frontend and backend APIs communicate\n    <\/a>\n    . For the security-testing perspective, see\n    <a href=\"https:\/\/niteducation.com\/resources\/how-do-ethical-hackers-find-website-vulnerabilities\/\">\n     how ethical hackers investigate website vulnerabilities with permission\n    <\/a>\n    . Those topics provide foundations rather than replacing this authentication project.\n   <\/p>\n   <p>\n    <strong>\n     Extension challenge:\n    <\/strong>\n    The revised lab already includes a\n    <strong>\n     practice_notes\n    <\/strong>\n    table with\n    <strong>\n     owner_user_id\n    <\/strong>\n    and tests direct cross-user lookups. Extend it by adding note editing, then write tests proving that Joel cannot update Mira&#8217;s note or change its owner by putting another account ID in a request. Test the denied cases\n    <strong>\n     before\n    <\/strong>\n    you consider the feature complete. This new editing extension is not implemented in the accompanying lab.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"references-title\" class=\"nit-section-panel\" id=\"references\">\n   <h2 id=\"references-title\">\n    Technical references and further reading\n   <\/h2>\n   <ol>\n    <li id=\"ref-1\">\n     <a href=\"https:\/\/cheatsheetseries.owasp.org\/cheatsheets\/Authentication_Cheat_Sheet.html\" target=\"_blank\" rel=\"noopener\">\n      OWASP \u2014 Authentication Cheat Sheet\n     <\/a>\n     . Authentication errors, rate controls and reauthentication.\n    <\/li>\n    <li id=\"ref-2\">\n     <a href=\"https:\/\/cheatsheetseries.owasp.org\/cheatsheets\/Session_Management_Cheat_Sheet.html\" target=\"_blank\" rel=\"noopener\">\n      OWASP \u2014 Session Management Cheat Sheet\n     <\/a>\n     . Session identifiers, rotation and expiry.\n    <\/li>\n    <li id=\"ref-3\">\n     <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Reference\/Headers\/Set-Cookie\" target=\"_blank\" rel=\"noopener\">\n      MDN \u2014 Set-Cookie Header\n     <\/a>\n     . Cookie attributes and prefixes.\n    <\/li>\n    <li id=\"ref-4\">\n     <a href=\"https:\/\/pages.nist.gov\/800-63-4\/sp800-63b.html\" target=\"_blank\" rel=\"noopener\">\n      NIST \u2014 SP 800-63B\n     <\/a>\n     . Current password verifier and authentication guidance.\n    <\/li>\n    <li id=\"ref-5\">\n     <a href=\"https:\/\/cheatsheetseries.owasp.org\/cheatsheets\/Password_Storage_Cheat_Sheet.html\" target=\"_blank\" rel=\"noopener\">\n      OWASP \u2014 Password Storage Cheat Sheet\n     <\/a>\n     . Argon2id, scrypt, salts and hashing trade-offs.\n    <\/li>\n    <li id=\"ref-6\">\n     <a href=\"https:\/\/cheatsheetseries.owasp.org\/cheatsheets\/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html\" target=\"_blank\" rel=\"noopener\">\n      OWASP \u2014 CSRF Prevention Cheat Sheet\n     <\/a>\n     . CSRF tokens and request validation.\n    <\/li>\n    <li id=\"ref-7\">\n     <a href=\"https:\/\/cheatsheetseries.owasp.org\/cheatsheets\/Forgot_Password_Cheat_Sheet.html\" target=\"_blank\" rel=\"noopener\">\n      OWASP \u2014 Forgot Password Cheat Sheet\n     <\/a>\n     . Single-use reset tokens and account privacy.\n    <\/li>\n    <li id=\"ref-8\">\n     <a href=\"https:\/\/expressjs.com\/en\/resources\/middleware\/session\/\" target=\"_blank\" rel=\"noopener\">\n      Express \u2014 Session Middleware Documentation\n     <\/a>\n     . Production storage and cookie settings.\n    <\/li>\n    <li id=\"ref-9\">\n     <a href=\"https:\/\/niteducation.com\/resources\/full-stack-software-development-course\/\">\n      Newton Institute of Technology \u2014 Full-Stack Software Development Course\n     <\/a>\n     . Verified course details and admissions route.\n    <\/li>\n    <li id=\"ref-10\">\n     <a href=\"https:\/\/cheatsheetseries.owasp.org\/cheatsheets\/Authorization_Cheat_Sheet.html\" target=\"_blank\" rel=\"noopener\">\n      OWASP \u2014 Authorization Cheat Sheet\n     <\/a>\n     . Deny-by-default, per-request permissions and regression testing.\n    <\/li>\n   <\/ol>\n   <p>\n    Sources reviewed 16 November 2026. Educational guidance may need revision as official recommendations and libraries change.\n   <\/p>\n  <\/section>\n  <section aria-labelledby=\"learn-with-nit-title\" class=\"nit-enrol\" id=\"learn-with-nit\">\n   <h2 id=\"learn-with-nit-title\">\n    <span class=\"nit-pg-hero__line\">Learn full-stack development at Newton Institute of Technology<\/span>\n   <\/h2>\n   <p class=\"nit-enrol__eyebrow\">\n    Practical ICT training\n   <\/p>\n   <article class=\"nit-enrol-card\">\n    <p>\n     Building authentication connects HTML forms, browser networking, backend logic, databases, access control and software testing. NIT&#8217;s\n    <strong>Full-Stack Software Development programme<\/strong>\n    includes practical work across these areas. The currently published pathway is\n    <strong>\n     8 months of institutional training\n    <\/strong>\n    , followed by\n    <strong>\n     at least 3 months of industrial attachment\n    <\/strong>\n    . Refer to the official course page for current programme terms and admissions.\n    \n    <\/p>\n    <p>\n     <a class=\"nit-pg-button nit-pg-button--outline\" href=\"#ref-9\">Reference [9]<\/a>\n    <\/p>\n   <\/article>\n   <p>\n    <strong>\n     Prepared by:\n    <\/strong>\n    Newton Institute of Technology Editorial Team. Technical exercises were checked with Node.js 22.16.0; institutional expert review and public hosting verification remain separate publication requirements.\n   <\/p>\n   <p>\n    <strong>\n     Newton Institute of Technology\n    <\/strong>\n    \u00b7 Migori Town, Kenya \u00b7 For Kenya, Africa and the world.\n   <\/p>\n   <div class=\"nit-pg-hero__actions\">\n    <a class=\"nit-pg-button nit-pg-button--gold\" href=\"https:\/\/niteducation.com\/resources\/full-stack-software-development-course\/\">\n     Explore NIT full-stack training\n    <\/a>\n    <a class=\"nit-pg-button nit-pg-button--outline\" href=\"https:\/\/niteducation.com\/\">\n     Visit NIT\n    <\/a>\n   <\/div>\n  <\/section>\n <\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Software Development Guide Build a Secure Login System: Sessions, Cookies and Password Reset Explained Register \u00b7 Sign In \u00b7 Protect \u00b7 Recover Understand passwords, session cookies, protected requests and recovery by examining one working local application\u2014and verifying its failure cases. Start the practical build Run the security tests Authentication lab LOGIN AUTH RESET Browser \u2192 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[30],"tags":[877,879,878],"class_list":["post-3898","post","type-post","status-publish","format-standard","hentry","category-ict","tag-how-to-build-a-secure-login-system","tag-secure-session-cookies","tag-session-based-authentication"],"_links":{"self":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts\/3898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/comments?post=3898"}],"version-history":[{"count":2,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts\/3898\/revisions"}],"predecessor-version":[{"id":3900,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/posts\/3898\/revisions\/3900"}],"wp:attachment":[{"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/media?parent=3898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/categories?post=3898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/niteducation.com\/resources\/wp-json\/wp\/v2\/tags?post=3898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}