Skip to content

Commit a509f7b

Browse files
committed
chore: fix formatting
1 parent 69ebf3c commit a509f7b

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

docs/latest/examples/session-management.md

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,39 @@ description: |
33
How to manage a session using cookies.
44
---
55

6-
This example is based on a [MDN-Guide on HTTP-Cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies). Check it out for further information.
6+
This example is based on a
7+
[MDN-Guide on HTTP-Cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies).
8+
Check it out for further information.
79

8-
Why do we want to *manage a session*? A *session* allows us to keep track of e.g. a user or a shoppingcart. Basically it can be used to associate data with an identifier, or even contain data itself.
9-
Cookies are the most common solution for session management. Since [Deno](https://deno.land) uses standard WebAPIs like `Request` & `Response`, it is quite easy to manage a session using cookies.
10+
Why do we want to _manage a session_? A _session_ allows us to keep track of
11+
e.g. a user or a shoppingcart. Basically it can be used to associate data with
12+
an identifier, or even contain data itself. Cookies are the most common solution
13+
for session management. Since [Deno](https://deno.land) uses standard WebAPIs
14+
like `Request` & `Response`, it is quite easy to manage a session using cookies.
1015

1116
## Setup
1217

13-
1. Create a [new fresh-project](https://fresh.deno.dev/docs/getting-started) or use your own.
18+
1. Create a [new fresh-project](https://fresh.deno.dev/docs/getting-started) or
19+
use your own.
1420
2. Add the `http`-package from the standard library:
1521
```sh
1622
deno add jsr:@std/http
1723
```
1824

1925
## Implementation
2026

21-
To a achieve the most basic implementation of session management we can associate a random UUID with a request. We will use the global [`crypto`](https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto)-object to generate a random UUID and the [`@std/http`](https://jsr.io/@std/http) package to evaluate the cookies.
27+
To a achieve the most basic implementation of session management we can
28+
associate a random UUID with a request. We will use the global
29+
[`crypto`](https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto)-object
30+
to generate a random UUID and the [`@std/http`](https://jsr.io/@std/http)
31+
package to evaluate the cookies.
2232

23-
### Creating a *session* cookie
33+
> It is
34+
35+
### Creating a _session_ cookie
2436

2537
```ts
26-
import { setCookie } from '@std/http';
38+
import { setCookie } from "@std/http";
2739

2840
const headers = new Headers();
2941
const uuid = crypto.randomUUID();
@@ -33,28 +45,35 @@ setCookie(headers, {
3345
});
3446
```
3547

36-
This script will create a HTTP headers-object and assign a cookie named *session* with a random UUID as its value using `setCookie`.
48+
This script will create a HTTP headers-object and assign a cookie named
49+
_session_ with a random UUID as its value using `setCookie`.
3750

38-
Adding this headers-object to a response will make the browser attach the cookie to the headers in every request.
51+
Adding this headers-object to a response will make the browser attach the cookie
52+
to the headers in every request.
3953

40-
### Retrieving a *session* cookie
54+
### Retrieving a _session_ cookie
4155

4256
```ts
43-
import { getCookies } from '@std/http';
57+
import { getCookies } from "@std/http";
4458

4559
/** arbitrary headers-object used to showcase API */
4660
const cookies = getCookies(headers);
4761
const uuid = cookies["session"];
4862
```
4963

50-
By using `getCookie` we can retrieve the cookies of a headers-object as a `Record<string, string>`. To access the value of a cookie we can index the record by the cookie name as a key.
64+
By using `getCookie` we can retrieve the cookies of a headers-object as a
65+
`Record<string, string>`. To access the value of a cookie we can index the
66+
record by the cookie name as a key.
5167

5268
## Solution
5369

54-
We can use a middleware to get the `Request` from the `Context` and create a `Response` for it. Using the previously mentioned methods we retrieve the cookies from the `Request`. If there is no *session* cookie already available, we will create a *session* cookie and add it to the `Response`.
70+
We can use a middleware to get the `Request` from the `Context` and create a
71+
`Response` for it. Using the previously mentioned methods we retrieve the
72+
cookies from the `Request`. If there is no _session_ cookie already available,
73+
we will create a _session_ cookie and add it to the `Response`.
5574

5675
```ts main.ts
57-
import { getCookie, setCookie } from '@std/http';
76+
import { getCookie, setCookie } from "@std/http";
5877

5978
/** session middleware */
6079
app.use(async (ctx) => {
@@ -72,3 +91,6 @@ app.use(async (ctx) => {
7291
return response;
7392
});
7493
```
94+
95+
> [info]: This is the most basic implementation. Expanding on this solution
96+
> could mean adding a database to relate data to a session.

0 commit comments

Comments
 (0)