Skip to content

Commit f2a9ca8

Browse files
darinfcompnerd
authored andcommitted
Change User to be a class wrapper (#57)
This way the type has reference semantics, allowing for code like: ``` if let user = auth.currentUser { user.reload { ... } } ``` This way the API is more compatible with the equivalent Obj C API.
1 parent 8ac2f3e commit f2a9ca8

4 files changed

+37
-33
lines changed

Sources/FirebaseAuth/AuthStateDidChangeListenerHandle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class _AuthStateDidChangeListenerHandle {
1919
self.listener = swift_firebase.swift_cxx_shims.firebase.auth.AuthStateListener.Create({ auth, user, callback in
2020
guard let auth else { return }
2121
if let callback, let body = Unmanaged<AnyObject>.fromOpaque(callback).takeUnretainedValue() as? ((Auth, User?) -> Void) {
22-
body(.init(auth), user.pointee.is_valid() ? user.pointee : nil)
22+
body(.init(auth), user.pointee.is_valid() ? .init(user.pointee) : nil)
2323
}
2424
}, UnsafeMutableRawPointer(self.callback.toOpaque()))
2525
}

Sources/FirebaseAuth/FirebaseAuth+Swift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class Auth {
2929
public var currentUser: User? {
3030
let user = impl.pointee.current_user()
3131
guard user.is_valid() else { return nil }
32-
return user
32+
return .init(user)
3333
}
3434

3535
public var languageCode: String? {

Sources/FirebaseAuth/FirebaseAuthResult+Swift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class AuthDataResult {
1414

1515
public var user: User? {
1616
guard impl.user.is_valid() else { return nil }
17-
return impl.user
17+
return .init(impl.user)
1818
}
1919

2020
// public var additionalUserInfo: AdditionalUserInfo? {

Sources/FirebaseAuth/FirebaseUser+Swift.swift

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import FirebaseCore
1010

1111
import CxxShim
1212

13-
public typealias User = firebase.auth.User
1413
public typealias AuthResult = firebase.auth.AuthResult
1514

1615
public protocol UserInfo {
@@ -23,13 +22,19 @@ public protocol UserInfo {
2322
}
2423

2524
// TODO(WPP-1581): Improve the API to match the ObjC one better.
26-
extension User {
25+
public final class User {
26+
let impl: firebase.auth.User
27+
28+
init(_ impl: firebase.auth.User) {
29+
self.impl = impl
30+
}
31+
2732
public var isAnonymous: Bool {
28-
self.is_anonymous()
33+
impl.is_anonymous()
2934
}
3035

3136
public var isEmailVerified: Bool {
32-
self.is_email_verified()
37+
impl.is_email_verified()
3338
}
3439

3540
public var refreshToken: String? {
@@ -68,7 +73,7 @@ extension User {
6873
// fatalError("\(#function) not yet implemented")
6974
// }
7075

71-
public mutating func reload(completion: ((Error?) -> Void)?) {
76+
public func reload(completion: ((Error?) -> Void)?) {
7277
reloadImpl() { error in
7378
if let completion {
7479
DispatchQueue.main.async {
@@ -78,7 +83,7 @@ extension User {
7883
}
7984
}
8085

81-
public mutating func reload() async throws {
86+
public func reload() async throws {
8287
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
8388
reloadImpl() { error in
8489
if let error {
@@ -90,15 +95,15 @@ extension User {
9095
}
9196
}
9297

93-
private mutating func reloadImpl(completion: @escaping (Error?) -> Void) {
94-
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_reload(self)
98+
private func reloadImpl(completion: @escaping (Error?) -> Void) {
99+
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_reload(impl)
95100
future.setCompletion({
96101
let (_, error) = future.resultAndError
97102
completion(error)
98103
})
99104
}
100105

101-
public mutating func reauthenticate(with credential: Credential, completion: ((AuthResult?, Error?) -> Void)?) {
106+
public func reauthenticate(with credential: Credential, completion: ((AuthResult?, Error?) -> Void)?) {
102107
reauthenticateImpl(with: credential) { result, error in
103108
if let completion {
104109
DispatchQueue.main.async {
@@ -108,7 +113,7 @@ extension User {
108113
}
109114
}
110115

111-
public mutating func reauthenticate(with credential: Credential) async throws {
116+
public func reauthenticate(with credential: Credential) async throws {
112117
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
113118
reauthenticateImpl(with: credential) { result, error in
114119
if let error {
@@ -120,8 +125,8 @@ extension User {
120125
}
121126
}
122127

123-
public mutating func reauthenticateImpl(with credential: Credential, completion: @escaping (AuthResult?, Error?) -> Void) {
124-
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_reauthenticate_and_retrieve_data(self, credential)
128+
public func reauthenticateImpl(with credential: Credential, completion: @escaping (AuthResult?, Error?) -> Void) {
129+
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_reauthenticate_and_retrieve_data(impl, credential)
125130
future.setCompletion({
126131
let (result, error) = future.resultAndError
127132
completion(result, error)
@@ -130,20 +135,20 @@ extension User {
130135

131136
// -reauthenticateWithProvider:UIDelegate:completion:
132137

133-
public mutating func getIDTokenResult() async throws -> AuthTokenResult {
138+
public func getIDTokenResult() async throws -> AuthTokenResult {
134139
return try await getIDTokenResult(forcingRefresh: false)
135140
}
136141

137-
public mutating func getIDTokenResult(forcingRefresh forceRefresh: Bool) async throws
142+
public func getIDTokenResult(forcingRefresh forceRefresh: Bool) async throws
138143
-> AuthTokenResult {
139144
return try await AuthTokenResult(idTokenForcingRefresh(forceRefresh))
140145
}
141146

142-
public mutating func getIDToken() async throws -> String {
147+
public func getIDToken() async throws -> String {
143148
return try await idTokenForcingRefresh(false)
144149
}
145150

146-
public mutating func idTokenForcingRefresh(_ forceRefresh: Bool, completion: ((String?, Error?) -> Void)?) {
151+
public func idTokenForcingRefresh(_ forceRefresh: Bool, completion: ((String?, Error?) -> Void)?) {
147152
idTokenForcingRefreshImpl(forceRefresh) { result, error in
148153
if let completion {
149154
DispatchQueue.main.async {
@@ -153,7 +158,7 @@ extension User {
153158
}
154159
}
155160

156-
public mutating func idTokenForcingRefresh(_ forceRefresh: Bool) async throws
161+
public func idTokenForcingRefresh(_ forceRefresh: Bool) async throws
157162
-> String {
158163
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<String, any Error>) in
159164
idTokenForcingRefreshImpl(forceRefresh) { result, error in
@@ -166,8 +171,8 @@ extension User {
166171
}
167172
}
168173

169-
private mutating func idTokenForcingRefreshImpl(_ forceRefresh: Bool, completion: @escaping (String?, Error?) -> Void) {
170-
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_get_token(self, forceRefresh)
174+
private func idTokenForcingRefreshImpl(_ forceRefresh: Bool, completion: @escaping (String?, Error?) -> Void) {
175+
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_get_token(impl, forceRefresh)
171176
future.setCompletion({
172177
let (result, error) = future.resultAndError
173178
let stringResult: String?
@@ -191,7 +196,7 @@ extension User {
191196
fatalError("\(#function) not yet implemented")
192197
}
193198

194-
public mutating func sendEmailVerification(completion: ((Error?) -> Void)?) {
199+
public func sendEmailVerification(completion: ((Error?) -> Void)?) {
195200
sendEmailVerificationImpl() { error in
196201
if let completion {
197202
DispatchQueue.main.async {
@@ -201,7 +206,7 @@ extension User {
201206
}
202207
}
203208

204-
public mutating func sendEmailVerification() async throws {
209+
public func sendEmailVerification() async throws {
205210
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
206211
sendEmailVerificationImpl() { error in
207212
if let error {
@@ -213,9 +218,8 @@ extension User {
213218
}
214219
}
215220

216-
public mutating func sendEmailVerificationImpl(completion: @escaping (Error?) -> Void) {
217-
//let future = self.SendEmailVerification()
218-
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_send_email_verification(self)
221+
public func sendEmailVerificationImpl(completion: @escaping (Error?) -> Void) {
222+
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_send_email_verification(impl)
219223
future.setCompletion({
220224
let (_, error) = future.resultAndError
221225
completion(error)
@@ -242,30 +246,30 @@ extension User {
242246

243247
extension User: UserInfo {
244248
public var providerID: String {
245-
String(swift_firebase.swift_cxx_shims.firebase.auth.user_provider_id(self))
249+
String(swift_firebase.swift_cxx_shims.firebase.auth.user_provider_id(impl))
246250
}
247251

248252
public var uid: String {
249-
String(swift_firebase.swift_cxx_shims.firebase.auth.user_uid(self))
253+
String(swift_firebase.swift_cxx_shims.firebase.auth.user_uid(impl))
250254
}
251255

252256
public var displayName: String? {
253-
let name = String(swift_firebase.swift_cxx_shims.firebase.auth.user_display_name(self))
257+
let name = String(swift_firebase.swift_cxx_shims.firebase.auth.user_display_name(impl))
254258
return name.isEmpty ? nil : name
255259
}
256260

257261
public var photoURL: URL? {
258-
let url = String(swift_firebase.swift_cxx_shims.firebase.auth.user_photo_url(self))
262+
let url = String(swift_firebase.swift_cxx_shims.firebase.auth.user_photo_url(impl))
259263
return url.isEmpty ? nil : URL(string: url)
260264
}
261265

262266
public var email: String? {
263-
let email = String(swift_firebase.swift_cxx_shims.firebase.auth.user_email(self))
267+
let email = String(swift_firebase.swift_cxx_shims.firebase.auth.user_email(impl))
264268
return email.isEmpty ? nil : email
265269
}
266270

267271
public var phoneNumber: String? {
268-
let number = String(swift_firebase.swift_cxx_shims.firebase.auth.user_phone_number(self))
272+
let number = String(swift_firebase.swift_cxx_shims.firebase.auth.user_phone_number(impl))
269273
return number.isEmpty ? nil : number
270274
}
271275
}

0 commit comments

Comments
 (0)