Skip to content

Commit 987f825

Browse files
authored
fix(Auth): Locally signOut the user if globaly signout fail with notAuthorized (#461)
* Added option to global signout and made local signOut as default
1 parent 71b1cb3 commit 987f825

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

Amplify/Categories/Auth/Request/AuthSignOutRequest.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct AuthSignOutRequest: AmplifyOperationRequest {
1212
public var options: Options
1313

1414
public init(options: Options) {
15+
1516
self.options = options
1617
}
1718
}
@@ -25,7 +26,11 @@ public extension AuthSignOutRequest {
2526
/// key/values
2627
public let pluginOptions: Any?
2728

28-
public init(pluginOptions: Any? = nil) {
29+
public let globalSignOut: Bool
30+
31+
public init(globalSignOut: Bool = false,
32+
pluginOptions: Any? = nil) {
33+
self.globalSignOut = globalSignOut
2934
self.pluginOptions = pluginOptions
3035
}
3136
}

AmplifyPlugins/Auth/AWSAuthPlugin/Dependency/AuthenticationProviderAdapter+SignOut.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ extension AuthenticationProviderAdapter {
1212

1313
func signOut(request: AuthSignOutRequest, completionHandler: @escaping (Result<Void, AuthError>) -> Void) {
1414

15-
// If user is signed in through HostedUI the signout require UI to complete. So calling this in main thread.
16-
DispatchQueue.main.async { [weak self] in
17-
guard let self = self else {
18-
return
15+
if (request.options.globalSignOut) {
16+
// If user is signed in through HostedUI the signout require UI to complete. So calling this in main thread.
17+
DispatchQueue.main.async { [weak self] in
18+
guard let self = self else {
19+
return
20+
}
21+
self.signOutWithUI(completionHandler)
1922
}
20-
self.signOutWithUI(completionHandler)
23+
} else {
24+
awsMobileClient.signOutLocally()
25+
completionHandler(.success(()))
2126
}
2227
}
2328

@@ -27,11 +32,19 @@ extension AuthenticationProviderAdapter {
2732
// There is no point on returning an error back to the developer, because
2833
// they do not control how the UI is presented.
2934
dispatchPrecondition(condition: .onQueue(DispatchQueue.main))
35+
3036
let signOutOptions = SignOutOptions(signOutGlobally: true, invalidateTokens: true)
31-
awsMobileClient.signOut(options: signOutOptions) { error in
37+
awsMobileClient.signOut(options: signOutOptions) { [weak self] error in
3238
guard error == nil else {
3339
let authError = AuthErrorHelper.toAuthError(error!)
34-
completionHandler(.failure(authError))
40+
if case .notAuthorized(_, _, _) = authError {
41+
// signOut globally might return notAuthorized when the current token is expired or invalidated
42+
// In this case, we just signOut the user locally and return a success result back.
43+
self?.awsMobileClient.signOutLocally()
44+
completionHandler(.success(()))
45+
} else {
46+
completionHandler(.failure(authError))
47+
}
3548
return
3649
}
3750
completionHandler(.success(()))

AmplifyPlugins/Auth/AWSAuthPlugin/Service/AWSMobileClient/AWSMobileClientAdapter.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class AWSMobileClientAdapter: AWSMobileClientBehavior {
103103
awsMobileClient.signOut(options: options, completionHandler: completionHandler)
104104
}
105105

106+
func signOutLocally() {
107+
awsMobileClient.signOut()
108+
}
109+
106110
func username() -> String? {
107111
return awsMobileClient.username
108112
}

AmplifyPlugins/Auth/AWSAuthPlugin/Service/AWSMobileClient/AWSMobileClientBehavior.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ protocol AWSMobileClientBehavior {
5050
func signOut(options: SignOutOptions,
5151
completionHandler: @escaping ((Error?) -> Void))
5252

53+
func signOutLocally()
54+
5355
func username() -> String?
5456

5557
func verifyUserAttribute(attributeName: String,

0 commit comments

Comments
 (0)