Skip to content

Commit 83b8ff7

Browse files
committed
update
1 parent 924aa81 commit 83b8ff7

1 file changed

Lines changed: 19 additions & 26 deletions

File tree

src/routes/solid-start/advanced/session.mdx

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,35 @@ Sessions allow web applications to maintain state between user requests.
66
Since HTTP is stateless, each request is treated independently.
77
Sessions address this by allowing the server to recognize multiple requests from the same user, which is helpful for tracking authentication and preferences.
88

9-
109
## How sessions work
1110

1211
A session typically involves:
1312

1413
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.
1815
2. **Session cookie transmission**: The server sends a `Set-Cookie` HTTP header.
1916
This instructs the browser to store the session cookie.
2017
3. **Subsequent requests**: The browser automatically includes the session cookie in the `Cookie` HTTP header for requests to the server.
2118
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.
2824

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).
3026

3127
### Database sessions
3228

3329
For larger applications or when more robust session management is required, SolidStart also supports storing session data in a database.
3430
This approach is similar to the cookie-based approach, but with some key differences:
3531

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.
4136

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.
4338

4439
## Session helpers
4540

@@ -89,7 +84,7 @@ In this example, the `useThemeSession` server function creates a session that st
8984

9085
`useSession` requires a strong password for encrypting and signing the session cookie.
9186
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.
9388

9489
A password can be generated using the following command:
9590

@@ -100,9 +95,9 @@ openssl rand -base64 32
10095
`useSession` adds a `Set-Cookie` HTTP header to the current server response.
10196
By default, the cookie is named `h3`, but can be customized with the `name` option, as shown in the example above.
10297

103-
## Getting session data
98+
## Getting the session data
10499

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.
106101

107102
```ts title="src/lib/session.ts"
108103
export async function getThemeSession() {
@@ -113,28 +108,26 @@ export async function getThemeSession() {
113108
}
114109
```
115110

116-
## Updating session data
111+
## Updating the session data
117112

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.
119114

120115
```ts title="src/lib/session.ts"
121116
export async function updateThemeSession(data: SessionData) {
122117
"use server";
123118
const session = await useThemeSession();
124-
125119
await session.update(data);
126120
}
127121
```
128122

129-
## Clearing a session
123+
## Clearing the session data
130124

131-
The `useSession` helper provides a `clear` method to destory the session.
125+
The `useSession` helper provides the `clear` method to clear the session data from the current request.
132126

133127
```ts title="src/lib/session.ts"
134128
export async function clearThemeSession() {
135129
"use server";
136130
const session = await useThemeSession();
137-
138131
await session.clear();
139132
}
140133
```

0 commit comments

Comments
 (0)