Skip to content

Commit 6c24eb9

Browse files
committed
fix example code
1 parent d6010d3 commit 6c24eb9

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

docs/ts/primitives/cookies.mdx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ Cookies can be utilized in three main contexts within Encore.ts:
2121
Cookies can be used in authentication handlers:
2222

2323
```ts
24-
import { auth, Gateway } from "encore.dev";
24+
import { Cookie, Gateway } from "encore.dev/api";
25+
import { authHandler } from "encore.dev/auth";
2526

2627
// Define auth parameters with cookies
2728
interface AuthParams {
2829
sessionId: Cookie<"sessionId">;
2930
}
3031

3132
// Auth handler that uses cookies
32-
const authHandler = auth<AuthParams, User>(async ({ sessionId }) => {
33-
const sessionId = sessionId.value;
34-
return validateAndGetUser(sessionId);
33+
const auth = authHandler<AuthParams, User>(async ({ sessionId }) => {
34+
return validateAndGetUser(sessionId.value);
3535
});
3636

3737
// Configure the gateway with the auth handler
3838
export const gateway = new Gateway({
39-
authHandler,
39+
authHandler: auth,
4040
});
4141
```
4242

@@ -45,7 +45,7 @@ export const gateway = new Gateway({
4545
You can set cookies in your API responses by including them in your response type:
4646

4747
```ts
48-
import { api } from "encore.dev";
48+
import { api, Cookie } from "encore.dev/api";
4949

5050
// Define a response type with a cookie
5151
interface LoginResponse {
@@ -58,7 +58,7 @@ export const login = api<LoginParams, LoginResponse>({
5858
method: "POST",
5959
path: "/login",
6060
expose: true,
61-
}, async (params) => {
61+
}, async (params): Promise<LoginResponse> => {
6262
// Authenticate user
6363
const user = await authenticateUser(params.username, params.password);
6464
const sessionId = generateSessionId();
@@ -85,25 +85,26 @@ export const login = api<LoginParams, LoginResponse>({
8585
When creating API endpoints, you can access cookies sent by the client by defining them in your parameter type:
8686

8787
```ts
88-
import { api } from "encore.dev";
88+
import { api } from "encore.dev/api";
8989

9090
// Define a request type with a cookie
9191
interface Params {
9292
language?: Cookie<"language">;
9393
}
9494

9595
// Create an API endpoint that uses the cookie
96-
export const get = api<Params, { msg: string }>({
97-
method: "GET",
98-
path: "/user/profile",
99-
expose: true,
100-
}, async ({ language }) => {
101-
// Access the cookie value
102-
const language = language.value ?? "en";
103-
104-
return { msg: `your language: ${language}` }
105-
});
106-
```
96+
export const get = api<Params, { msg: string }>(
97+
{
98+
method: "GET",
99+
path: "/user/profile",
100+
expose: true,
101+
},
102+
async ({ language }) => {
103+
// Access the cookie value
104+
const lang = language?.value ?? "en";
105+
return { msg: `your language: ${lang}` };
106+
},
107+
);```
107108
108109
## Typed Cookie Values
109110

0 commit comments

Comments
 (0)