Skip to content

Commit

Permalink
fix(auth): sign out regardless of request success (#375)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
leoMehlig and grdsdev committed May 10, 2024
1 parent b4b9276 commit 25178e2
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,15 +698,23 @@ public final class AuthClient: Sendable {
/// If using ``SignOutScope/others`` scope, no ``AuthChangeEvent/signedOut`` event is fired.
/// - Parameter scope: Specifies which sessions should be logged out.
public func signOut(scope: SignOutScope = .global) async throws {
do {
// Make sure we have a valid session.
_ = try await sessionManager.session()
guard let accessToken = currentSession?.accessToken else {
configuration.logger?.warning("signOut called without a session")
return
}

if scope != .others {
await sessionManager.remove()
eventEmitter.emit(.signedOut, session: nil)
}

try await api.authorizedExecute(
do {
_ = try await api.execute(
.init(
url: configuration.url.appendingPathComponent("logout"),
method: .post,
query: [URLQueryItem(name: "scope", value: scope.rawValue)]
query: [URLQueryItem(name: "scope", value: scope.rawValue)],
headers: [.init(name: "Authorization", value: "Bearer \(accessToken)")]
)
)
} catch {
Expand All @@ -720,11 +728,6 @@ public final class AuthClient: Sendable {
throw error
}
}

if scope != .others {
await sessionManager.remove()
eventEmitter.emit(.signedOut, session: nil)
}
}

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

0 comments on commit 25178e2

Please sign in to comment.