Skip to content

Commit

Permalink
Expose User.reauthenticate (#41)
Browse files Browse the repository at this point in the history
This implements one of the missing methods - `User.reauthenticate()` in the `swift-firebase` API.

The implementation is pretty standard wrapper.

Additionally `EmailAuthProvider`, `Credential`, `AuthResult` as very basic typealiases, so that they can be passed around in the reauthentication calls we want to do in Arc.

On top of that `EmailAuthProvider.credential` is provided to build the `Credential` in a way that is compatible with macos swift version of this library.
  • Loading branch information
fiedukow authored Mar 7, 2024
1 parent cc1dfa6 commit 7153928
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
18 changes: 18 additions & 0 deletions Sources/FirebaseAuth/FirebaseEmailAuthProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: BSD-3-Clause

@_exported
import firebase
@_spi(FirebaseInternal)
import FirebaseCore

import CxxShim
import Foundation

public typealias EmailAuthProvider = firebase.auth.EmailAuthProvider
public typealias Credential = firebase.auth.Credential

extension EmailAuthProvider {
static func credential(withEmail email: String, password: String) -> Credential {
GetCredential(email, password)
}
}
26 changes: 22 additions & 4 deletions Sources/FirebaseAuth/FirebaseUser+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import firebase
import FirebaseCore

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

public protocol UserInfo {
var providerID: String { get }
Expand All @@ -19,6 +20,7 @@ public protocol UserInfo {
var phoneNumber: String? { get }
}

// TODO(WPP-1581): Improve the API to match the ObjC one better.
extension User {
public var isAnonymous: Bool {
self.is_anonymous()
Expand Down Expand Up @@ -84,10 +86,26 @@ extension User {
}
}

// public mutating func reauthenticate(with credential: AuthCredential) async throws
// -> AuthResult {
// fatalError("\(#function) not yet implemented")
// }
public mutating func reauthenticate(with credential: Credential) async throws
-> AuthResult {
typealias Promise = CheckedContinuation<firebase.auth.AuthResult, any Error>
return try await withCheckedThrowingContinuation { (continuation: Promise) in
let future = self.ReauthenticateAndRetrieveData(credential)
withUnsafePointer(to: continuation) { continuation in
future.OnCompletion_SwiftWorkaround({ future, pvContinuation in
let pContinuation = pvContinuation?.assumingMemoryBound(to: Promise.self)
if future.pointee.error() == 0 {
pContinuation.pointee.resume(returning: future.pointee.__resultUnsafe().pointee)
} else {
let code = future.pointee.error()
let message = String(cString: future.pointee.__error_messageUnsafe()!)
pContinuation.pointee.resume(throwing: FirebaseError(code: code, message: message))
}
}, UnsafeMutableRawPointer(mutating: continuation))
}
future.Wait(firebase.FutureBase.kWaitTimeoutInfinite)
}
}

// -reauthenticateWithProvider:UIDelegate:completion:

Expand Down

0 comments on commit 7153928

Please sign in to comment.