Skip to content

Commit

Permalink
feat: rename onAuthStateChange to authStateChanges and add event key …
Browse files Browse the repository at this point in the history
…to posted notification (#163)
  • Loading branch information
grdsdev authored Nov 18, 2023
1 parent 44441b7 commit 6cd6dda
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -591,6 +592,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -672,7 +674,6 @@
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
Expand Down Expand Up @@ -715,7 +716,6 @@
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
Expand Down
2 changes: 1 addition & 1 deletion Examples/UserManagement/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct AppView: View {
}
}
.task {
for await state in await supabase.auth.onAuthStateChange() {
for await state in await supabase.auth.authStateChanges {
if [.initialSession, .signedIn, .signedOut].contains(state.event) {
isAuthenticated = state.session != nil
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/UserManagement/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct ProfileView: View {
}
}
})
.onChange(of: imageSelection) { _, newValue in
.onChange(of: imageSelection) { newValue in
guard let newValue else { return }
loadTransferable(from: newValue)
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/GoTrue/GoTrueClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public actor GoTrueClient {
/// Listen for auth state changes.
///
/// An `.initialSession` is always emitted when this method is called.
public func onAuthStateChange() async -> AsyncStream<(
public var authStateChanges: AsyncStream<(
event: AuthChangeEvent,
session: Session?
)> {
Expand Down Expand Up @@ -828,6 +828,14 @@ public actor GoTrueClient {
extension GoTrueClient {
/// Notification posted when an auth state event is triggered.
public static let didChangeAuthStateNotification = Notification.Name(
"DID_CHANGE_AUTH_STATE_NOTIFICATION"
"GoTrueClient.didChangeAuthStateNotification"
)

/// A user info key to retrieve the ``AuthChangeEvent`` value for a
/// ``GoTrueClient/didChangeAuthStateNotification`` notification.
public static let authChangeEventInfoKey = "GoTrueClient.authChangeEvent"

/// A user info key to retrieve the ``Session`` value for a
/// ``GoTrueClient/didChangeAuthStateNotification`` notification.
public static let authChangeSessionInfoKey = "GoTrueClient.authChangeSession"
}
6 changes: 5 additions & 1 deletion Sources/GoTrue/Internal/EventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ extension EventEmitter {
emit: { event, session, id in
NotificationCenter.default.post(
name: GoTrueClient.didChangeAuthStateNotification,
object: nil
object: nil,
userInfo: [
GoTrueClient.authChangeEventInfoKey: event,
GoTrueClient.authChangeSessionInfoKey: session as Any,
]
)
if let id {
continuations.value[id]?.yield((event, session))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public final class SupabaseClient: @unchecked Sendable {
private func listenForAuthEvents() {
listenForAuthEventsTask.setValue(
Task {
for await (event, session) in await auth.onAuthStateChange() {
for await (event, session) in await auth.authStateChanges {
handleTokenChanged(event: event, session: session)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/GoTrueTests/GoTrueClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ConcurrencyExtras
final class GoTrueClientTests: XCTestCase {
fileprivate var api: APIClient!

func testOnAuthStateChange() async throws {
func testAuthStateChanges() async throws {
let session = Session.validSession
let sut = makeSUT()

Expand All @@ -25,7 +25,7 @@ final class GoTrueClientTests: XCTestCase {
$0.eventEmitter = .live
$0.sessionManager.session = { @Sendable _ in session }
} operation: {
let authStateStream = await sut.onAuthStateChange()
let authStateStream = await sut.authStateChanges

let streamTask = Task {
for await (event, _) in authStateStream {
Expand Down

0 comments on commit 6cd6dda

Please sign in to comment.