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

Change User to be a class wrapper #57

Merged
merged 1 commit into from
Apr 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class _AuthStateDidChangeListenerHandle {
self.listener = swift_firebase.swift_cxx_shims.firebase.auth.AuthStateListener.Create({ auth, user, callback in
guard let auth else { return }
if let callback, let body = Unmanaged<AnyObject>.fromOpaque(callback).takeUnretainedValue() as? ((Auth, User?) -> Void) {
body(.init(auth), user.pointee.is_valid() ? user.pointee : nil)
body(.init(auth), user.pointee.is_valid() ? .init(user.pointee) : nil)
}
}, UnsafeMutableRawPointer(self.callback.toOpaque()))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/FirebaseAuth/FirebaseAuth+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class Auth {
public var currentUser: User? {
let user = impl.pointee.current_user()
guard user.is_valid() else { return nil }
return user
return .init(user)
}

public var languageCode: String? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/FirebaseAuth/FirebaseAuthResult+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class AuthDataResult {

public var user: User? {
guard impl.user.is_valid() else { return nil }
return impl.user
return .init(impl.user)
}

// public var additionalUserInfo: AdditionalUserInfo? {
Expand Down
64 changes: 34 additions & 30 deletions Sources/FirebaseAuth/FirebaseUser+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import FirebaseCore

import CxxShim

public typealias User = firebase.auth.User
public typealias AuthResult = firebase.auth.AuthResult

public protocol UserInfo {
Expand All @@ -23,13 +22,19 @@ public protocol UserInfo {
}

// TODO(WPP-1581): Improve the API to match the ObjC one better.
extension User {
public final class User {
let impl: firebase.auth.User

init(_ impl: firebase.auth.User) {
self.impl = impl
}

public var isAnonymous: Bool {
self.is_anonymous()
impl.is_anonymous()
}

public var isEmailVerified: Bool {
self.is_email_verified()
impl.is_email_verified()
}

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

public mutating func reload(completion: ((Error?) -> Void)?) {
public func reload(completion: ((Error?) -> Void)?) {
reloadImpl() { error in
if let completion {
DispatchQueue.main.async {
Expand All @@ -78,7 +83,7 @@ extension User {
}
}

public mutating func reload() async throws {
public func reload() async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
reloadImpl() { error in
if let error {
Expand All @@ -90,15 +95,15 @@ extension User {
}
}

private mutating func reloadImpl(completion: @escaping (Error?) -> Void) {
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_reload(self)
private func reloadImpl(completion: @escaping (Error?) -> Void) {
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_reload(impl)
future.setCompletion({
let (_, error) = future.resultAndError
completion(error)
})
}

public mutating func reauthenticate(with credential: Credential, completion: ((AuthResult?, Error?) -> Void)?) {
public func reauthenticate(with credential: Credential, completion: ((AuthResult?, Error?) -> Void)?) {
reauthenticateImpl(with: credential) { result, error in
if let completion {
DispatchQueue.main.async {
Expand All @@ -108,7 +113,7 @@ extension User {
}
}

public mutating func reauthenticate(with credential: Credential) async throws {
public func reauthenticate(with credential: Credential) async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
reauthenticateImpl(with: credential) { result, error in
if let error {
Expand All @@ -120,8 +125,8 @@ extension User {
}
}

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

// -reauthenticateWithProvider:UIDelegate:completion:

public mutating func getIDTokenResult() async throws -> AuthTokenResult {
public func getIDTokenResult() async throws -> AuthTokenResult {
return try await getIDTokenResult(forcingRefresh: false)
}

public mutating func getIDTokenResult(forcingRefresh forceRefresh: Bool) async throws
public func getIDTokenResult(forcingRefresh forceRefresh: Bool) async throws
-> AuthTokenResult {
return try await AuthTokenResult(idTokenForcingRefresh(forceRefresh))
}

public mutating func getIDToken() async throws -> String {
public func getIDToken() async throws -> String {
return try await idTokenForcingRefresh(false)
}

public mutating func idTokenForcingRefresh(_ forceRefresh: Bool, completion: ((String?, Error?) -> Void)?) {
public func idTokenForcingRefresh(_ forceRefresh: Bool, completion: ((String?, Error?) -> Void)?) {
idTokenForcingRefreshImpl(forceRefresh) { result, error in
if let completion {
DispatchQueue.main.async {
Expand All @@ -153,7 +158,7 @@ extension User {
}
}

public mutating func idTokenForcingRefresh(_ forceRefresh: Bool) async throws
public func idTokenForcingRefresh(_ forceRefresh: Bool) async throws
-> String {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<String, any Error>) in
idTokenForcingRefreshImpl(forceRefresh) { result, error in
Expand All @@ -166,8 +171,8 @@ extension User {
}
}

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

public mutating func sendEmailVerification(completion: ((Error?) -> Void)?) {
public func sendEmailVerification(completion: ((Error?) -> Void)?) {
sendEmailVerificationImpl() { error in
if let completion {
DispatchQueue.main.async {
Expand All @@ -201,7 +206,7 @@ extension User {
}
}

public mutating func sendEmailVerification() async throws {
public func sendEmailVerification() async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
sendEmailVerificationImpl() { error in
if let error {
Expand All @@ -213,9 +218,8 @@ extension User {
}
}

public mutating func sendEmailVerificationImpl(completion: @escaping (Error?) -> Void) {
//let future = self.SendEmailVerification()
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_send_email_verification(self)
public func sendEmailVerificationImpl(completion: @escaping (Error?) -> Void) {
let future = swift_firebase.swift_cxx_shims.firebase.auth.user_send_email_verification(impl)
future.setCompletion({
let (_, error) = future.resultAndError
completion(error)
Expand All @@ -242,30 +246,30 @@ extension User {

extension User: UserInfo {
public var providerID: String {
String(swift_firebase.swift_cxx_shims.firebase.auth.user_provider_id(self))
String(swift_firebase.swift_cxx_shims.firebase.auth.user_provider_id(impl))
}

public var uid: String {
String(swift_firebase.swift_cxx_shims.firebase.auth.user_uid(self))
String(swift_firebase.swift_cxx_shims.firebase.auth.user_uid(impl))
}

public var displayName: String? {
let name = String(swift_firebase.swift_cxx_shims.firebase.auth.user_display_name(self))
let name = String(swift_firebase.swift_cxx_shims.firebase.auth.user_display_name(impl))
return name.isEmpty ? nil : name
}

public var photoURL: URL? {
let url = String(swift_firebase.swift_cxx_shims.firebase.auth.user_photo_url(self))
let url = String(swift_firebase.swift_cxx_shims.firebase.auth.user_photo_url(impl))
return url.isEmpty ? nil : URL(string: url)
}

public var email: String? {
let email = String(swift_firebase.swift_cxx_shims.firebase.auth.user_email(self))
let email = String(swift_firebase.swift_cxx_shims.firebase.auth.user_email(impl))
return email.isEmpty ? nil : email
}

public var phoneNumber: String? {
let number = String(swift_firebase.swift_cxx_shims.firebase.auth.user_phone_number(self))
let number = String(swift_firebase.swift_cxx_shims.firebase.auth.user_phone_number(impl))
return number.isEmpty ? nil : number
}
}
Loading