6.7 KiB
Identity Creation Migration
Overview
This document describes the migration of automatic identity creation from individual view components to a centralized router navigation guard. This change ensures that user identities are created consistently regardless of entry point, improving the user experience and reducing code duplication.
Problem Statement
Previously, automatic identity creation was scattered across multiple view components:
HomeView.vue
- Primary entry pointInviteOneAcceptView.vue
- Deep link entry pointContactsView.vue
- Contact managementOnboardMeetingMembersView.vue
- Meeting setup
This approach had several issues:
- Inconsistent behavior - Different entry points could have different identity creation logic
- Code duplication - Similar identity creation code repeated across multiple components
- Race conditions - Multiple components could attempt identity creation simultaneously
- Maintenance overhead - Changes to identity creation required updates in multiple files
Solution: Router Navigation Guard
Implementation
The solution moves identity creation to a global router navigation guard in src/router/index.ts
:
router.beforeEach(async (to, from, next) => {
try {
// Skip identity check for certain routes
const skipIdentityRoutes = ['/start', '/new-identifier', '/import-account', '/database-migration'];
if (skipIdentityRoutes.includes(to.path)) {
return next();
}
// Check if user has any identities
const allMyDids = await retrieveAccountDids();
// Create identity if none exists
if (allMyDids.length === 0) {
logger.info("[Router] No identities found, creating default seed-based identity");
await generateSaveAndActivateIdentity();
}
next();
} catch (error) {
logger.error("[Router] Identity creation failed:", error);
next('/start'); // Redirect to manual identity creation
}
});
Benefits
- Centralized Logic - All identity creation happens in one place
- Consistent Behavior - Same identity creation process regardless of entry point
- Early Execution - Identity creation happens before any view loads
- Error Handling - Centralized error handling with fallback to manual creation
- Maintainability - Single point of change for identity creation logic
Migration Details
Files Modified
-
src/router/index.ts
- Added global
beforeEach
navigation guard - Added identity creation logic with error handling
- Added route exclusions for manual identity creation
- Added global
-
src/views/HomeView.vue
- Removed automatic identity creation logic
- Removed
isCreatingIdentifier
state and UI - Simplified
initializeIdentity()
method - Added fallback error handling
-
src/views/InviteOneAcceptView.vue
- Kept identity creation as fallback for deep links
- Added logging for fallback scenarios
- Simplified logic since router guard handles most cases
-
src/views/ContactsView.vue
- Kept identity creation as fallback for invite processing
- Added logging for fallback scenarios
- Simplified logic since router guard handles most cases
-
src/views/OnboardMeetingMembersView.vue
- Kept identity creation as fallback for meeting setup
- Added logging for fallback scenarios
- Simplified logic since router guard handles most cases
Route Exclusions
The following routes are excluded from automatic identity creation:
/start
- Manual identity creation selection/new-identifier
- Manual seed-based identity creation/import-account
- Manual account import/database-migration
- Database migration process
Fallback Strategy
For deep link scenarios and edge cases, individual views retain minimal identity creation logic as fallbacks:
- Only triggers if
activeDid
is missing - Includes logging to identify when fallbacks are used
- Maintains backward compatibility
Testing Considerations
Test Scenarios
-
First-time user navigation
- Navigate to any route without existing identity
- Verify automatic identity creation
- Verify proper navigation to intended route
-
Existing user navigation
- Navigate to any route with existing identity
- Verify no unnecessary identity creation
- Verify normal navigation flow
-
Manual identity creation routes
- Navigate to
/start
,/new-identifier
,/import-account
- Verify no automatic identity creation
- Verify manual creation flow works
- Navigate to
-
Error scenarios
- Simulate identity creation failure
- Verify fallback to
/start
route - Verify error logging
-
Deep link scenarios
- Test invite acceptance without existing identity
- Verify fallback identity creation works
- Verify proper invite processing
Performance Impact
- Positive: Reduced code duplication and simplified view logic
- Minimal: Router guard adds negligible overhead
- Improved: Consistent identity creation timing
Security Considerations
Privacy Preservation
- Identity creation still uses the same secure seed generation
- No changes to cryptographic implementation
- Maintains user privacy and data sovereignty
Error Handling
- Centralized error handling prevents identity creation failures from breaking the app
- Fallback to manual creation ensures users can always create identities
- Proper logging for debugging and monitoring
Future Enhancements
Potential Improvements
-
Identity Type Selection
- Allow users to choose identity type during automatic creation
- Support for different identity creation methods
-
Progressive Enhancement
- Add identity creation progress indicators
- Improve user feedback during creation process
-
Advanced Fallbacks
- Implement more sophisticated fallback strategies
- Add retry logic for failed identity creation
-
Analytics Integration
- Track identity creation success rates
- Monitor fallback usage patterns
Rollback Plan
If issues arise, the migration can be rolled back by:
- Removing the router navigation guard from
src/router/index.ts
- Restoring automatic identity creation in individual views
- Reverting to the previous implementation pattern
Conclusion
This migration successfully centralizes identity creation logic while maintaining backward compatibility and improving the overall user experience. The router navigation guard approach provides a robust, maintainable solution that ensures consistent identity creation across all entry points.