@@ -21,22 +21,22 @@ Cookies can be utilized in three main contexts within Encore.ts:
21
21
Cookies can be used in authentication handlers:
22
22
23
23
``` ts
24
- import { auth , Gateway } from " encore.dev" ;
24
+ import { Cookie , Gateway } from " encore.dev/api" ;
25
+ import { authHandler } from " encore.dev/auth" ;
25
26
26
27
// Define auth parameters with cookies
27
28
interface AuthParams {
28
29
sessionId: Cookie <" sessionId" >;
29
30
}
30
31
31
32
// 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 );
35
35
});
36
36
37
37
// Configure the gateway with the auth handler
38
38
export const gateway = new Gateway ({
39
- authHandler ,
39
+ authHandler: auth ,
40
40
});
41
41
```
42
42
@@ -45,7 +45,7 @@ export const gateway = new Gateway({
45
45
You can set cookies in your API responses by including them in your response type:
46
46
47
47
``` ts
48
- import { api } from " encore.dev" ;
48
+ import { api , Cookie } from " encore.dev/api " ;
49
49
50
50
// Define a response type with a cookie
51
51
interface LoginResponse {
@@ -58,7 +58,7 @@ export const login = api<LoginParams, LoginResponse>({
58
58
method: " POST" ,
59
59
path: " /login" ,
60
60
expose: true ,
61
- }, async (params ) => {
61
+ }, async (params ): Promise < LoginResponse > => {
62
62
// Authenticate user
63
63
const user = await authenticateUser (params .username , params .password );
64
64
const sessionId = generateSessionId ();
@@ -85,25 +85,26 @@ export const login = api<LoginParams, LoginResponse>({
85
85
When creating API endpoints, you can access cookies sent by the client by defining them in your parameter type:
86
86
87
87
``` ts
88
- import { api } from " encore.dev" ;
88
+ import { api } from " encore.dev/api " ;
89
89
90
90
// Define a request type with a cookie
91
91
interface Params {
92
92
language? : Cookie <" language" >;
93
93
}
94
94
95
95
// 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
+ );` ` `
107
108
108
109
## Typed Cookie Values
109
110
0 commit comments