ds_magic_auth_provider 0.0.1
ds_magic_auth_provider: ^0.0.1 copied to clipboard
DartStream package for Magic authentication provider.
DartStream Magic Authentication Provider #
A DartStream authentication provider implementing Magic Authentication services.
Features #
- Passwordless authentication via Magic DID tokens
- Token management
- Session handling
- Error mapping
- Event system integration
- Lifecycle management
Installation #
- Add to your
pubspec.yaml:
dependencies:
# Magic Authentication provider for DartStream
ds_magic_auth_provider: ^0.0.3
# Base authentication package
ds_auth_base: ^0.0.1
# Framework features
ds_standard_features: ^0.0.8
Usage #
Basic Setup #
import 'package:ds_auth_base/ds_auth_base_export.dart';
import 'package:ds_magic_auth_provider/ds_magic_auth_provider.dart';
// Initialize Magic Provider with your keys
final magicProvider = DSMagicAuthProvider(
publishableKey: 'your-magic-publishable-key',
secretKey: 'your-magic-secret-key',
);
await magicProvider.initialize({
'publishableKey': 'your-magic-publishable-key',
'secretKey': 'your-magic-secret-key',
});
// Register with DartStream Auth Manager
DSAuthManager.registerProvider('magic', magicProvider);
Architecture #
Components #
-
Provider (
ds_magic_auth_provider.dart)- Implements the
DSAuthProviderinterface - Integrates with Magic’s DID token API
- Implements the
-
Token Manager (
ds_token_manager.dart)- Handles DID token storage, validation, expiration
-
Session Manager (
ds_session_manager.dart)- Manages user sessions in memory
-
Error Mapper (
ds_error_mapper.dart)- Maps Magic API errors to
DSAuthError
- Maps Magic API errors to
-
Event Handlers (
ds_event_handlers.dart)- Hooks for login/logout events
Implementation Status #
Completed Features #
- ✅ Initialization (
initialize) - ✅ Passwordless sign-in (
signInusing DID token) - ✅ Sign-out (
signOut) - ✅ Current user retrieval (
getCurrentUser) - ✅ Token verification (
verifyToken) - ✅ Session management via
DSSessionManager - ✅ Error mapping with
DSMagicErrorMapper - ✅ Lifecycle hooks (
onLoginSuccess,onLogout)
Limitations & Notes #
- No
getUsersupport: Magic API doesn’t provide direct user lookup; store user info after sign-in if needed. - No
refreshToken: DID tokens are short-lived; re-authentication required. - No MFA/Groups/Audit Logs: Not offered by Magic’s API.
Enhanced Usage Examples #
// Magic link flow: front-end obtains DID token and sends it to backend
final didToken = await magicSdk.loginWithMagicLink('user@example.com');
await magicProvider.signIn('user@example.com', didToken);
// Listen to login success event
magicProvider.onLoginSuccess((user) {
print('User signed in: ${user.uid}');
});
// Validate token
final valid = await magicProvider.verifyToken(didToken);
print('Token valid: $valid');
Testing #
Running Tests #
# Run all tests for Magic provider
cd packages/standard/standard_extensions/auth/providers/magic
dart test
Test Coverage #
- Passwordless Authentication flows
- Token management edge cases
- Session creation/removal
- Error handling scenarios
- Lifecycle event triggers
Production Setup #
-
Obtain Magic Keys
- Create a Magic account and get publishable & secret keys
-
Secure Storage
- Store keys in environment variables or Secret Manager
-
Configure Provider
export MAGIC_PUBLISHABLE_KEY="your-key" export MAGIC_SECRET_KEY="your-secret"
Framework Integration #
DartStream Backend #
// In your main.dart or bootstrap
configureLogging(Tier.enterprise);
await magicProvider.initialize(...);
DSAuthManager.registerProvider('magic', magicProvider);
Flutter Integration #
import 'package:ds_flutter_mobile/ds_flutter_mobile.dart';
await DSFlutterMobileCore.initialize(
defaultAuthProvider: 'magic',
enableLogging: true,
);
Security Features #
- No passwords stored or transmitted
- DID token verification via Magic API
- In-memory session handling
- Standardized error reporting
Troubleshooting #
- Uninitialized provider: Ensure
initializeis called before use. - Invalid DID token: Verify token with
verifyTokenmethod. - Missing hooks: Check that event handlers are registered.
Performance Considerations #
- Lightweight in-memory session manager
- No heavy Crypto operations on every request
- Single HTTP call to Magic API for token verification
Status: Production Ready 🚀 #
The Magic authentication provider is complete and production-ready with passwordless login, robust session management, and comprehensive testing.