Page updated Nov 3, 2023

Enable sign-out

Invoke the signOut api to sign out a user from the Auth category. You can only have one user signed in at a given time. Calling signOut without any options will delete the local cache and keychain of the user and revoke the token if enabled on Amazon Cognito User Pools. If you would like to sign out of all devices, invoke the signOut api with advanced options.

1func signOutLocally() async {
2 let result = await Amplify.Auth.signOut()
3 guard let signOutResult = result as? AWSCognitoSignOutResult
4 else {
5 print("Signout failed")
6 return
7 }
8
9 print("Local signout successful: \(signOutResult.signedOutLocally)")
10 switch signOutResult {
11 case .complete:
12 // Sign Out completed fully and without errors.
13 print("Signed out successfully")
14
15 case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
16 // Sign Out completed with some errors. User is signed out of the device.
17
18 if let hostedUIError = hostedUIError {
19 print("HostedUI error \(String(describing: hostedUIError))
20 }
21
22 if let globalSignOutError = globalSignOutError {
23 // Optional: Use escape hatch to retry revocation of globalSignOutError.accessToken.
24 print("GlobalSignOut error \(String(describing: globalSignOutError))
25 }
26
27 if let revokeTokenError = revokeTokenError {
28 // Optional: Use escape hatch to retry revocation of revokeTokenError.accessToken.
29 print("Revoke token error \(String(describing: revokeTokenError))
30 }
31
32 case .failed(let error):
33 // Sign Out failed with an exception, leaving the user signed in.
34 print("SignOut failed with \(error)")
35 }
36}
1func signOutLocally() -> AnyCancellable {
2 Amplify.Publisher.create {
3 await Amplify.Auth.signOut()
4 }.sink(receiveValue: { result in
5 guard let signOutResult = result as? AWSCognitoSignOutResult
6 else {
7 print("Signout failed")
8 return
9 }
10 print("Local signout successful: \(signOutResult.signedOutLocally)")
11 switch signOutResult {
12 case .complete:
13 // Sign Out completed fully and without errors.
14 print("Signed out successfully")
15
16 case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
17 // Sign Out completed with some errors. User is signed out of the device.
18 if let hostedUIError = hostedUIError {
19 print("HostedUI error \(String(describing: hostedUIError))
20 }
21
22 if let globalSignOutError = globalSignOutError {
23 // Optional: Use escape hatch to retry revocation of globalSignOutError.accessToken.
24 print("GlobalSignOut error \(String(describing: globalSignOutError))
25 }
26
27 if let revokeTokenError = revokeTokenError {
28 // Optional: Use escape hatch to retry revocation of revokeTokenError.accessToken.
29 print("Revoke token error \(String(describing: revokeTokenError))
30 }
31
32 case .failed(let error):
33 // Sign Out failed with an exception, leaving the user signed in.
34 print("SignOut failed with \(error)")
35 }
36 })
37}

Sign Out Result Types

.complete

Indicates a successful sign out with no errors.

.partial

Indicates that sign out was completed, but with errors. The device credentials have been cleared and the user is locally signed out of the device. The .partial case will return 1 or more errors where sign out actions can be retried manually.

  • .globalSignOutError - The GlobalSignOut action failed.
    • error: Provides a message and recovery suggestion for the failure.
    • accessToken: The access token that was unable to be revoked. The Escape Hatch can be used to manually retry the global sign out.
  • .revokeTokenError - The RevokeToken action failed.
    • error: Provides a message and recovery suggestion for the failure.
    • refreshToken: The refresh token that was unable to be revoked. The Escape Hatch can be used to manually retry revoking the token.
  • .hostedUIError - The HostedUI sign out action failed.
    • error: Provides a message and recovery suggestion for the failure.

.failed

Indicates a failed sign out where user credentials remain on the device. See the attached error to determine the cause.

Token Revocation

Amazon Cognito now supports token revocation. This means that the Cognito refresh token cannot be used anymore to generate new Access and Id Tokens.

Access and Id Tokens are short-lived (60 minutes by default but can be set from 5 minutes to 1 day). After revocation, these tokens cannot be used with Cognito User Pools anymore. However, they are still valid when used with other services like AppSync or API Gateway.

For limiting subsequent calls to these other services after invalidating tokens, we recommend lowering token expiration time for your app client in the Cognito User Pools console. If you are using the Amplify CLI this can be accessed by running amplify console auth.

Token revocation is enabled automatically on new Amazon Cognito User Pools, however existing User Pools must enable this feature, using the Cognito Console or AWS CLI.

Global Sign Out

Calling signout with globalSignOut = true will invalidate all the Cognito User Pool tokens of the signed in user. If the user is signed into a device, they won't be authorized to perform a task that requires a valid token when a global signout is called from some other device. They need to sign in again to get valid tokens.

1import AWSCognitoAuthPlugin
2
3func signOutGlobally() async {
4 let result = await Amplify.Auth.signOut(options: .init(globalSignOut: true))
5 guard let signOutResult = result as? AWSCognitoSignOutResult
6 else {
7 print("Signout failed")
8 return
9 }
10
11 print("Local signout successful: \(signOutResult.signedOutLocally)")
12 switch signOutResult {
13 case .complete:
14 // handle successful sign out
15 case .failed(let error):
16 // handle failed sign out
17 case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
18 // handle partial sign out
19 }
20}
1func signOutGlobally() -> AnyCancellable {
2 Amplify.Publisher.create {
3 await Amplify.Auth.signOut(options: .init(globalSignOut: true))
4 }.sink(receiveValue: { result in
5 guard let signOutResult = result as? AWSCognitoSignOutResult
6 else {
7 print("Signout failed")
8 return
9 }
10 print("Local signout successful: \(signOutResult.signedOutLocally)")
11 switch signOutResult {
12 case .complete:
13 // handle successful sign out
14 case .failed(let error):
15 // handle failed sign out
16 case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
17 // handle partial sign out
18 }
19 })
20}