diff --git a/packages/system/vyuh_core/lib/plugin/auth.dart b/packages/system/vyuh_core/lib/plugin/auth.dart deleted file mode 100644 index 82b0afbf..00000000 --- a/packages/system/vyuh_core/lib/plugin/auth.dart +++ /dev/null @@ -1,63 +0,0 @@ -import 'package:vyuh_core/vyuh_core.dart'; - -abstract base class AuthPlugin extends Plugin { - AuthPlugin({required super.name, required super.title}) - : super(pluginType: PluginType.auth); - - Future loginAnonymously(); - - Future loginWithPhoneOtp({required String phoneNumber}); - Future loginWithEmailPassword( - {required String username, required String password}); - Future sendEmailLink({required String email, required String clientId}); - Future loginWithEmailLink( - {required String email, required String link}); - - Future loginWithGoogle(); - Future loginWithMeta(); - Future loginWithApple(); - Future loginWithTwitter(); - Future loginWithGithub(); - Future loginWithLinkedin(); - Future loginWithMicrosoft(); - - Future logout(); - - Future deleteAccount(); -} - -class User { - final String id; - final String? name; - final String? email; - final String? phoneNumber; - final LoginMethod loginMethod; - - const User({ - required this.id, - this.name, - this.email, - this.phoneNumber, - this.loginMethod = LoginMethod.anonymous, - }); - - bool get isAnonymous => this == anonymous; - - static const anonymous = User( - id: 'anonymous', - ); -} - -enum LoginMethod { - anonymous, - emailPassword, - phoneOtp, - emailLink, - google, - meta, - apple, - twitter, - github, - linkedin, - microsoft, -} diff --git a/packages/system/vyuh_core/lib/plugin/auth/anonymous_auth_plugin.dart b/packages/system/vyuh_core/lib/plugin/auth/anonymous_auth_plugin.dart new file mode 100644 index 00000000..5889dd26 --- /dev/null +++ b/packages/system/vyuh_core/lib/plugin/auth/anonymous_auth_plugin.dart @@ -0,0 +1,83 @@ +import 'dart:async'; + +import 'package:vyuh_core/vyuh_core.dart'; + +final class AnonymousAuthPlugin extends AuthPlugin { + AnonymousAuthPlugin() + : super( + title: 'Anonymous Auth Plugin', + name: 'vyuh.plugin.auth.anonymous', + ); + + @override + Future deleteAccount() async { + controller.add(User.anonymous); + } + + @override + Future loginAnonymously() async { + controller.add(User.anonymous); + } + + @override + Future loginWithApple() async { + controller.add(User.anonymous); + } + + @override + Future loginWithEmailLink( + {required String email, required String link}) async { + controller.add(User.anonymous); + } + + @override + Future loginWithEmailPassword( + {required String username, required String password}) async { + controller.add(User.anonymous); + } + + @override + Future loginWithGithub() async { + controller.add(User.anonymous); + } + + @override + Future loginWithGoogle() async { + controller.add(User.anonymous); + } + + @override + Future loginWithLinkedin() async { + controller.add(User.anonymous); + } + + @override + Future loginWithMeta() async { + controller.add(User.anonymous); + } + + @override + Future loginWithMicrosoft() async { + controller.add(User.anonymous); + } + + @override + Future loginWithPhoneOtp( + {required String phoneNumber, required String otp}) async { + controller.add(User.anonymous); + } + + @override + Future loginWithTwitter() async { + controller.add(User.anonymous); + } + + @override + Future logout() async { + controller.add(User.anonymous); + } + + @override + Future sendEmailLink( + {required String email, required String clientId}) async {} +} diff --git a/packages/system/vyuh_core/lib/plugin/auth/auth_plugin.dart b/packages/system/vyuh_core/lib/plugin/auth/auth_plugin.dart new file mode 100644 index 00000000..91cf87cd --- /dev/null +++ b/packages/system/vyuh_core/lib/plugin/auth/auth_plugin.dart @@ -0,0 +1,102 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:vyuh_core/vyuh_core.dart'; + +abstract base class AuthPlugin extends Plugin { + @protected + var controller = StreamController.broadcast(); + + @protected + var _initialized = false; + + AuthPlugin({required super.name, required super.title}) + : super(pluginType: PluginType.auth); + + Stream get user { + if (!_initialized) { + throw StateError('Plugin not initialized'); + } + + return controller.stream; + } + + @override + @mustCallSuper + Future dispose() async { + _initialized = false; + unawaited(controller.close()); + } + + @override + @mustCallSuper + Future init() async { + if (_initialized) { + return; + } + + controller = StreamController.broadcast(); + + _initialized = true; + } + + Future loginAnonymously(); + + Future loginWithPhoneOtp( + {required String phoneNumber, required String otp}); + Future loginWithEmailPassword( + {required String username, required String password}); + Future sendEmailLink({required String email, required String clientId}); + Future loginWithEmailLink( + {required String email, required String link}); + + Future loginWithGoogle(); + Future loginWithMeta(); + Future loginWithApple(); + Future loginWithTwitter(); + Future loginWithGithub(); + Future loginWithLinkedin(); + Future loginWithMicrosoft(); + + Future logout(); + + Future deleteAccount(); +} + +class User { + final String id; + final String? name; + final String? email; + final String? phoneNumber; + final String? photoUrl; + final LoginMethod loginMethod; + + const User({ + required this.id, + this.name, + this.email, + this.phoneNumber, + this.photoUrl, + this.loginMethod = LoginMethod.anonymous, + }); + + bool get isAnonymous => this == anonymous; + + static const anonymous = User( + id: 'anonymous', + ); +} + +enum LoginMethod { + anonymous, + emailPassword, + phoneOtp, + emailLink, + google, + meta, + apple, + twitter, + github, + linkedin, + microsoft, +} diff --git a/packages/system/vyuh_core/lib/runtime/platform/default_platform.dart b/packages/system/vyuh_core/lib/runtime/platform/default_platform.dart index 7d399ab5..46cd3616 100644 --- a/packages/system/vyuh_core/lib/runtime/platform/default_platform.dart +++ b/packages/system/vyuh_core/lib/runtime/platform/default_platform.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart' as flutter; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart' as g; import 'package:mobx/mobx.dart'; +import 'package:vyuh_core/plugin/auth/anonymous_auth_plugin.dart'; import 'package:vyuh_core/runtime/platform/fallback_route_page_builder.dart'; import 'package:vyuh_core/runtime/platform/framework_init_view.dart'; import 'package:vyuh_core/runtime/platform/platform_init_tracker.dart'; @@ -242,6 +243,7 @@ extension on PluginType { PluginType.content => NoOpContentPlugin(), PluginType.di => GetItDIPlugin(), PluginType.network => HttpNetworkPlugin(), + PluginType.auth => AnonymousAuthPlugin(), _ => null }; } diff --git a/packages/system/vyuh_core/lib/runtime/platform/vyuh_platform.dart b/packages/system/vyuh_core/lib/runtime/platform/vyuh_platform.dart index 29d17f65..fff58e01 100644 --- a/packages/system/vyuh_core/lib/runtime/platform/vyuh_platform.dart +++ b/packages/system/vyuh_core/lib/runtime/platform/vyuh_platform.dart @@ -34,6 +34,7 @@ abstract class VyuhPlatform { PluginType.di, PluginType.analytics, PluginType.network, + PluginType.auth, // PluginType.storage, ]; @@ -65,6 +66,8 @@ extension NamedPlugins on VyuhPlatform { NetworkPlugin get network => ensurePlugin(PluginType.network); + AuthPlugin get auth => ensurePlugin(PluginType.auth); + T ensurePlugin(PluginType type, {bool mustExist = true}) { final plugin = getPlugin(type) as T; diff --git a/packages/system/vyuh_core/lib/vyuh_core.dart b/packages/system/vyuh_core/lib/vyuh_core.dart index 9f4138a2..78a5409b 100644 --- a/packages/system/vyuh_core/lib/vyuh_core.dart +++ b/packages/system/vyuh_core/lib/vyuh_core.dart @@ -11,6 +11,7 @@ export 'feature_descriptor.dart'; export 'plugin/analytics/analytics_plugin.dart'; export 'plugin/analytics/analytics_provider.dart'; export 'plugin/analytics/noop_analytics_provider.dart'; +export 'plugin/auth/auth_plugin.dart'; export 'plugin/content/content_plugin.dart'; export 'plugin/content/content_provider.dart'; export 'plugin/content/noop_content_plugin.dart';