Visitar URL original
refactor(auth,apple): migrate iOS/macOS plugin implementation to Swif… · firebase/flutterfire@8d97b56 · GitHub
Skip to content

Commit 8d97b56

Browse files
authored
refactor(auth,apple): migrate iOS/macOS plugin implementation to Swift (#18581)
* refactor(auth,apple): migrate iOS/macOS plugin implementation to Swift * fix(auth,apple): restore Apple credential switch case and format Swift * fix(auth,apple): ignore camelCase lint on UIKit URLContexts parameter * fix(auth,apple): use lowerCamelCase for openURLContexts internal parameter * fix(auth,apple): use current Firebase Auth Swift APIs
1 parent a54439f commit 8d97b56

53 files changed

Lines changed: 4844 additions & 6592 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/firebase_auth/firebase_auth/ios/firebase_auth.podspec‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Pod::Spec.new do |s|
2525
s.authors = 'The Chromium Authors'
2626
s.source = { :path => '.' }
2727

28-
s.source_files = 'firebase_auth/Sources/firebase_auth/**/*.{h,m}'
29-
s.public_header_files = 'firebase_auth/Sources/firebase_auth/include/Public/**/*.h'
30-
s.private_header_files = 'firebase_auth/Sources/firebase_auth/include/Private/**/*.h'
28+
s.source_files = 'firebase_auth/Sources/firebase_auth/**/*.swift'
29+
30+
s.swift_version = '5.0'
3131

3232
s.ios.deployment_target = '15.0'
3333
s.dependency 'Flutter'

‎packages/firebase_auth/firebase_auth/ios/firebase_auth/Package.swift‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ let package = Package(
3535
.process("Resources")
3636
],
3737
cSettings: [
38-
.headerSearchPath("include/Private"),
39-
.headerSearchPath("include/Public"),
4038
.define("LIBRARY_VERSION", to: "\"\(libraryVersion)\""),
4139
.define("LIBRARY_NAME", to: "\"flutter-fire-auth\""),
4240
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2025 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
let kFLTFirebaseAuthChannelName = "plugins.flutter.io/firebase_auth"
6+
let kFirebaseAuthLibraryName = "flutter-fire-auth"
7+
let kFirebaseAuthLibraryVersion = "6.5.7"
8+
9+
let kSignInMethodPassword = "password"
10+
let kSignInMethodEmailLink = "emailLink"
11+
let kSignInMethodFacebook = "facebook.com"
12+
let kSignInMethodGoogle = "google.com"
13+
let kSignInMethodGameCenter = "gc.apple.com"
14+
let kSignInMethodTwitter = "twitter.com"
15+
let kSignInMethodGithub = "github.com"
16+
let kSignInMethodApple = "apple.com"
17+
let kSignInMethodPhone = "phone"
18+
let kSignInMethodOAuth = "oauth"
19+
20+
let kErrCodeNoCurrentUser = "no-current-user"
21+
let kErrMsgNoCurrentUser = "No user currently signed in."
22+
let kErrCodeInvalidCredential = "invalid-credential"
23+
let kErrMsgInvalidCredential =
24+
"The supplied auth credential is malformed, has expired or is not currently supported."
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2025 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import FirebaseAuth
6+
import Foundation
7+
8+
#if os(iOS)
9+
import Flutter
10+
#elseif os(macOS)
11+
import FlutterMacOS
12+
#endif
13+
14+
enum AuthErrors {
15+
static func convertToFlutterError(_ error: Error?) -> FlutterError {
16+
var code = "unknown"
17+
var message = "An unknown error has occurred."
18+
19+
guard let error = error as NSError? else {
20+
return FlutterError(code: code, message: message, details: [:])
21+
}
22+
23+
if let firebaseErrorCode = error.userInfo[AuthErrorUserInfoNameKey] as? String {
24+
code = firebaseErrorCode.replacingOccurrences(of: "ERROR_", with: "")
25+
.replacingOccurrences(of: "_", with: "-")
26+
.lowercased()
27+
}
28+
29+
if let localized = error.userInfo[NSLocalizedDescriptionKey] as? String {
30+
message = localized
31+
}
32+
33+
var additionalData: [String: Any] = [:]
34+
if let email = error.userInfo[AuthErrorUserInfoEmailKey] as? String {
35+
additionalData["email"] = email
36+
}
37+
38+
let token = FLTFirebaseAuthPlugin.storeAuthCredentialIfPresent(error)
39+
if let authCredential = error.userInfo[AuthErrorUserInfoUpdatedCredentialKey] as? AuthCredential
40+
{
41+
additionalData["authCredential"] = PigeonParser.getPigeonAuthCredential(
42+
authCredential, token: token)
43+
}
44+
45+
if message == "The password must be 6 characters long or more." {
46+
message = "Password should be at least 6 characters"
47+
}
48+
49+
return FlutterError(code: code, message: message, details: additionalData)
50+
}
51+
52+
static func convertAppleAuthorizationErrorToFlutterError(_ error: Error) -> FlutterError {
53+
let nsError = error as NSError
54+
var message = "An unknown error has occurred."
55+
if !nsError.localizedDescription.isEmpty {
56+
message = nsError.localizedDescription
57+
}
58+
59+
var additionalData: [String: Any] = [:]
60+
let nativeErrorDomain = nsError.domain.isEmpty ? "unknown" : nsError.domain
61+
additionalData["nativeErrorDomain"] = nativeErrorDomain
62+
additionalData["nativeErrorCode"] = nsError.code
63+
64+
var underlyingMessage = ""
65+
if let underlyingError = nsError.userInfo[NSUnderlyingErrorKey] as? NSError {
66+
let underlyingErrorDomain =
67+
underlyingError.domain.isEmpty ? "unknown" : underlyingError.domain
68+
additionalData["underlyingNativeErrorDomain"] = underlyingErrorDomain
69+
additionalData["underlyingNativeErrorCode"] = underlyingError.code
70+
underlyingMessage =
71+
", Underlying Domain=\(underlyingErrorDomain) Code=\(underlyingError.code)"
72+
}
73+
74+
let detailMessage =
75+
"\(message) (Domain=\(nativeErrorDomain) Code=\(nsError.code)\(underlyingMessage))"
76+
return FlutterError(code: "unknown", message: detailMessage, details: additionalData)
77+
}
78+
79+
static func noCurrentUser() -> FlutterError {
80+
FlutterError(code: kErrCodeNoCurrentUser, message: kErrMsgNoCurrentUser, details: nil)
81+
}
82+
83+
static func invalidCredential() -> FlutterError {
84+
FlutterError(
85+
code: kErrCodeInvalidCredential, message: kErrMsgInvalidCredential, details: nil)
86+
}
87+
}

‎packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/FLTAuthStateChannelStreamHandler.m‎

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2021 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import FirebaseAuth
6+
import Foundation
7+
8+
#if os(iOS)
9+
import Flutter
10+
#elseif os(macOS)
11+
import FlutterMacOS
12+
#endif
13+
14+
final class FLTAuthStateChannelStreamHandler: NSObject, FlutterStreamHandler {
15+
private let auth: Auth
16+
private var handle: AuthStateDidChangeListenerHandle?
17+
18+
init(auth: Auth) {
19+
self.auth = auth
20+
}
21+
22+
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink)
23+
-> FlutterError?
24+
{
25+
var initialAuthState = true
26+
handle = auth.addStateDidChangeListener { auth, user in
27+
if initialAuthState {
28+
initialAuthState = false
29+
return
30+
}
31+
32+
if user != nil, let currentUser = auth.currentUser {
33+
events(["user": PigeonParser.getManualList(PigeonParser.getPigeonDetails(currentUser))])
34+
} else {
35+
events(["user": NSNull()])
36+
}
37+
}
38+
return nil
39+
}
40+
41+
func onCancel(withArguments arguments: Any?) -> FlutterError? {
42+
if let handle {
43+
auth.removeStateDidChangeListener(handle)
44+
}
45+
handle = nil
46+
return nil
47+
}
48+
}

0 commit comments

Comments
 (0)