|
|
%!s(int64=4) %!d(string=hai) anos | |
|---|---|---|
| .. | ||
| Sources | 2d4e13962e Storage async await (#8289) | %!s(int64=4) %!d(string=hai) anos |
| Tests | 38948c6637 Merge branch 'master' into combine-main | %!s(int64=4) %!d(string=hai) anos |
| CHANGELOG.md | fda03d0f13 Update changelog | %!s(int64=4) %!d(string=hai) anos |
| DECISIONS.md | 5063d9fba5 Combine Support: Cloud Functions (#7547) | %!s(int64=5) %!d(string=hai) anos |
| DEVELOPING.md | ffa115847c Add development documentation | %!s(int64=5) %!d(string=hai) anos |
| README.md | a284f94b1c Add more details to the readme. | %!s(int64=5) %!d(string=hai) anos |
This module contains Combine support for Firebase APIs.
Note: This feature is under development and not yet supported for use in production environments. You can follow development on the project tracker
Auth.auth().signInAnonymously()
.sink { completion in
switch completion {
case .finished:
print("Finished")
case let .failure(error):
print("\(error.localizedDescription)")
}
} receiveValue: { authDataResult in
}
.store(in: &cancellables)
Auth.auth().signInAnonymously()
.map { result in
result.user.uid
}
.replaceError(with: "(unable to sign in anonymously)")
.assign(to: \.uid, on: self)
.store(in: &cancellables)
In the sign(_:didSignInFor:withError:) method, get a Google ID token and Google access token from the GIDAuthentication object and asynchronously exchange them for a Firebase credential:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
// ...
if let error = error {
// ...
return
}
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth()
.signIn(withCredential: credential)
.mapError { $0 as NSError }
.tryCatch(handleError)
.sink { /* ... */ } receiveValue: { /* ... */ }
.store(in: &subscriptions)
}
private func handleError(_ error: NSError) throws -> AnyPublisher<AuthDataResult, Error> {
guard isMFAEnabled && error.code == AuthErrorCode.secondFactorRequired.rawValue
else { throw error }
// The user is a multi-factor user. Second factor challenge is required.
let resolver = error.userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver
let displayNameString = resolver.hints.compactMap(\.displayName).joined(separator: " ")
return showTextInputPrompt(withMessage: "Select factor to sign in\n\(displayNameString)")
.compactMap { displayName in
resolver.hints.first(where: { displayName == $0.displayName }) as? PhoneMultiFactorInfo
}
.flatMap { [unowned self] factorInfo in
PhoneAuthProvider.provider()
.verifyPhoneNumber(withMultiFactorInfo: factorInfo, multiFactorSession: resolver.session)
.zip(self.showTextInputPrompt(withMessage: "Verification code for \(factorInfo.displayName ?? "")"))
.map { (verificationID, verificationCode) in
let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID,
verificationCode: verificationCode)
return PhoneMultiFactorGenerator.assertion(with: credential)
}
}
.flatMap { assertion in
resolver.resolveSignIn(withAssertion: assertion)
}
.eraseToAnyPublisher()
}
let helloWorld = Functions.functions().httpsCallable("helloWorld")
helloWorld.call()
.sink { completion in
switch completion {
case .finished:
print("Finished")
case let .failure(error):
print("\(error.localizedDescription)")
}
} receiveValue: { functionResult in
if let result = functionResult.data as? String {
print("The function returned: \(result)")
}
}
.store(in: &cancellables)
let helloWorld = Functions.functions().httpsCallable("helloWorld")
helloWorld.call("Peter")
.sink { completion in
switch completion {
case .finished:
print("Finished")
case let .failure(error):
print("\(error.localizedDescription)")
}
} receiveValue: { functionResult in
if let result = functionResult.data as? String {
print("The function returned: \(result)")
}
}
.store(in: &cancellables)