diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f591d2..c2fb49e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.1+1 + +- Updated example in README + ## 1.1.1 - [BREAKING] Updated ArcaneAuthInterface to make the `resendVerificationCode`, `confirmSignup`, and `resetPassword` methods more versatile diff --git a/README.md b/README.md index cd7075a..513833c 100644 --- a/README.md +++ b/README.md @@ -242,57 +242,42 @@ The Arcane Framework provides a useful interface for performing common authentic To get started, create an authentication interface provider and register it in the Arcane authentication module: ```dart -typedef LoginInput = ({String email, String password}); +import "package:arcane_framework/arcane_framework.dart"; -// Create an authentication interface -class AuthProviderInterface implements ArcaneAuthInterface { - AuthProviderInterface._internal(); +typedef Credentials = ({String email, String password}); - static bool _mocked = false; +class DebugAuthInterface implements ArcaneAuthInterface { + DebugAuthInterface._internal(); - static final AuthProviderInterface _instance = AuthProviderInterface._internal(); - static AuthProviderInterface get I => _instance; - - Future get _session async { - return await ThirdPartyAuthProvider.fetchAuthSession(); - } + static final ArcaneAuthInterface _instance = DebugAuthInterface._internal(); + static ArcaneAuthInterface get I => _instance; @override - Future get isSignedIn => - _session.then((value) => value?.isSignedIn == true); + Future get isSignedIn => Future.value(_isSignedIn); + bool _isSignedIn = false; @override Future get accessToken => isSignedIn.then( - (loggedIn) => loggedIn - ? _session.then( - (value) => value?.accessToken, - ) - : null, + (loggedIn) => loggedIn ? "access_token" : null, ); @override Future get refreshToken => isSignedIn.then( - (loggedIn) => loggedIn - ? _session.then( - (value) => value?.refreshToken, - ) - : null, + (loggedIn) => loggedIn ? "refresh_token" : null, ); @override Future> logout() async { - final result = await _session.signOut(); + Arcane.log("Logging out"); - if (result is FailedSignOut) { - return Result.error(result.exception.message); - } + _isSignedIn = false; return Result.ok(null); } @override - Future> login({ - LoginInput? input, + Future> login({ + Credentials? input, Future Function()? onLoggedIn, }) async { final bool alreadyLoggedIn = await isSignedIn; @@ -304,111 +289,67 @@ class AuthProviderInterface implements ArcaneAuthInterface { final String email = credentials.email; final String password = credentials.password; - try { - final SignInResult result = await _session.signIn( - username: email, - password: password, - ); - return Result.ok(null); - } on AuthException catch (e) { - return Result.error("Error signing in: ${e.message}"); - } catch (e) { - return Result.error("Error signing in: $e"); - } + Arcane.log("Logging in as $email using password $password"); + + _isSignedIn = true; + + return Result.ok(null); } @override - Future> resendVerificationCode(String email) async { - try { - final result = await _session.resendSignUpCode(username: email); - return Result.ok(result.message); - } catch (e) { - return Result.error("Error resending verification code: ${e.message}"); - } - } - - @override - Future> signup({ - required String password, - required String email, + Future> resendVerificationCode({ + T? input, }) async { - try { - final SignUpResult result = await _session.signUp( - username: email, - password: password, - ); + Arcane.log("Re-sending verification code to $input"); + return Result.ok("Code sent"); + } - if (result.nextStep.signUpStep == AuthSignUpStep.confirmSignUp) { - return Result.ok(SignUpStep.confirmSignUp); - } + @override + Future> register({ + Credentials? input, + }) async { + if (input != null) { + final credentials = input as ({String email, String password}); - return Result.ok(SignUpStep.done); - } catch (e) { - return Result.error("Error signing up user: ${e.message}"); + final String email = credentials.email; + final String password = credentials.password; + + Arcane.log("Creating account for $email with password $password"); } + + return Result.ok(SignUpStep.confirmSignUp); } @override Future> confirmSignup({ - required String username, - required String confirmationCode, + String? username, + String? confirmationCode, }) async { - try { - final SignUpResult result = await _session.confirmSignUp( - username: username, - confirmationCode: confirmationCode, - ); - - return Result.ok(result.isSignUpComplete); - } on AuthException catch (e) { - return Result.error("Error confirming user: ${e.message}"); - } + Arcane.log( + "Confirming registration for $username with code $confirmationCode", + ); + return Result.ok(true); } @override Future> resetPassword({ - required String email, + String? email, String? newPassword, String? code, }) async { - try { - late ResetPasswordResult result; - if (newPassword != null && code != null) { - result = await _session.confirmResetPassword( - username: email, - newPassword: newPassword, - confirmationCode: code, - ); - } - - if (newPassword == null && code == null) { - result = await _session.resetPassword( - username: email, - ); - } - - return Result.ok(result.isPasswordReset); - } catch (e) { - return Result.error("Error resetting the password: ${e.message}"); - } + Arcane.log("Resetting password for $email"); + return Result.ok(true); } @override Future init() async { - if (_mocked) return; - - if (ThirdPartyAuthProvider.isConfigured) return; - - await ThirdPartyAuthProvider.initialize(); - } - - @visibleForTesting - static void setMocked() { - _mocked = true; + Arcane.log("Debug auth interface initialized."); + return; } } + // Register an interface to handle user authentication. await Arcane.auth.registerInterface(AuthProviderInterface.I); ``` diff --git a/pubspec.yaml b/pubspec.yaml index 2693942..ffce5ce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: arcane_framework description: "Agnostic Reusable Component Architecture for New Ecosystems: a modern framework for bootstrapping new applications" -version: 1.1.1 +version: 1.1.1+1 repository: https://github.com/hanskokx/arcane_framework issue_tracker: https://github.com/hanskokx/arcane_framework/issues