Skip to content

Commit

Permalink
fix(ui_auth): add showPasswordVisibilityToggle to the SignInScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnitsky committed Sep 22, 2023
1 parent 0f0fa80 commit 1d0201a
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 3 deletions.
29 changes: 28 additions & 1 deletion packages/firebase_ui_auth/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,34 @@ Future<void> main() async {

## macOS entitlements

If you're building for macOS, make sure to add necessary entitlements. Learn more [from the official Flutter documentation](https://docs.flutter.dev/development/platform-integration/macos/building).
If you're building for macOS, make sure to add necessary entitlements. Learn more [from the official Flutter documentation](https://docs.flutter.dev/platform-integration/macos/building#setting-up-entitlements).

## Writing widget unit tests

If you're writing widget unit tests, you'll need to add the following to your `setUpAll` method:

```dart
setUpAll(() {
setFirebaseUiIsTestMode(true);
});
```

Also, you will likely want to mock the `FirebaseAuth` instance:

```dart
class MockFirebaseAuth extends Mock implements FirebaseAuth {
/// mock necessary methods
}
```

An instance of `MockFirebaseAuth` can then be passed to Firebase UI widgets:

```dart
SignInScreen(
auth: MockFirebaseAuth(),
/// ... other props
)
```

## Next steps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "generated_plugin_registrant.h"

#include <desktop_webview_auth/desktop_webview_auth_plugin.h>
#include <firebase_auth/firebase_auth_plugin_c_api.h>
#include <firebase_core/firebase_core_plugin_c_api.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
DesktopWebviewAuthPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktopWebviewAuthPlugin"));
FirebaseAuthPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseAuthPluginCApi"));
FirebaseCorePluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseCorePluginCApi"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
desktop_webview_auth
firebase_auth
firebase_core
flutter_secure_storage_windows
)
Expand Down
10 changes: 9 additions & 1 deletion packages/firebase_ui_auth/lib/firebase_ui_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export 'src/widgets/sign_out_button.dart';
export 'src/widgets/sms_code_input.dart' show SMSCodeInputState, SMSCodeInput;
export 'src/widgets/user_avatar.dart';

bool _isTestMode = false;

@visibleForTesting
void setFirebaseUiIsTestMode(bool isTestMode) {
_isTestMode = isTestMode;
}

class FirebaseUIAuth {
static final _providers = <FirebaseApp, List<AuthProvider>>{};
static final _configuredApps = <FirebaseApp, bool>{};
Expand All @@ -104,14 +111,15 @@ class FirebaseUIAuth {
}

static bool isAppConfigured(FirebaseApp app) {
if (_isTestMode) return true;
return _providers.containsKey(app);
}

static void configureProviders(
List<AuthProvider> configs, {
FirebaseApp? app,
}) {
if (Firebase.apps.isEmpty) {
if (!_isTestMode && Firebase.apps.isEmpty) {
throw Exception(
'You must call Firebase.initializeApp() '
'before calling configureProviders()',
Expand Down
5 changes: 5 additions & 0 deletions packages/firebase_ui_auth/lib/src/screens/sign_in_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class SignInScreen extends MultiProviderScreen {
/// * [EmailFormStyle]
final Set<FirebaseUIStyle>? styles;

/// {@macro ui.auth.widgets.email_form.showPasswordVisibilityToggle}
final bool showPasswordVisibilityToggle;

/// {@macro ui.auth.screens.sign_in_screen}
const SignInScreen({
super.key,
Expand All @@ -114,6 +117,7 @@ class SignInScreen extends MultiProviderScreen {
this.actions = const [],
this.breakpoint = 800,
this.styles,
this.showPasswordVisibilityToggle = false,
});

Future<void> _signInWithDifferentProvider(
Expand Down Expand Up @@ -165,6 +169,7 @@ class SignInScreen extends MultiProviderScreen {
subtitleBuilder: subtitleBuilder,
footerBuilder: footerBuilder,
breakpoint: breakpoint,
showPasswordVisibilityToggle: showPasswordVisibilityToggle,
),
);
}
Expand Down
43 changes: 43 additions & 0 deletions packages/firebase_ui_auth/test/screens/sign_in_screen_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../test_utils.dart';

void main() {
group("$SignInScreen", () {
setUpAll(() {
setFirebaseUiIsTestMode(true);
});

testWidgets(
"doesn't have password visibility toggle by default",
(tester) async {
await tester.pumpWidget(
TestMaterialApp(
child: SignInScreen(
auth: MockAuth(),
providers: [EmailAuthProvider()],
),
),
);

expect(find.byIcon(Icons.visibility), findsNothing);
},
);

testWidgets('allows to add password visibility toggle', (tester) async {
await tester.pumpWidget(
TestMaterialApp(
child: SignInScreen(
auth: MockAuth(),
providers: [EmailAuthProvider()],
showPasswordVisibilityToggle: true,
),
),
);

expect(find.byIcon(Icons.visibility), findsOneWidget);
});
});
}
8 changes: 8 additions & 0 deletions packages/firebase_ui_auth/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
import 'package:flutter/material.dart';
import 'package:mockito/mockito.dart';
Expand Down Expand Up @@ -60,12 +61,19 @@ class MockDynamicLinks extends Mock implements FirebaseDynamicLinks {
Stream<PendingDynamicLinkData> get onLink => _linkStream;
}

class MockApp extends Mock implements FirebaseApp {}

class MockAuth extends Mock implements FirebaseAuth {
MockUser? user;

@override
User? get currentUser => user;

@override
FirebaseApp get app => MockApp();

List<FirebaseApp> get apps => [app];

@override
Future<UserCredential> signInWithCredential(
AuthCredential? credential,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../test_utils.dart';
import '../test_utils.dart';

void main() {
group("$LoginView", () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#include "generated_plugin_registrant.h"

#include <desktop_webview_auth/desktop_webview_auth_plugin.h>
#include <firebase_auth/firebase_auth_plugin_c_api.h>
#include <firebase_core/firebase_core_plugin_c_api.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
DesktopWebviewAuthPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktopWebviewAuthPlugin"));
FirebaseAuthPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseAuthPluginCApi"));
FirebaseCorePluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseCorePluginCApi"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
desktop_webview_auth
firebase_auth
firebase_core
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "generated_plugin_registrant.h"

#include <desktop_webview_auth/desktop_webview_auth_plugin.h>
#include <firebase_auth/firebase_auth_plugin_c_api.h>
#include <firebase_core/firebase_core_plugin_c_api.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
DesktopWebviewAuthPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktopWebviewAuthPlugin"));
FirebaseAuthPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseAuthPluginCApi"));
FirebaseCorePluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseCorePluginCApi"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
desktop_webview_auth
firebase_auth
firebase_core
flutter_secure_storage_windows
)
Expand Down

0 comments on commit 1d0201a

Please sign in to comment.