Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 17, 2024

Sign-out

Amplify provides a client library that enables you to interact with backend resources such as Amplify Auth.

The quickest way to get started with Amplify Auth in your frontend application is with the Authenticator component, which provides a customizable UI and complete authentication flows.

To sign a user out of your application use the signOut API.

func signOutLocally() async {
let result = await Amplify.Auth.signOut()
guard let signOutResult = result as? AWSCognitoSignOutResult
else {
print("Signout failed")
return
}
print("Local signout successful: \(signOutResult.signedOutLocally)")
switch signOutResult {
case .complete:
// Sign Out completed fully and without errors.
print("Signed out successfully")
case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
// Sign Out completed with some errors. User is signed out of the device.
if let hostedUIError = hostedUIError {
print("HostedUI error \(String(describing: hostedUIError))
}
if let globalSignOutError = globalSignOutError {
// Optional: Use escape hatch to retry revocation of globalSignOutError.accessToken.
print("GlobalSignOut error \(String(describing: globalSignOutError))
}
if let revokeTokenError = revokeTokenError {
// Optional: Use escape hatch to retry revocation of revokeTokenError.accessToken.
print("Revoke token error \(String(describing: revokeTokenError))
}
case .failed(let error):
// Sign Out failed with an exception, leaving the user signed in.
print("SignOut failed with \(error)")
}
}
func signOutLocally() -> AnyCancellable {
Amplify.Publisher.create {
await Amplify.Auth.signOut()
}.sink(receiveValue: { result in
guard let signOutResult = result as? AWSCognitoSignOutResult
else {
print("Signout failed")
return
}
print("Local signout successful: \(signOutResult.signedOutLocally)")
switch signOutResult {
case .complete:
// Sign Out completed fully and without errors.
print("Signed out successfully")
case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
// Sign Out completed with some errors. User is signed out of the device.
if let hostedUIError = hostedUIError {
print("HostedUI error \(String(describing: hostedUIError))
}
if let globalSignOutError = globalSignOutError {
// Optional: Use escape hatch to retry revocation of globalSignOutError.accessToken.
print("GlobalSignOut error \(String(describing: globalSignOutError))
}
if let revokeTokenError = revokeTokenError {
// Optional: Use escape hatch to retry revocation of revokeTokenError.accessToken.
print("Revoke token error \(String(describing: revokeTokenError))
}
case .failed(let error):
// Sign Out failed with an exception, leaving the user signed in.
print("SignOut failed with \(error)")
}
})
}

You can also sign out users from all devices by performing a global sign-out. This will also invalidate all refresh tokens issued to a user. The user's current access and ID tokens will remain valid on other devices until the refresh token expires (access and ID tokens expire one hour after they are issued).

import AWSCognitoAuthPlugin
func signOutGlobally() async {
let result = await Amplify.Auth.signOut(options: .init(globalSignOut: true))
guard let signOutResult = result as? AWSCognitoSignOutResult
else {
print("Signout failed")
return
}
print("Local signout successful: \(signOutResult.signedOutLocally)")
switch signOutResult {
case .complete:
// handle successful sign out
case .failed(let error):
// handle failed sign out
case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
// handle partial sign out
}
}
func signOutGlobally() -> AnyCancellable {
Amplify.Publisher.create {
await Amplify.Auth.signOut(options: .init(globalSignOut: true))
}.sink(receiveValue: { result in
guard let signOutResult = result as? AWSCognitoSignOutResult
else {
print("Signout failed")
return
}
print("Local signout successful: \(signOutResult.signedOutLocally)")
switch signOutResult {
case .complete:
// handle successful sign out
case .failed(let error):
// handle failed sign out
case let .partial(revokeTokenError, globalSignOutError, hostedUIError):
// handle partial sign out
}
})
}