Skip to content

Commit

Permalink
feat: Flag as available for registration with duplicate providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed May 10, 2024
1 parent cf22540 commit e9770d5
Show file tree
Hide file tree
Showing 21 changed files with 902 additions and 557 deletions.
16 changes: 8 additions & 8 deletions packages/katana_auth/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,18 @@ packages:
dependency: transitive
description:
name: json_annotation
sha256: b10a7b2ff83d83c777edba3c6a0f97045ddadd56c944e1a23a3fdf43a1bf4467
sha256: "1ce844379ca14835a50d2f019a3099f419082cfdd231cd86a142af94dd5c6bb1"
url: "https://pub.dev"
source: hosted
version: "4.8.1"
version: "4.9.0"
json_serializable:
dependency: "direct dev"
description:
name: json_serializable
sha256: aa1f5a8912615733e0fdc7a02af03308933c93235bdc8d50d0b0c8a8ccb0b969
sha256: ea1432d167339ea9b5bb153f0571d0039607a873d6e04e0117af043f14a1fd4b
url: "https://pub.dev"
source: hosted
version: "6.7.1"
version: "6.8.0"
katana:
dependency: "direct overridden"
description:
Expand All @@ -373,14 +373,14 @@ packages:
path: ".."
relative: true
source: path
version: "2.8.5"
version: "2.8.6"
katana_logger:
dependency: "direct overridden"
description:
path: "../../katana_logger"
relative: true
source: path
version: "2.2.4"
version: "2.2.5"
leak_tracker:
dependency: transitive
description:
Expand Down Expand Up @@ -806,10 +806,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: "0a989dc7ca2bb51eac91e8fd00851297cfffd641aa7538b165c62637ca0eaa4a"
sha256: "0eaf06e3446824099858367950a813472af675116bf63f008a4c2a75ae13e9cb"
url: "https://pub.dev"
source: hosted
version: "5.4.0"
version: "5.5.0"
xdg_directories:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AnonymouslySignInAuthProvider extends SignInAuthProvider {
/// {@macro sign_in_auth_provider}
///
/// {@macro anonymously_auth}
const AnonymouslySignInAuthProvider();
const AnonymouslySignInAuthProvider({super.allowMultiProvider = true});
@override
// ignore: avoid_field_initializers_in_const_classes
final String providerId = _kAnonymouslyAuthProviderId;
Expand Down
1 change: 1 addition & 0 deletions packages/katana_auth/lib/provider/direct_auth_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DirectSignInAuthProvider extends SignInAuthProvider {
/// {@macro direct_auth}
const DirectSignInAuthProvider({
required this.userId,
super.allowMultiProvider = true,
});

/// User ID to be specified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class EmailAndPasswordSignInAuthProvider extends SignInAuthProvider {
const EmailAndPasswordSignInAuthProvider({
required this.email,
required this.password,
super.allowMultiProvider = true,
});

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class EmailLinkSignInAuthProvider extends SignInAuthProvider {
required this.email,
required this.url,
this.locale,
super.allowMultiProvider = true,
});

@override
Expand Down
1 change: 1 addition & 0 deletions packages/katana_auth/lib/provider/sms_auth_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class SmsSignInAuthProvider extends SignInAuthProvider {
this.locale,
this.onAutoVerificationCompleted,
this.onAutoVerificationFailed,
super.allowMultiProvider = true,
});

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class SnsSignInAuthProvider extends SignInAuthProvider {
///
/// SNSのOAuth認証を行うための`AuthQuery`
/// {@endtemplate}
const SnsSignInAuthProvider();
const SnsSignInAuthProvider({super.allowMultiProvider = true});

/// Obtain credentials for SNS sign-in.
///
Expand Down
140 changes: 114 additions & 26 deletions packages/katana_auth/lib/src/auth_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,14 @@ class AuthDatabase {
}
if (provider is EmailAndPasswordRegisterAuthProvider) {
final accounts = _data._getAccounts();
if (accounts
.any((element) => element.get(userEmailKey, "") == provider.email)) {
throw Exception(
"This Email address is already registered. Please register another email address.",
);
final account = accounts.firstWhereOrNull(
(element) => element.get(userEmailKey, "") == provider.email);
if (account != null) {
if (!provider.allowMultiProvider) {
throw Exception(
"This Email address is already registered. Please register another email address.",
);
}
}
final userId = _uuid;
_data._setAccount(
Expand Down Expand Up @@ -508,24 +511,36 @@ class AuthDatabase {
_activeId = userId;
} else {
final current = _data._getAccount(userId);
final providers = [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!provider.allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...current,
activeProvidersKey: [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct(),
activeProvidersKey: providers,
});
_activeId = userId;
}
} else {
final userId = _activeId!;
final current = _data._getAccount(userId);
final providers = [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!provider.allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...current,
activeProvidersKey: [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct(),
activeProvidersKey: providers,
});
}
} else if (provider is AnonymouslySignInAuthProvider ||
Expand All @@ -545,26 +560,38 @@ class AuthDatabase {
_activeId = userId;
} else {
final current = _data._getAccount(userId);
final providers = [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!provider.allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...current,
anonymouslyKey: true,
activeProvidersKey: [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct(),
activeProvidersKey: providers,
});
_activeId = userId;
}
} else {
final userId = _activeId!;
final current = _data._getAccount(userId);
final providers = [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!provider.allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...current,
anonymouslyKey: true,
activeProvidersKey: [
...current.getAsList(activeProvidersKey),
provider.providerId
].distinct(),
activeProvidersKey: providers,
});
}
} else if (provider is EmailAndPasswordSignInAuthProvider) {
Expand Down Expand Up @@ -593,15 +620,30 @@ class AuthDatabase {
emailLinkUrlKey: provider.url,
tmpUserEmailKey: provider.email,
});
_data._setTemporary(userId);
_data._setTemporary(
userId,
allowMultiProvider: provider.allowMultiProvider,
);
} else {
final userId = account.get(userIdKey, "");
final providers = [
...account.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!provider.allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...account,
emailLinkUrlKey: provider.url,
tmpUserEmailKey: provider.email,
});
_data._setTemporary(userId);
_data._setTemporary(
userId,
allowMultiProvider: provider.allowMultiProvider,
);
}
} else if (provider is SmsSignInAuthProvider) {
final account = accounts.firstWhereOrNull(
Expand All @@ -619,15 +661,30 @@ class AuthDatabase {
tmpUserPhoneNumberKey: provider.phoneNumber,
smsCodeKey: code,
});
_data._setTemporary(userId);
_data._setTemporary(
userId,
allowMultiProvider: provider.allowMultiProvider,
);
} else {
final userId = account.get(userIdKey, "");
final providers = [
...account.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!provider.allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...account,
tmpUserPhoneNumberKey: provider.phoneNumber,
smsCodeKey: code,
});
_data._setTemporary(userId);
_data._setTemporary(
userId,
allowMultiProvider: provider.allowMultiProvider,
);
}
}
await onSaved?.call(this);
Expand Down Expand Up @@ -656,7 +713,17 @@ class AuthDatabase {
);
}
final userId = temporary.get(userIdKey, "");
final allowMultiProvider = _data._getTemporaryAllowMultiProvider();
_data._clearTemporary();
final providers = [
...temporary.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...temporary,
AuthDatabase.userEmailKey: email,
Expand All @@ -681,7 +748,17 @@ class AuthDatabase {
);
}
final userId = temporary.get(userIdKey, "");
final allowMultiProvider = _data._getTemporaryAllowMultiProvider();
_data._clearTemporary();
final providers = [
...temporary.getAsList(activeProvidersKey),
provider.providerId
].distinct();
if (!allowMultiProvider && providers.length > 1) {
throw Exception(
"Signing in with multiple providers is not allowed.",
);
}
_data._setAccount(userId, {
...temporary,
AuthDatabase.userPhoneNumberKey: phoenNumber,
Expand Down Expand Up @@ -997,6 +1074,7 @@ class AuthDatabase {
extension _AuthDatabaseDynamicMapExtensions on Map {
static const _currentKey = "current";
static const _temporaryKey = "temporary";
static const _temporaryAllowMultiProviderKey = "temporaryAllowMultiProvider";
static const _accountKey = "account";

void _initialize() {
Expand Down Expand Up @@ -1034,17 +1112,27 @@ extension _AuthDatabaseDynamicMapExtensions on Map {
return _getAccount(get(_temporaryKey, ""));
}

void _setTemporary(String? userId) {
void _setTemporary(
String? userId, {
bool allowMultiProvider = true,
}) {
if (userId.isEmpty) {
return;
}
_initialize();
this[_temporaryKey] = userId;
this[_temporaryAllowMultiProviderKey] = allowMultiProvider;
}

bool _getTemporaryAllowMultiProvider() {
_initialize();
return get(_temporaryAllowMultiProviderKey, true);
}

void _clearTemporary() {
_initialize();
this[_temporaryKey] = null;
this[_temporaryAllowMultiProviderKey] = null;
}

DynamicMap _getCurrent() {
Expand Down
26 changes: 24 additions & 2 deletions packages/katana_auth/lib/src/auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@ abstract class RegisterAuthProvider extends AuthProvider {
/// {@template register_auth_provider}
/// [AuthProvider] for performing [Authentication.register].
///
/// [allowMultiProvider] is a flag that allows multiple providers to be used.
///
/// [Authentication.register]を実行するための[AuthProvider]
///
/// [allowMultiProvider]は複数のプロバイダーを利用することを許可するフラグです。
/// {@endtemplate}
const RegisterAuthProvider();
const RegisterAuthProvider({
this.allowMultiProvider = true,
});

/// Flag that allows multiple providers to be used.
///
/// 複数のプロバイダーを利用することを許可するフラグ。
final bool allowMultiProvider;
}

/// {@template sign_in_auth_provider}
Expand All @@ -64,9 +75,20 @@ abstract class SignInAuthProvider extends AuthProvider {
/// {@template sign_in_auth_provider}
/// [AuthProvider] for performing [Authentication.signIn].
///
/// [allowMultiProvider] is a flag that allows multiple providers to be used.
///
/// [Authentication.signIn]を実行するための[AuthProvider]
///
/// [allowMultiProvider]は複数のプロバイダーを利用することを許可するフラグです。
/// {@endtemplate}
const SignInAuthProvider();
const SignInAuthProvider({
this.allowMultiProvider = true,
});

/// Flag that allows multiple providers to be used.
///
/// 複数のプロバイダーを利用することを許可するフラグ。
final bool allowMultiProvider;
}

/// {@template confirm_sign_in_auth_provider}
Expand Down

0 comments on commit e9770d5

Please sign in to comment.