Skip to content

Commit

Permalink
basic auth plugin interface with support for anon logins
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanpodila committed Apr 8, 2024
1 parent 0dea3e6 commit 3b17fbf
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 63 deletions.
63 changes: 0 additions & 63 deletions packages/system/vyuh_core/lib/plugin/auth.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<void> deleteAccount() async {
controller.add(User.anonymous);
}

@override
Future<void> loginAnonymously() async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithApple() async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithEmailLink(
{required String email, required String link}) async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithEmailPassword(
{required String username, required String password}) async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithGithub() async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithGoogle() async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithLinkedin() async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithMeta() async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithMicrosoft() async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithPhoneOtp(
{required String phoneNumber, required String otp}) async {
controller.add(User.anonymous);
}

@override
Future<void> loginWithTwitter() async {
controller.add(User.anonymous);
}

@override
Future<void> logout() async {
controller.add(User.anonymous);
}

@override
Future<void> sendEmailLink(
{required String email, required String clientId}) async {}
}
102 changes: 102 additions & 0 deletions packages/system/vyuh_core/lib/plugin/auth/auth_plugin.dart
Original file line number Diff line number Diff line change
@@ -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<User>.broadcast();

@protected
var _initialized = false;

AuthPlugin({required super.name, required super.title})
: super(pluginType: PluginType.auth);

Stream<User> get user {
if (!_initialized) {
throw StateError('Plugin not initialized');
}

return controller.stream;
}

@override
@mustCallSuper
Future<void> dispose() async {
_initialized = false;
unawaited(controller.close());
}

@override
@mustCallSuper
Future<void> init() async {
if (_initialized) {
return;
}

controller = StreamController<User>.broadcast();

_initialized = true;
}

Future<void> loginAnonymously();

Future<void> loginWithPhoneOtp(
{required String phoneNumber, required String otp});
Future<void> loginWithEmailPassword(
{required String username, required String password});
Future<void> sendEmailLink({required String email, required String clientId});
Future<void> loginWithEmailLink(
{required String email, required String link});

Future<void> loginWithGoogle();
Future<void> loginWithMeta();
Future<void> loginWithApple();
Future<void> loginWithTwitter();
Future<void> loginWithGithub();
Future<void> loginWithLinkedin();
Future<void> loginWithMicrosoft();

Future<void> logout();

Future<void> 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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -242,6 +243,7 @@ extension on PluginType {
PluginType.content => NoOpContentPlugin(),
PluginType.di => GetItDIPlugin(),
PluginType.network => HttpNetworkPlugin(),
PluginType.auth => AnonymousAuthPlugin(),
_ => null
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ abstract class VyuhPlatform {
PluginType.di,
PluginType.analytics,
PluginType.network,
PluginType.auth,
// PluginType.storage,
];

Expand Down Expand Up @@ -65,6 +66,8 @@ extension NamedPlugins on VyuhPlatform {

NetworkPlugin get network => ensurePlugin<NetworkPlugin>(PluginType.network);

AuthPlugin get auth => ensurePlugin<AuthPlugin>(PluginType.auth);

T ensurePlugin<T>(PluginType type, {bool mustExist = true}) {
final plugin = getPlugin(type) as T;

Expand Down
1 change: 1 addition & 0 deletions packages/system/vyuh_core/lib/vyuh_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 3b17fbf

Please sign in to comment.