Skip to content

Commit 2344228

Browse files
committed
feat(swift): Documentation for shared keychain
1 parent 5ac6f98 commit 2344228

File tree

1 file changed

+49
-0
lines changed
  • src/pages/[platform]/build-a-backend/auth/advanced-workflows

1 file changed

+49
-0
lines changed

src/pages/[platform]/build-a-backend/auth/advanced-workflows/index.mdx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,55 @@ func federateToIdentityPoolsUsingCustomIdentityId() async throws {
375375
}
376376
```
377377

378+
## Keychain Sharing
379+
380+
### Migrating to a Shared Keychain
381+
382+
To use a shared keychain:
383+
384+
1. In Xcode, go to Project Settings → Signing & Capabilities
385+
2. Click +Capability
386+
3. Add Keychain Sharing capability
387+
4. Add a keychain group
388+
5. Repeat for all apps for which you want to share auth state, adding the same keychain group for all of them
389+
390+
To move to the shared keychain using this new keychain access group, specify the accessGroup parameter when instantiating the `AWSCognitoAuthPlugin`. If a user is currently signed-in, they will be logged out when first using the access group:
391+
392+
```swift
393+
let accessGroup = AccessGroup(name: "\(teamID).com.example.sharedItems")
394+
let secureStoragePreferences = AWSCognitoSecureStoragePreferences(
395+
accessGroup: accessGroup)
396+
try Amplify.add(
397+
plugin: AWSCognitoAuthPlugin(
398+
secureStoragePreferences: secureStoragePreferences))
399+
try Amplify.configure()
400+
```
401+
402+
If you would prefer the user session to be migrated (which will allow the user to continue to be signed-in), then specify the `migrateKeychainItemsOfUserSession` boolean in the AccessGroup to be true like so:
403+
404+
```swift
405+
let accessGroup = AccessGroup(
406+
name: "\(teamID).com.example.sharedItems",
407+
migrateKeychainItemsOfUserSession: true)
408+
let secureStoragePreferences = AWSCognitoSecureStoragePreferences(
409+
accessGroup: accessGroup)
410+
try Amplify.add(
411+
plugin: AWSCognitoAuthPlugin(
412+
secureStoragePreferences: secureStoragePreferences))
413+
try Amplify.configure()
414+
```
415+
416+
Sign in a user with any sign-in method within one app that uses this access group. After reloading another app that uses this access group, the user will be signed in. Likewise, signing out of one app will sign out the other app after reloading it.
417+
418+
### Migrating to another Shared Keychain
419+
420+
To move to a different access group, update the name parameter of the AccessGroup to be the new access group. Set `migrateKeychainItemsOfUserSession` to `true` to migrate an existing user session under the previously used access group.
421+
422+
### Migrating from a Shared Keychain
423+
424+
If you’d like to stop sharing state between this app and other apps, you can set the access group to be `AccessGroup.none` or `AccessGroup.none(migrateKeychainItemsOfUserSession: true)` if you’d like the session to be migrated.
425+
426+
378427
</InlineFilter>
379428
<InlineFilter filters={['javascript','react-native','angular','nextjs','react','vue']}>
380429
## Subscribing to Events

0 commit comments

Comments
 (0)