Skip to content

Commit

Permalink
feat: introduce AuthProvider widget
Browse files Browse the repository at this point in the history
Rename AuthProvider to AuthManager and introduce AuthProvider wrapping widget

BREAKING CHANGE: Rename AuthProvider to AuthManager
  • Loading branch information
iamchathu committed May 16, 2022
1 parent 475b6ad commit bc02bcb
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 58 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.5

* Update dev dependencies.
* Add simple example project.

## 0.0.4

* Upgrade dev dependencies.
Expand Down
2 changes: 1 addition & 1 deletion example/lib/auth/type_def.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter_auth_provider/flutter_auth_provider.dart';
import 'package:flutter_auth_provider_example/auth/user.dart';

typedef MyAuthProvider = AuthProvider<User>;
typedef MyAuthManager = AuthManager<User>;
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import 'package:provider/provider.dart';

void main() {
runApp(
ChangeNotifierProvider(
create: (_) => AuthProvider<User>(SecureStore())..initialize(),
AuthProvider<User>(
store: SecureStore(),
child: const MyApp(),
),
);
Expand All @@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
final bool isAuthenticated = context.watch<MyAuthProvider>().isLoggedIn;
final bool isAuthenticated = context.watch<MyAuthManager>().isLoggedIn;
if (isAuthenticated) {
return const AuthenticatedApp();
} else {
Expand Down
2 changes: 1 addition & 1 deletion example/lib/screens/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Dashboard extends StatelessWidget {
actions: [
IconButton(
onPressed: () {
context.read<MyAuthProvider>().logout();
context.read<MyAuthManager>().logout();
},
icon: const Icon(Icons.logout),
)
Expand Down
2 changes: 1 addition & 1 deletion example/lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Login extends StatelessWidget {

@override
Widget build(BuildContext context) {
final AuthProvider<User> authProvider = context.read<AuthProvider<User>>();
final AuthManager<User> authProvider = context.read<AuthManager<User>>();
return Scaffold(
appBar: AppBar(
title: const Text("Login"),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.4"
version: "0.0.5"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
1 change: 1 addition & 0 deletions lib/flutter_auth_provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library flutter_auth_provider;

export 'package:flutter_auth_provider/src/auth_manager.dart';
export 'package:flutter_auth_provider/src/auth_provider.dart';
export 'package:flutter_auth_provider/src/auth_store.dart';
export 'package:flutter_auth_provider/src/listener.dart';
Expand Down
63 changes: 16 additions & 47 deletions lib/src/auth_provider.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_auth_provider/src/auth_store.dart';
import 'package:flutter_auth_provider/src/core.dart';
import 'package:flutter_auth_provider/src/listener.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_auth_provider/flutter_auth_provider.dart';
import 'package:provider/provider.dart';

/// Auth provider.
class AuthProvider<U> extends AuthService<U> with ChangeNotifier {
final List<LoginListener<U>> _loginListeners = [];
final List<LogoutListener> _logoutListeners = [];
class AuthProvider<U> extends StatelessWidget {
final AuthStore<U> store;
final Widget child;

/// Initialize AuthProvide with [AuthStore] implementation to store logged in [U].
AuthProvider(AuthStore<U> store) : super(store);
const AuthProvider({
Key? key,
required this.store,
required this.child,
}) : super(key: key);

/// This function should be called after user [U] credentials got authenticated.
/// Authenticated [user] must be provided.
@override
Future<void> onLogin(U user) async {
super.onLogin(user);
for (LoginListener listener in _loginListeners) {
listener.onLogin(user);
}
}

/// This function should be called when you want to logged out current user.
/// Store will get cleared and [LogoutListener] will get called.
@override
Future<void> logout() async {
super.logout();
for (LogoutListener listener in _logoutListeners) {
listener.onLogout();
}
}

@override
void setUser(U? user) {
super.setUser(user);
notifyListeners();
}

/// Add [listener] to LoginListeners.
/// The listeners will get called with logged in user [U].
/// Setting up Sentry user and other login related activities can be done using the listener.
void addLoginListener(LoginListener<U> onLogin) {
_loginListeners.add(onLogin);
}

/// Add [listener] to LogoutListeners.
/// The listeners will get called when user logout is called.
/// Clearing Sentry user other cache clearing can be done using the listener
void addLogoutListener(LogoutListener onLogout) {
_logoutListeners.add(onLogout);
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => AuthManager<U>(store)..initialize(),
child: child,
);
}
}
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.0"
nested:
dependency: transitive
description:
name: nested
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
package_config:
dependency: transitive
description:
Expand All @@ -228,6 +235,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.1"
provider:
dependency: "direct main"
description:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.2"
pub_semver:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_auth_provider
description: Simple, customizable authentication manager for Flutter
version: 0.0.4
version: 0.0.5
homepage: https://github.com/iamchathu/flutter_auth_provider

environment:
Expand All @@ -10,6 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
provider: ^6.0.2


dev_dependencies:
Expand Down
6 changes: 3 additions & 3 deletions test/flutter_auth_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import 'testing/fake_store.dart';
import 'testing/mock_listeners.dart';

void main() {
group(AuthProvider, () {
group(AuthManager, () {
late AuthStore<String> authStore;
late AuthProvider<String> authProvider;
late AuthManager<String> authProvider;

setUp(() {
authStore = FakeAuthStore<String>();
authProvider = AuthProvider<String>(authStore);
authProvider = AuthManager<String>(authStore);
});

test('initially isLoggedIn is false', () {
Expand Down

0 comments on commit bc02bcb

Please sign in to comment.