Skip to content

Commit 25178e2

Browse files
leoMehliggrdsdev
andauthored
fix(auth): sign out regardless of request success (#375)
* Logout should always reset the session. * Update Sources/Auth/AuthClient.swift Co-authored-by: Guilherme Souza <[email protected]> --------- Co-authored-by: Guilherme Souza <[email protected]>
1 parent b4b9276 commit 25178e2

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Sources/Auth/AuthClient.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,23 @@ public final class AuthClient: Sendable {
698698
/// If using ``SignOutScope/others`` scope, no ``AuthChangeEvent/signedOut`` event is fired.
699699
/// - Parameter scope: Specifies which sessions should be logged out.
700700
public func signOut(scope: SignOutScope = .global) async throws {
701-
do {
702-
// Make sure we have a valid session.
703-
_ = try await sessionManager.session()
701+
guard let accessToken = currentSession?.accessToken else {
702+
configuration.logger?.warning("signOut called without a session")
703+
return
704+
}
705+
706+
if scope != .others {
707+
await sessionManager.remove()
708+
eventEmitter.emit(.signedOut, session: nil)
709+
}
704710

705-
try await api.authorizedExecute(
711+
do {
712+
_ = try await api.execute(
706713
.init(
707714
url: configuration.url.appendingPathComponent("logout"),
708715
method: .post,
709-
query: [URLQueryItem(name: "scope", value: scope.rawValue)]
716+
query: [URLQueryItem(name: "scope", value: scope.rawValue)],
717+
headers: [.init(name: "Authorization", value: "Bearer \(accessToken)")]
710718
)
711719
)
712720
} catch {
@@ -720,11 +728,6 @@ public final class AuthClient: Sendable {
720728
throw error
721729
}
722730
}
723-
724-
if scope != .others {
725-
await sessionManager.remove()
726-
eventEmitter.emit(.signedOut, session: nil)
727-
}
728731
}
729732

730733
/// Log in an user given a User supplied OTP received via email.

0 commit comments

Comments
 (0)