Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the authentication views in FirebaseAuthSwiftUI to support full UI customization by introducing customizable screen wrappers (such as EmailAuthScreen and MFAEnrolmentScreen) and extracting the stock UI into default content views. It also adds a comprehensive multi-theme customization example to the sample application. The review feedback highlights critical improvements, including reusing a static CIContext for QR code generation to prevent performance degradation, and replacing a force cast with safe optional casting to avoid potential runtime crashes.
| private func generateQRCode(from string: String) -> UIImage? { | ||
| let data = Data(string.utf8) | ||
|
|
||
| guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil } | ||
| filter.setValue(data, forKey: "inputMessage") | ||
| filter.setValue("H", forKey: "inputCorrectionLevel") | ||
|
|
||
| guard let ciImage = filter.outputImage else { return nil } | ||
|
|
||
| // Scale up the QR code for better quality | ||
| let transform = CGAffineTransform(scaleX: 10, y: 10) | ||
| let scaledImage = ciImage.transformed(by: transform) | ||
|
|
||
| let context = CIContext() | ||
| guard let cgImage = context.createCGImage(scaledImage, from: scaledImage.extent) else { | ||
| return nil | ||
| } | ||
|
|
||
| return UIImage(cgImage: cgImage) | ||
| } |
There was a problem hiding this comment.
Creating a new CIContext on every invocation of generateQRCode(from:) is highly inefficient and can cause UI stuttering/performance degradation because CIContext is expensive to instantiate. It is recommended to reuse a single shared/static CIContext instance.
private static let qrContext = CIContext()
private func generateQRCode(from string: String) -> UIImage? {
let data = Data(string.utf8)
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
filter.setValue(data, forKey: "inputMessage")
filter.setValue("H", forKey: "inputCorrectionLevel")
guard let ciImage = filter.outputImage else { return nil }
// Scale up the QR code for better quality
let transform = CGAffineTransform(scaleX: 10, y: 10)
let scaledImage = ciImage.transformed(by: transform)
guard let cgImage = Self.qrContext.createCGImage(scaledImage, from: scaledImage.extent) else {
return nil
}
return UIImage(cgImage: cgImage)
}There was a problem hiding this comment.
Fixed on main, which now shares a static qrCodeContext. After the rebase this file is no longer part of this PR.
| if factor.factorID == PhoneMultiFactorID { | ||
| let phoneInfo = factor as! PhoneMultiFactorInfo | ||
| Text("SMS: \(phoneInfo.phoneNumber)") | ||
| .authFont(.caption) | ||
| .foregroundColor(.secondary) | ||
| } else { | ||
| Text("Authenticator App") | ||
| .authFont(.caption) | ||
| .foregroundColor(.secondary) | ||
| } |
There was a problem hiding this comment.
Force casting factor as! PhoneMultiFactorInfo can lead to runtime crashes if the factor is not of the expected type. It is safer and more idiomatic to use safe optional casting (as?) to prevent potential crashes.
if let phoneInfo = factor as? PhoneMultiFactorInfo {
Text("SMS: \(phoneInfo.phoneNumber)")
.authFont(.caption)
.foregroundColor(.secondary)
} else {
Text("Authenticator App")
.authFont(.caption)
.foregroundColor(.secondary)
}There was a problem hiding this comment.
Fixed on main, which now uses as? PhoneMultiFactorInfo here. After the rebase this file is no longer part of this PR.
| private struct DemoQRCode: View { | ||
| let image: UIImage | ||
|
|
||
| init?(url: URL) { | ||
| let filter = CIFilter.qrCodeGenerator() | ||
| filter.message = Data(url.absoluteString.utf8) | ||
| guard let output = filter.outputImage?.transformed(by: CGAffineTransform(scaleX: 8, y: 8)), | ||
| let cgImage = CIContext().createCGImage(output, from: output.extent) else { | ||
| return nil | ||
| } | ||
| image = UIImage(cgImage: cgImage) | ||
| } |
There was a problem hiding this comment.
Instantiating a new CIContext inside the initializer of DemoQRCode is inefficient because SwiftUI views are frequently recreated. Reusing a single static CIContext avoids this overhead.
| private struct DemoQRCode: View { | |
| let image: UIImage | |
| init?(url: URL) { | |
| let filter = CIFilter.qrCodeGenerator() | |
| filter.message = Data(url.absoluteString.utf8) | |
| guard let output = filter.outputImage?.transformed(by: CGAffineTransform(scaleX: 8, y: 8)), | |
| let cgImage = CIContext().createCGImage(output, from: output.extent) else { | |
| return nil | |
| } | |
| image = UIImage(cgImage: cgImage) | |
| } | |
| private struct DemoQRCode: View { | |
| private static let qrContext = CIContext() | |
| let image: UIImage | |
| init?(url: URL) { | |
| let filter = CIFilter.qrCodeGenerator() | |
| filter.message = Data(url.absoluteString.utf8) | |
| guard let output = filter.outputImage?.transformed(by: CGAffineTransform(scaleX: 8, y: 8)), | |
| let cgImage = Self.qrContext.createCGImage(output, from: output.extent) else { | |
| return nil | |
| } | |
| image = UIImage(cgImage: cgImage) | |
| } |
There was a problem hiding this comment.
Fixed in 4905784: DemoQRCode now shares one static CIContext.
1847902 to
995a908
Compare
edd4d3b to
293a99f
Compare
293a99f to
1673bce
Compare
1673bce to
1255ca6
Compare
b915133 to
2c26a10
Compare
Adds a Full customization example to the SwiftUI sample. It replaces the first screen with .pickerContent { state in }, every pushed screen through its content slot and the signed-in screen with .signedInContent, while the library keeps auth, navigation, MFA and reauthentication.
Doggo, Grid and Coffee Spark themes come from the iOS Landing frames of the Firebase auth flows Figma file and switch live from any screen. Screens the frames don't cover follow the FirebaseUI-Android demo's copy. The theming modifiers restyle the stock screens left in the flow.
Starts sign-in from the other-providers sheet after it dismisses, creates the example's AuthService when it appears rather than when ContentView builds the link, anchors the mascot to the form's trailing edge, generates the TOTP QR code once per URL, disables Remove while an unenrolment runs, gives sheet providers an icon, and ships the spark background at 2x only.
The form and footer now keep the Figma frame's 332 and 334 point widths, centred, instead of fixed 49 and 48 point insets that only fit the 430 point frame and squeezed the form on narrower phones.
The background sat in the scroll view's .background, so it shrank with the scroll view and left white edges once the form had a maximum width. It is now a ZStack layer behind a full-width scroll view.
The remaining providers open from a pill styled like Google's instead of a text link, so the three buttons keep Figma's 12 point spacing. The flow switch gets a 44 point tap target, and text links get a taller tap area without changing the layout.
…sheet Providers the Figma frames don't draw now appear as the library's AuthProviderButton with each provider's own label and ProviderStyle, instead of improvised pills. The action still goes through AuthMethodPickerState.select once the sheet has closed.
The switch toggles the email form, so it now sits right below the form and above the other sign-in methods divider, rather than under the provider buttons where the Figma frames place it.
The switch text now sits 24 points from the form above and the divider below in every theme. The per-theme form-to-divider spacing it replaced is removed.
The enrolment steps move into DemoMFAEnrolmentContent so a preview can render the TOTP setup step from a stub MFAEnrollmentContentState. The Auth emulator only enrols SMS factors, so the step can't be reached against it.
Every demo screen now uses one content width: 32 points from the sheet's edges, capped at 480 points. The Figma frames' 332 point form left 49 points each side on a 430 point phone, which was too much.
A spec-defined theme with no Figma frame: the system font, neutral greys with one blue accent, 10 point corners, hairline borders, no background art, and light and dark variants. It reuses the shared layout; the theme adds button height, card border and pill fill, border and corner tokens, which the Figma themes set to their existing values.
Professional now brings its own screens through the same slots instead of reskinning the shared layout: flat, left-aligned pages with labelled fields, sign-in providers first, a separate Create account screen pushed on the .signUp route, a settings-style account page and grouped two-factor choices. The Figma themes keep the shared layout and their in-place sign-in / sign-up switch. The Professional dark accent fill is darkened so white labels keep 4.5:1 contrast.
Reset password and the two-factor method choice had a text link that only went back a screen, which the navigation bar's back button already does.
- Ask for the email when a sign-in link is opened on another device. - Respect isEmailSignInEnabled, canSignUp and requiresDisplayName, and turn on the name field. - Go back to the sign-in form from a pushed sign-up screen, and draw .signUp in the shared layout. - Keep a default case for ReauthPrompt and the MFA enrolment step. - Offer a password change only to accounts that sign in with a password. - Keep Doggo, Grid and Coffee Spark light, scale Professional text with Dynamic Type, and outline the stock fields.
262cc9c to
773a36d
Compare
Related to #1400.
Adds a full customization example to the SwiftUI sample: every auth screen drawn by the app, in four live-switchable themes. Three reskin one shared layout; Professional brings its own layout through the same slots.
Changes
.pickerContent { state in }draws the first screen;.pickerDestinationand.signedInContentreplace every other screen through its content slot..signUproute..reauthContentdraws the password, phone and email-link reauthentication prompts in each theme. The theming modifiers restyle the stock screens left: update password and delete confirmation.AuthServiceto enable email-link sign-in without changing other examples. Fonts ship with their OFL licenses.Preview
Maintainer note: Fixes internal CPRN-523