Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gotrue): add scope to signOut #175

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/GoTrue/GoTrueClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,22 @@ public actor GoTrueClient {
}

/// Signs out the current user, if there is a logged in user.
public func signOut() async throws {
/// 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 {
_ = try await sessionManager.session()
try await api.authorizedExecute(
.init(
path: "/logout",
method: .post
method: .post,
query: [URLQueryItem(name: "scope", value: scope.rawValue)]
)
)
await sessionManager.remove()
eventEmitter.emit(.signedOut, session: nil)
if scope != .others {
await sessionManager.remove()
eventEmitter.emit(.signedOut, session: nil)
}
} catch {
eventEmitter.emit(.signedOut, session: nil)
throw error
Expand Down
9 changes: 9 additions & 0 deletions Sources/GoTrue/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,15 @@ public struct AuthMFAGetAuthenticatorAssuranceLevelResponse: Decodable, Hashable
public let currentAuthenticationMethods: [AMREntry]
}

public enum SignOutScope: String {
/// All sessions by this account will be signed out.
case global
/// Only this session will be signed out.
case local
/// All other sessions except the current one will be signed out. When using `others`, there is no `AuthChangeEvent.signedOut` event fired on the current session.
case others
}

// MARK: - Encodable & Decodable

private let dateFormatterWithFractionalSeconds = { () -> ISO8601DateFormatter in
Expand Down
24 changes: 24 additions & 0 deletions Tests/GoTrueTests/RequestsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ final class RequestsTests: XCTestCase {
}
}
}

func testSignOutWithLocalScope() async {
let sut = makeSUT()
await withDependencies {
$0.sessionManager.session = { @Sendable _ in .validSession }
$0.eventEmitter = .noop
} operation: {
await assert {
try await sut.signOut(scope: .local)
}
}
}

func testSignOutWithOthersScope() async {
let sut = makeSUT()
await withDependencies {
$0.sessionManager.session = { @Sendable _ in .validSession }
$0.eventEmitter = .noop
} operation: {
await assert {
try await sut.signOut(scope: .others)
}
}
}

func testVerifyOTPUsingEmail() async {
let sut = makeSUT()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ curl \
--header "Authorization: Bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "apikey: dummy.api.key" \
"http://localhost:54321/auth/v1/logout"
"http://localhost:54321/auth/v1/logout?scope=global"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
curl \
--request POST \
--header "Authorization: Bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "apikey: dummy.api.key" \
"http://localhost:54321/auth/v1/logout?scope=local"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
curl \
--request POST \
--header "Authorization: Bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "apikey: dummy.api.key" \
"http://localhost:54321/auth/v1/logout?scope=others"