Skip to content

Commit 1f16259

Browse files
authored
chore: add cookie storage example (#6554)
1 parent 00203ef commit 1f16259

File tree

1 file changed

+60
-15
lines changed
  • src/pages/[platform]/build-a-backend/auth/manage-user-session

1 file changed

+60
-15
lines changed

src/pages/[platform]/build-a-backend/auth/manage-user-session/index.mdx

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ In Amplify the `localStorage` is the default storage mechanism. It saves the tok
156156
<Block name="TypeScript">
157157
158158
```ts
159-
import { Amplify, ResourcesConfig } from 'aws-amplify';
159+
import { Amplify, type ResourcesConfig } from 'aws-amplify';
160160
import { defaultStorage } from 'aws-amplify/utils';
161161
import { cognitoUserPoolsTokenProvider } from 'aws-amplify/auth/cognito';
162162

@@ -199,6 +199,57 @@ cognitoUserPoolsTokenProvider.setKeyValueStorage(defaultStorage);
199199
</Block>
200200
</BlockSwitcher>
201201
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+
202253
#### Browser Session Storage
203254
204255
`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);
207258
<Block name="TypeScript">
208259
209260
```ts
210-
import { Amplify, ResourcesConfig } from 'aws-amplify';
261+
import { Amplify, type ResourcesConfig } from 'aws-amplify';
211262
import { sessionStorage } from 'aws-amplify/utils';
212263
import { cognitoUserPoolsTokenProvider } from 'aws-amplify/auth/cognito';
213264

@@ -218,12 +269,9 @@ const authConfig: ResourcesConfig['Auth'] = {
218269
}
219270
};
220271

221-
Amplify.configure(
222-
{
223-
Auth: authConfig
224-
},
225-
{ Auth: { tokenProvider: cognitoUserPoolsTokenProvider } }
226-
);
272+
Amplify.configure({
273+
Auth: authConfig
274+
});
227275

228276
cognitoUserPoolsTokenProvider.setKeyValueStorage(sessionStorage);
229277
```
@@ -261,7 +309,7 @@ You can implement your own custom storage mechanism by creating a class that imp
261309
<Block name="TypeScript">
262310
263311
```ts
264-
import { Amplify, ResourcesConfig } from 'aws-amplify';
312+
import { Amplify, type ResourcesConfig } from 'aws-amplify';
265313
import { KeyValueStorageInterface } from 'aws-amplify/utils';
266314
import { cognitoUserPoolsTokenProvider } from 'aws-amplify/auth/cognito';
267315

@@ -288,12 +336,9 @@ class MyCustomStorage implements KeyValueStorageInterface {
288336
}
289337
}
290338

291-
Amplify.configure(
292-
{
293-
Auth: authConfig
294-
},
295-
{ Auth: { tokenProvider: cognitoUserPoolsTokenProvider } }
296-
);
339+
Amplify.configure({
340+
Auth: authConfig
341+
});
297342

298343
cognitoUserPoolsTokenProvider.setKeyValueStorage(new MyCustomStorage());
299344
```

0 commit comments

Comments
 (0)