> For the complete documentation index, see [llms.txt](https://program.hackyourfuture.dk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://program.hackyourfuture.dk/course-content/backend/node/week3/session-materials/13-auth-sessions.md).

# 13. Auth - Sessions

In this part, you will add **session-based authentication** to the Snippets API and compare it to your JWT-based approach.

We will:

* Configure `express-session`.
* Implement login and logout using sessions.
* Protect at least one Snippets API route with session-based middleware.

## 1. Configure express-session

* Install `express-session` in your project if it is not already installed.
* In your main application file (e.g. `app.js`), add the session middleware with:
  * A `secret` value (for local development you can keep it simple).
  * Reasonable `resave` and `saveUninitialized` settings.

## 2. Implement login with sessions

Create a route (for example in `routes/auth-session.js`) that:

1. Reads `username` and `password` from the request body.
2. Looks up the user and verifies the password using bcrypt (re-using your secure users table).
3. Sets `req.session.userId` when the login is successful.
4. Returns a small success payload (e.g. `{ "message": "Logged in with session" }`).

## 3. Protect routes with session middleware

Create a middleware function (for example `requireSessionAuth`) that:

1. Checks if `req.session.userId` is set.
2. Calls `next()` if it is.
3. Sends a `401` response with a short JSON error if it is not.

Use this middleware to protect at least one Snippets API route (for example, a route that creates or deletes a snippet).

## 4. Implement logout

Add a `/logout-session` route that:

* Destroys the current session (e.g. via `req.session.destroy`).
* Returns a simple JSON response to confirm logout.

## 5. Suggested exercises

* Compare your session-based solution with your JWT-based solution:
  * What changes in the client’s behaviour?
  * How does each solution handle revocation?
  * What would you need to consider when scaling beyond a single server instance?


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://program.hackyourfuture.dk/course-content/backend/node/week3/session-materials/13-auth-sessions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
