@@ -156,7 +156,7 @@ In Amplify the `localStorage` is the default storage mechanism. It saves the tok
156
156
<Block name="TypeScript">
157
157
158
158
` ` ` ts
159
- import { Amplify , ResourcesConfig } from ' aws-amplify' ;
159
+ import { Amplify , type ResourcesConfig } from ' aws-amplify' ;
160
160
import { defaultStorage } from ' aws-amplify/utils' ;
161
161
import { cognitoUserPoolsTokenProvider } from ' aws-amplify/auth/cognito' ;
162
162
@@ -199,6 +199,57 @@ cognitoUserPoolsTokenProvider.setKeyValueStorage(defaultStorage);
199
199
</Block>
200
200
</BlockSwitcher>
201
201
202
+ #### Cookie Storage
203
+
204
+ ` CookieStorage` saves the tokens in the browser's ` Cookies` . The cookies will persist across browser sessions and tabs. You can explicitly set to this storage by calling:
205
+
206
+ <BlockSwitcher>
207
+ <Block name="TypeScript">
208
+
209
+ ` ` ` ts
210
+ import { Amplify , type ResourcesConfig } from ' aws-amplify' ;
211
+ import { CookieStorage } from ' aws-amplify/utils' ;
212
+ import { cognitoUserPoolsTokenProvider } from ' aws-amplify/auth/cognito' ;
213
+
214
+ const authConfig: ResourcesConfig ['Auth '] = {
215
+ Cognito: {
216
+ userPoolId: ' your_user_pool_id' ,
217
+ userPoolClientId: ' your_user_pool_client_id'
218
+ }
219
+ };
220
+
221
+ Amplify .configure ({
222
+ Auth: authConfig
223
+ });
224
+
225
+ cognitoUserPoolsTokenProvider .setKeyValueStorage (new CookieStorage ());
226
+ ` ` `
227
+
228
+ </Block>
229
+ <Block name="JavaScript">
230
+
231
+ ` ` ` javascript
232
+ import { Amplify } from ' aws-amplify' ;
233
+ import { CookieStorage } from ' aws-amplify/utils' ;
234
+ import { cognitoUserPoolsTokenProvider } from ' aws-amplify/auth/cognito' ;
235
+
236
+ const authConfig = {
237
+ Cognito: {
238
+ userPoolId: ' your_user_pool_id' ,
239
+ userPoolClientId: ' your_user_pool_client_id'
240
+ }
241
+ };
242
+
243
+ Amplify .configure ({
244
+ Auth: authConfig
245
+ });
246
+
247
+ cognitoUserPoolsTokenProvider .setKeyValueStorage (new CookieStorage ());
248
+ ` ` `
249
+
250
+ </Block>
251
+ </BlockSwitcher>
252
+
202
253
#### Browser Session Storage
203
254
204
255
` sessionStorage` saves the tokens in the browser's ` sessionStorage` and these tokens will clear when a tab is closed. The benefit to this storage mechanism is that the session only lasts as long as the browser is open and you can sign out users when they close the tab. You can update to this storage by calling:
@@ -207,7 +258,7 @@ cognitoUserPoolsTokenProvider.setKeyValueStorage(defaultStorage);
207
258
<Block name="TypeScript">
208
259
209
260
` ` ` ts
210
- import { Amplify , ResourcesConfig } from ' aws-amplify' ;
261
+ import { Amplify , type ResourcesConfig } from ' aws-amplify' ;
211
262
import { sessionStorage } from ' aws-amplify/utils' ;
212
263
import { cognitoUserPoolsTokenProvider } from ' aws-amplify/auth/cognito' ;
213
264
@@ -218,12 +269,9 @@ const authConfig: ResourcesConfig['Auth'] = {
218
269
}
219
270
};
220
271
221
- Amplify .configure (
222
- {
223
- Auth: authConfig
224
- },
225
- { Auth: { tokenProvider: cognitoUserPoolsTokenProvider } }
226
- );
272
+ Amplify .configure ({
273
+ Auth: authConfig
274
+ });
227
275
228
276
cognitoUserPoolsTokenProvider .setKeyValueStorage (sessionStorage);
229
277
` ` `
@@ -261,7 +309,7 @@ You can implement your own custom storage mechanism by creating a class that imp
261
309
<Block name="TypeScript">
262
310
263
311
` ` ` ts
264
- import { Amplify , ResourcesConfig } from ' aws-amplify' ;
312
+ import { Amplify , type ResourcesConfig } from ' aws-amplify' ;
265
313
import { KeyValueStorageInterface } from ' aws-amplify/utils' ;
266
314
import { cognitoUserPoolsTokenProvider } from ' aws-amplify/auth/cognito' ;
267
315
@@ -288,12 +336,9 @@ class MyCustomStorage implements KeyValueStorageInterface {
288
336
}
289
337
}
290
338
291
- Amplify .configure (
292
- {
293
- Auth: authConfig
294
- },
295
- { Auth: { tokenProvider: cognitoUserPoolsTokenProvider } }
296
- );
339
+ Amplify .configure ({
340
+ Auth: authConfig
341
+ });
297
342
298
343
cognitoUserPoolsTokenProvider .setKeyValueStorage (new MyCustomStorage ());
299
344
` ` `
0 commit comments