You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/latest/examples/session-management.md
+36-14Lines changed: 36 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,27 +3,39 @@ description: |
3
3
How to manage a session using cookies.
4
4
---
5
5
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.
7
9
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.
10
15
11
16
## Setup
12
17
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.
14
20
2. Add the `http`-package from the standard library:
15
21
```sh
16
22
deno add jsr:@std/http
17
23
```
18
24
19
25
## Implementation
20
26
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
to generate a random UUID and the [`@std/http`](https://jsr.io/@std/http)
31
+
package to evaluate the cookies.
22
32
23
-
### Creating a *session* cookie
33
+
> It is
34
+
35
+
### Creating a _session_ cookie
24
36
25
37
```ts
26
-
import { setCookie } from'@std/http';
38
+
import { setCookie } from"@std/http";
27
39
28
40
const headers =newHeaders();
29
41
const uuid =crypto.randomUUID();
@@ -33,28 +45,35 @@ setCookie(headers, {
33
45
});
34
46
```
35
47
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`.
37
50
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.
39
53
40
-
### Retrieving a *session* cookie
54
+
### Retrieving a _session_ cookie
41
55
42
56
```ts
43
-
import { getCookies } from'@std/http';
57
+
import { getCookies } from"@std/http";
44
58
45
59
/** arbitrary headers-object used to showcase API */
46
60
const cookies =getCookies(headers);
47
61
const uuid =cookies["session"];
48
62
```
49
63
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.
51
67
52
68
## Solution
53
69
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`.
55
74
56
75
```ts main.ts
57
-
import { getCookie, setCookie } from'@std/http';
76
+
import { getCookie, setCookie } from"@std/http";
58
77
59
78
/** session middleware */
60
79
app.use(async (ctx) => {
@@ -72,3 +91,6 @@ app.use(async (ctx) => {
72
91
returnresponse;
73
92
});
74
93
```
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