feat: centralize identity creation with router navigation guard
Migrate automatic identity creation from scattered view components to centralized router navigation guard for consistent behavior across all entry points. **Key Changes:** - Add global beforeEach navigation guard in router/index.ts - Remove automatic identity creation from HomeView, ContactsView, InviteOneAcceptView, and OnboardMeetingMembersView - Keep minimal fallback logic in deep link scenarios with logging - Exclude manual identity creation routes (/start, /new-identifier, /import-account) **Benefits:** - Eliminates code duplication and race conditions - Ensures consistent identity creation regardless of entry point - Centralizes error handling with fallback to manual creation - Improves maintainability with single point of change **Files Modified:** - src/router/index.ts: Add navigation guard with identity creation logic - src/views/HomeView.vue: Remove automatic creation, simplify initializeIdentity() - src/views/ContactsView.vue: Add fallback with logging - src/views/InviteOneAcceptView.vue: Add fallback with logging - src/views/OnboardMeetingMembersView.vue: Add fallback with logging **Testing:** - Verified first-time user navigation creates identity automatically - Confirmed existing users bypass creation logic - Validated manual creation routes remain unaffected - Tested deep link scenarios with fallback logic **Documentation:** - Created docs/identity-creation-migration.md with comprehensive details - Includes migration rationale, implementation details, testing scenarios - Documents security considerations and rollback plan Resolves inconsistent identity creation behavior across different app entry points.
This commit is contained in:
@@ -6,6 +6,10 @@ import {
|
||||
RouteRecordRaw,
|
||||
} from "vue-router";
|
||||
import { logger } from "../utils/logger";
|
||||
import {
|
||||
retrieveAccountDids,
|
||||
generateSaveAndActivateIdentity,
|
||||
} from "../libs/util";
|
||||
|
||||
const routes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
@@ -316,6 +320,65 @@ const errorHandler = (
|
||||
|
||||
router.onError(errorHandler); // Assign the error handler to the router instance
|
||||
|
||||
/**
|
||||
* Global navigation guard to ensure user identity exists
|
||||
*
|
||||
* This guard checks if the user has any identities before navigating to most routes.
|
||||
* If no identity exists, it automatically creates one using the default seed-based method.
|
||||
*
|
||||
* Routes that are excluded from this check:
|
||||
* - /start - Manual identity creation selection
|
||||
* - /new-identifier - Manual seed-based creation
|
||||
* - /import-account - Manual import flow
|
||||
* - /import-derive - Manual derivation flow
|
||||
* - /database-migration - Migration utilities
|
||||
* - /deep-link-error - Error page
|
||||
*
|
||||
* @param to - Target route
|
||||
* @param from - Source route
|
||||
* @param next - Navigation function
|
||||
*/
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
try {
|
||||
// Skip identity check for routes that handle identity creation manually
|
||||
const skipIdentityRoutes = [
|
||||
"/start",
|
||||
"/new-identifier",
|
||||
"/import-account",
|
||||
"/import-derive",
|
||||
"/database-migration",
|
||||
"/deep-link-error",
|
||||
];
|
||||
|
||||
if (skipIdentityRoutes.includes(to.path)) {
|
||||
return next();
|
||||
}
|
||||
|
||||
// Check if user has any identities
|
||||
const allMyDids = await retrieveAccountDids();
|
||||
|
||||
if (allMyDids.length === 0) {
|
||||
logger.info("[Router] No identities found, creating default identity");
|
||||
|
||||
// Create identity automatically using seed-based method
|
||||
await generateSaveAndActivateIdentity();
|
||||
|
||||
logger.info("[Router] Default identity created successfully");
|
||||
}
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
"[Router] Identity creation failed in navigation guard:",
|
||||
error,
|
||||
);
|
||||
|
||||
// Redirect to start page if identity creation fails
|
||||
// This allows users to manually create an identity or troubleshoot
|
||||
next("/start");
|
||||
}
|
||||
});
|
||||
|
||||
// router.beforeEach((to, from, next) => {
|
||||
// console.log("Navigating to view:", to.name);
|
||||
// console.log("From view:", from.name);
|
||||
|
||||
Reference in New Issue
Block a user