Skip to content

Commit 8c7c257

Browse files
authored
feat(gotrue): add scope to signOut (#175)
1 parent 4fd5041 commit 8c7c257

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

Sources/GoTrue/GoTrueClient.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,17 +587,22 @@ public actor GoTrueClient {
587587
}
588588

589589
/// Signs out the current user, if there is a logged in user.
590-
public func signOut() async throws {
590+
/// If using `SignOutScope.others` scope, no `AuthChangeEvent.signedOut` event is fired.
591+
/// - Parameter scope: Specifies which sessions should be logged out.
592+
public func signOut(scope: SignOutScope = .global) async throws {
591593
do {
592594
_ = try await sessionManager.session()
593595
try await api.authorizedExecute(
594596
.init(
595597
path: "/logout",
596-
method: .post
598+
method: .post,
599+
query: [URLQueryItem(name: "scope", value: scope.rawValue)]
597600
)
598601
)
599-
await sessionManager.remove()
600-
eventEmitter.emit(.signedOut, session: nil)
602+
if scope != .others {
603+
await sessionManager.remove()
604+
eventEmitter.emit(.signedOut, session: nil)
605+
}
601606
} catch {
602607
eventEmitter.emit(.signedOut, session: nil)
603608
throw error

Sources/GoTrue/Types.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,15 @@ public struct AuthMFAGetAuthenticatorAssuranceLevelResponse: Decodable, Hashable
576576
public let currentAuthenticationMethods: [AMREntry]
577577
}
578578

579+
public enum SignOutScope: String {
580+
/// All sessions by this account will be signed out.
581+
case global
582+
/// Only this session will be signed out.
583+
case local
584+
/// 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.
585+
case others
586+
}
587+
579588
// MARK: - Encodable & Decodable
580589

581590
private let dateFormatterWithFractionalSeconds = { () -> ISO8601DateFormatter in

Tests/GoTrueTests/RequestsTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@ final class RequestsTests: XCTestCase {
249249
}
250250
}
251251
}
252+
253+
func testSignOutWithLocalScope() async {
254+
let sut = makeSUT()
255+
await withDependencies {
256+
$0.sessionManager.session = { @Sendable _ in .validSession }
257+
$0.eventEmitter = .noop
258+
} operation: {
259+
await assert {
260+
try await sut.signOut(scope: .local)
261+
}
262+
}
263+
}
264+
265+
func testSignOutWithOthersScope() async {
266+
let sut = makeSUT()
267+
await withDependencies {
268+
$0.sessionManager.session = { @Sendable _ in .validSession }
269+
$0.eventEmitter = .noop
270+
} operation: {
271+
await assert {
272+
try await sut.signOut(scope: .others)
273+
}
274+
}
275+
}
252276

253277
func testVerifyOTPUsingEmail() async {
254278
let sut = makeSUT()

Tests/GoTrueTests/__Snapshots__/RequestsTests/testSignOut.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ curl \
33
--header "Authorization: Bearer accesstoken" \
44
--header "X-Client-Info: gotrue-swift/x.y.z" \
55
--header "apikey: dummy.api.key" \
6-
"http://localhost:54321/auth/v1/logout"
6+
"http://localhost:54321/auth/v1/logout?scope=global"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
curl \
2+
--request POST \
3+
--header "Authorization: Bearer accesstoken" \
4+
--header "X-Client-Info: gotrue-swift/x.y.z" \
5+
--header "apikey: dummy.api.key" \
6+
"http://localhost:54321/auth/v1/logout?scope=local"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
curl \
2+
--request POST \
3+
--header "Authorization: Bearer accesstoken" \
4+
--header "X-Client-Info: gotrue-swift/x.y.z" \
5+
--header "apikey: dummy.api.key" \
6+
"http://localhost:54321/auth/v1/logout?scope=others"

0 commit comments

Comments
 (0)