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: src/routes/solid-start/advanced/session.mdx
+19-26Lines changed: 19 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,40 +6,35 @@ Sessions allow web applications to maintain state between user requests.
6
6
Since HTTP is stateless, each request is treated independently.
7
7
Sessions address this by allowing the server to recognize multiple requests from the same user, which is helpful for tracking authentication and preferences.
8
8
9
-
10
9
## How sessions work
11
10
12
11
A session typically involves:
13
12
14
13
1.**Session creation**: When tracking is needed (e.g., upon login or first visit), the server creates a session.
15
-
This involves:
16
-
- Generating a unique **session ID**.
17
-
- Storing the session data _encrypted and signed_ within a cookie.
14
+
This involves generating a unique **session ID** and storing the session data, _encrypted and signed_, within a cookie.
18
15
2.**Session cookie transmission**: The server sends a `Set-Cookie` HTTP header.
19
16
This instructs the browser to store the session cookie.
20
17
3.**Subsequent requests**: The browser automatically includes the session cookie in the `Cookie` HTTP header for requests to the server.
21
18
4.**Session retrieval and data access**: For each request, the server:
22
-
- Checks for the session cookie.
23
-
- Retrieves session data from the cookie.
24
-
- Decrypts and verifies the signature of the session data.
25
-
- Application code can access and use this data.
26
-
5.**Session expiration and destruction**: Sessions typically expire after a time or upon user logout.
27
-
Destruction involves instructing the browser to delete the session cookie (e.g., setting an expired `Set-Cookie`).
19
+
1. Checks for the session cookie.
20
+
2. Retrieves the session data if a cookie is present.
21
+
3. Decrypts and verifies the signature of the session data for the application to access and use this data.
22
+
5.**Session expiration and destruction**: Sessions typically expire after a period of time or upon user sign-out and the data is removed.
23
+
This is done by setting a `Max-Age` attribute on the cookie or by sending a `Set-Cookie` HTTP header with an expired date.
28
24
29
-
Most of these steps are automatically managed by [session helpers](#session-helpers).
25
+
Most of these steps are automatically managed by the [session helpers](#session-helpers).
30
26
31
27
### Database sessions
32
28
33
29
For larger applications or when more robust session management is required, SolidStart also supports storing session data in a database.
34
30
This approach is similar to the cookie-based approach, but with some key differences:
35
31
36
-
1. A session ID is generated.
37
-
2. The session data is stored in the database, associated with the session ID.
38
-
3. Only the session ID is stored in the cookie.
39
-
4. The session data is retrieved from the database using the session ID.
40
-
5. Upon expiration, in addition to the session cookie, the database record containing the session data is also removed.
32
+
- The session data is stored in the database, associated with the session ID.
33
+
- Only the session ID is stored in the cookie, not the session data.
34
+
- The session data is retrieved from the database using the session ID, instead of being retrieved directly from the cookie.
35
+
- Upon expiration, in addition to the session cookie, the database record containing the session data is also removed.
41
36
42
-
Note that managing interactions with the database is not directly supported by SolidStart; you will need to implement this yourself.
37
+
SolidStart does not automatically handle interactions with a database; you need to implement this yourself.
43
38
44
39
## Session helpers
45
40
@@ -89,7 +84,7 @@ In this example, the `useThemeSession` server function creates a session that st
89
84
90
85
`useSession` requires a strong password for encrypting and signing the session cookie.
91
86
This password must be at least 32 characters long and should be kept highly secure.
92
-
It is strongly recommended to store this password in an environment variable, as shown in the example above, rather than hardcoding it in your source code.
87
+
It is strongly recommended to store this password in a [private environment variable](/configuration/environment-variables#private-environment-variables), as shown in the example above, rather than hardcoding it in your source code.
93
88
94
89
A password can be generated using the following command:
95
90
@@ -100,9 +95,9 @@ openssl rand -base64 32
100
95
`useSession` adds a `Set-Cookie` HTTP header to the current server response.
101
96
By default, the cookie is named `h3`, but can be customized with the `name` option, as shown in the example above.
102
97
103
-
## Getting session data
98
+
## Getting the session data
104
99
105
-
The `useSession` helper provides access to the session data through the `data` property.
100
+
The `useSession` helper provides access to the session data from the current request with the `data` property.
106
101
107
102
```ts title="src/lib/session.ts"
108
103
exportasyncfunction getThemeSession() {
@@ -113,28 +108,26 @@ export async function getThemeSession() {
113
108
}
114
109
```
115
110
116
-
## Updating session data
111
+
## Updating the session data
117
112
118
-
The `useSession` helper provides an`update` method to modify session data.
113
+
The `useSession` helper provides the`update` method to update the session data from the current request.
0 commit comments