mirror of
https://github.com/hanskokx/arcane_implementations.git
synced 2026-05-14 10:29:04 +02:00
Update Amplify interface to use latest Arcane
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
import "package:amplify_auth_cognito/amplify_auth_cognito.dart";
|
import "package:amplify_auth_cognito/amplify_auth_cognito.dart";
|
||||||
import "package:amplify_flutter/amplify_flutter.dart";
|
import "package:amplify_flutter/amplify_flutter.dart";
|
||||||
import "package:arcane_framework/arcane_framework.dart";
|
import "package:arcane_framework/arcane_framework.dart";
|
||||||
|
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||||
import "package:flutter/widgets.dart";
|
import "package:flutter/widgets.dart";
|
||||||
|
|
||||||
typedef LoginInput = ({String email, String password});
|
typedef Credentials = ({String email, String password});
|
||||||
|
|
||||||
class AmplifyInterface implements ArcaneAuthInterface {
|
class AmplifyInterface
|
||||||
|
with ArcaneAuthAccountRegistration, ArcaneAuthPasswordManagement
|
||||||
|
implements ArcaneAuthInterface {
|
||||||
AmplifyInterface._internal();
|
AmplifyInterface._internal();
|
||||||
|
|
||||||
static bool _mocked = false;
|
static bool _mocked = false;
|
||||||
@@ -13,13 +16,14 @@ class AmplifyInterface implements ArcaneAuthInterface {
|
|||||||
static final ArcaneAuthInterface _instance = AmplifyInterface._internal();
|
static final ArcaneAuthInterface _instance = AmplifyInterface._internal();
|
||||||
static ArcaneAuthInterface get I => _instance;
|
static ArcaneAuthInterface get I => _instance;
|
||||||
|
|
||||||
AmplifyAuthCognito get _cognito =>
|
AmplifyAuthCognito get _cognito => Amplify.Auth.getPlugin(
|
||||||
Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey);
|
AmplifyAuthCognito.pluginKey,
|
||||||
|
);
|
||||||
|
|
||||||
Future<CognitoAuthSession?> get _session async {
|
Future<CognitoAuthSession?> get _session async {
|
||||||
try {
|
try {
|
||||||
return await _cognito.fetchAuthSession();
|
return await _cognito.fetchAuthSession();
|
||||||
} on AuthException catch (_) {
|
} on AuthException {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,13 +89,6 @@ class AmplifyInterface implements ArcaneAuthInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Result<void, String>> loginWithEmailAndPassword({
|
|
||||||
required String email,
|
|
||||||
required String password,
|
|
||||||
}) async =>
|
|
||||||
throw UnimplementedError();
|
|
||||||
|
|
||||||
Future<Result<void, String>> _handleSignInResult(
|
Future<Result<void, String>> _handleSignInResult(
|
||||||
SignInResult result,
|
SignInResult result,
|
||||||
String email,
|
String email,
|
||||||
@@ -142,8 +139,11 @@ class AmplifyInterface implements ArcaneAuthInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Result<String, String>> resendVerificationCode(String email) async {
|
Future<Result<String, String>> resendVerificationCode<T>({T? input}) async {
|
||||||
try {
|
try {
|
||||||
|
final String? email = input as String?;
|
||||||
|
if (email == null) return Result.error("No email address provided.");
|
||||||
|
|
||||||
final result = await _cognito.resendSignUpCode(
|
final result = await _cognito.resendSignUpCode(
|
||||||
username: email.toLowerCase(),
|
username: email.toLowerCase(),
|
||||||
);
|
);
|
||||||
@@ -156,11 +156,19 @@ class AmplifyInterface implements ArcaneAuthInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Result<SignUpStep, String>> signup({
|
Future<Result<SignUpStep, String>> register<Credentials>({
|
||||||
required String password,
|
Credentials? input,
|
||||||
required String email,
|
|
||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
|
if (input == null) {
|
||||||
|
return Result.error("Unable to create an account with no credentials");
|
||||||
|
}
|
||||||
|
|
||||||
|
final credentials = input as ({String email, String password});
|
||||||
|
|
||||||
|
final String email = credentials.email;
|
||||||
|
final String password = credentials.password;
|
||||||
|
|
||||||
final String accountEmail = email.toLowerCase();
|
final String accountEmail = email.toLowerCase();
|
||||||
final userAttributes = {
|
final userAttributes = {
|
||||||
AuthUserAttributeKey.email: accountEmail,
|
AuthUserAttributeKey.email: accountEmail,
|
||||||
@@ -185,13 +193,25 @@ class AmplifyInterface implements ArcaneAuthInterface {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Result<bool, String>> confirmSignup({
|
Future<Result<bool, String>> confirmSignup({
|
||||||
required String username,
|
String? username,
|
||||||
required String confirmationCode,
|
String? confirmationCode,
|
||||||
}) async {
|
}) async {
|
||||||
|
if (username.isNullOrEmpty) {
|
||||||
|
return Result.error(
|
||||||
|
"Unable to confirm account due to an empty username.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (confirmationCode.isNullOrEmpty) {
|
||||||
|
return Result.error(
|
||||||
|
"Unable to confirm account due to an empty confirmation code.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final CognitoSignUpResult result = await _cognito.confirmSignUp(
|
final CognitoSignUpResult result = await _cognito.confirmSignUp(
|
||||||
username: username.toLowerCase(),
|
username: username!.toLowerCase(),
|
||||||
confirmationCode: confirmationCode,
|
confirmationCode: confirmationCode!,
|
||||||
);
|
);
|
||||||
|
|
||||||
return Result.ok(result.isSignUpComplete);
|
return Result.ok(result.isSignUpComplete);
|
||||||
@@ -202,15 +222,18 @@ class AmplifyInterface implements ArcaneAuthInterface {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Result<bool, String>> resetPassword({
|
Future<Result<bool, String>> resetPassword({
|
||||||
required String email,
|
String? email,
|
||||||
String? newPassword,
|
String? newPassword,
|
||||||
String? code,
|
String? code,
|
||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
|
if (email.isNullOrEmpty) {
|
||||||
|
return Result.error("Email address is empty.");
|
||||||
|
}
|
||||||
late ResetPasswordResult result;
|
late ResetPasswordResult result;
|
||||||
if (newPassword != null && code != null) {
|
if (newPassword != null && code != null) {
|
||||||
result = await _cognito.confirmResetPassword(
|
result = await _cognito.confirmResetPassword(
|
||||||
username: email,
|
username: email!,
|
||||||
newPassword: newPassword,
|
newPassword: newPassword,
|
||||||
confirmationCode: code,
|
confirmationCode: code,
|
||||||
);
|
);
|
||||||
@@ -218,7 +241,7 @@ class AmplifyInterface implements ArcaneAuthInterface {
|
|||||||
|
|
||||||
if (newPassword == null && code == null) {
|
if (newPassword == null && code == null) {
|
||||||
result = await _cognito.resetPassword(
|
result = await _cognito.resetPassword(
|
||||||
username: email,
|
username: email!,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user