shogun-core 4.2.5 → 4.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134625,30 +134625,22 @@ class DataBase {
134625
134625
  }
134626
134626
  // Set the user instance
134627
134627
  this.user = this.gun.user();
134628
- // Run post-authentication tasks
134629
- try {
134630
- const postAuthResult = await this.runPostAuthOnAuthResult(username, authResult.userPub, authResult);
134631
- // Return the post-auth result which includes the complete user data
134632
- return postAuthResult;
134633
- }
134634
- catch (postAuthError) {
134635
- console.error(`Post-auth error: ${postAuthError}`);
134636
- // Even if post-auth fails, the user was created and authenticated successfully
134637
- return {
134638
- success: true,
134639
- userPub: authResult.userPub,
134640
- username: username,
134641
- isNewUser: true,
134642
- sea: this.gun.user()?._?.sea
134643
- ? {
134644
- pub: this.gun.user()._?.sea.pub,
134645
- priv: this.gun.user()._?.sea.priv,
134646
- epub: this.gun.user()._?.sea.epub,
134647
- epriv: this.gun.user()._?.sea.epriv,
134648
- }
134649
- : undefined,
134650
- };
134651
- }
134628
+ console.log(`[SIGNUP] Signup completed successfully for user: ${username}`);
134629
+ // Return the signup result
134630
+ return {
134631
+ success: true,
134632
+ userPub: authResult.userPub,
134633
+ username: username,
134634
+ isNewUser: true,
134635
+ sea: this.gun.user()?._?.sea
134636
+ ? {
134637
+ pub: this.gun.user()._?.sea.pub,
134638
+ priv: this.gun.user()._?.sea.priv,
134639
+ epub: this.gun.user()._?.sea.epub,
134640
+ epriv: this.gun.user()._?.sea.epriv,
134641
+ }
134642
+ : undefined,
134643
+ };
134652
134644
  }
134653
134645
  catch (error) {
134654
134646
  console.error(`Exception during signup for ${username}: ${error}`);
@@ -134686,475 +134678,6 @@ class DataBase {
134686
134678
  resolve({ success: true, userPub: pair.pub });
134687
134679
  });
134688
134680
  }
134689
- async runPostAuthOnAuthResult(username, userPub, authResult) {
134690
- console.log(`[POSTAUTH] Starting post-auth setup for user: ${username}, userPub: ${userPub}`);
134691
- try {
134692
- console.log(`[POSTAUTH] Validating parameters for user: ${username}`);
134693
- // Validate required parameters
134694
- if (!username ||
134695
- typeof username !== "string" ||
134696
- username.trim().length === 0) {
134697
- console.error(`[POSTAUTH] Invalid username provided: ${username}`);
134698
- throw new Error("Invalid username provided");
134699
- }
134700
- if (!userPub ||
134701
- typeof userPub !== "string" ||
134702
- userPub.trim().length === 0) {
134703
- console.error("[POSTAUTH] Invalid userPub provided:", {
134704
- userPub,
134705
- type: typeof userPub,
134706
- authResult,
134707
- });
134708
- throw new Error("Invalid userPub provided");
134709
- }
134710
- // Additional validation for userPub format
134711
- if (!userPub.includes(".") || userPub.length < 10) {
134712
- console.error(`[POSTAUTH] Invalid userPub format: ${userPub}`);
134713
- throw new Error("Invalid userPub format");
134714
- }
134715
- console.log(`[POSTAUTH] Parameters validated for user: ${username}`);
134716
- // Normalize username to prevent path issues
134717
- const normalizedUsername = username.trim().toLowerCase();
134718
- if (normalizedUsername.length === 0) {
134719
- console.error(`[POSTAUTH] Normalized username is empty for user: ${username}`);
134720
- throw new Error("Username cannot be empty");
134721
- }
134722
- console.log(`[POSTAUTH] Normalized username: ${normalizedUsername}`);
134723
- console.log(`[POSTAUTH] Checking if user exists: ${userPub}`);
134724
- // Add timeout to prevent hanging
134725
- const existingUser = await Promise.race([
134726
- this.gun.get(userPub).then(),
134727
- new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout checking user existence")), 5000)),
134728
- ]).catch((error) => {
134729
- console.error(`[POSTAUTH] Error or timeout checking user existence:`, error);
134730
- return null;
134731
- });
134732
- console.log(`[POSTAUTH] User existence check completed, existingUser:`, existingUser);
134733
- const isNewUser = !existingUser || !existingUser.alias;
134734
- console.log(`[POSTAUTH] User is ${isNewUser ? "NEW" : "EXISTING"}: ${userPub}`);
134735
- // Get user's encryption public key (epub) for comprehensive tracking
134736
- const userInstance = this.gun.user();
134737
- const userSea = userInstance?._?.sea;
134738
- const epub = userSea?.epub;
134739
- console.log(`[POSTAUTH] User epub retrieved: ${epub ? "yes" : "no"}`);
134740
- // Enhanced user tracking system
134741
- console.log(`[POSTAUTH] Setting up comprehensive user tracking for: ${normalizedUsername}`);
134742
- const trackingResult = await this.setupComprehensiveUserTracking(normalizedUsername, userPub, epub);
134743
- if (!trackingResult) {
134744
- console.error(`[POSTAUTH] Comprehensive user tracking setup failed for: ${normalizedUsername}`);
134745
- return {
134746
- success: false,
134747
- error: "Comprehensive user tracking setup failed",
134748
- };
134749
- }
134750
- console.log(`[POSTAUTH] User tracking setup completed successfully for: ${normalizedUsername}`);
134751
- const result = {
134752
- success: true,
134753
- userPub: userPub,
134754
- username: normalizedUsername,
134755
- isNewUser: isNewUser,
134756
- // Get the SEA pair from the user object
134757
- sea: userSea
134758
- ? {
134759
- pub: userSea.pub,
134760
- priv: userSea.priv,
134761
- epub: userSea.epub,
134762
- epriv: userSea.epriv,
134763
- }
134764
- : undefined,
134765
- };
134766
- console.log(`[POSTAUTH] Post-auth setup completed successfully for user: ${username}`);
134767
- return result;
134768
- }
134769
- catch (error) {
134770
- console.error(`[POSTAUTH] Error in post-authentication setup for ${username}:`, error);
134771
- return {
134772
- success: false,
134773
- error: `Post-authentication setup failed: ${error}`,
134774
- };
134775
- }
134776
- }
134777
- /**
134778
- * Sets up comprehensive user tracking system for agile user lookup
134779
- * Creates multiple indexes for efficient user discovery
134780
- */
134781
- async setupComprehensiveUserTracking(username, userPub, epub) {
134782
- console.log(`[TRACKING] Starting comprehensive user tracking setup for: ${username}, userPub: ${userPub}`);
134783
- try {
134784
- // 1. Create alias index: ~@alias -> userPub (for GunDB compatibility)
134785
- console.log(`[TRACKING] Step 1: Creating alias index for ${username}`);
134786
- const aliasIndexResult = await this.createAliasIndex(username, userPub);
134787
- if (!aliasIndexResult) {
134788
- console.error(`[TRACKING] Failed to create alias index for ${username}`);
134789
- return false;
134790
- }
134791
- console.log(`[TRACKING] Step 1 completed: Alias index created for ${username}`);
134792
- // 2. Create username mapping: usernames/alias -> userPub
134793
- console.log(`[TRACKING] Step 2: Creating username mapping for ${username}`);
134794
- const usernameMappingResult = await this.createUsernameMapping(username, userPub);
134795
- if (!usernameMappingResult) {
134796
- console.error(`[TRACKING] Failed to create username mapping for ${username}`);
134797
- return false;
134798
- }
134799
- console.log(`[TRACKING] Step 2 completed: Username mapping created for ${username}`);
134800
- // 3. Create user registry: users/userPub -> user data
134801
- console.log(`[TRACKING] Step 3: Creating user registry for ${username}`);
134802
- const userRegistryResult = await this.createUserRegistry(username, userPub, epub);
134803
- if (!userRegistryResult) {
134804
- console.error(`[TRACKING] Failed to create user registry for ${username}`);
134805
- return false;
134806
- }
134807
- console.log(`[TRACKING] Step 3 completed: User registry created for ${username}`);
134808
- // 4. Create reverse lookup: userPub -> alias
134809
- console.log(`[TRACKING] Step 4: Creating reverse lookup for ${username}`);
134810
- const reverseLookupResult = await this.createReverseLookup(username, userPub);
134811
- if (!reverseLookupResult) {
134812
- console.error(`[TRACKING] Failed to create reverse lookup for ${username}`);
134813
- return false;
134814
- }
134815
- console.log(`[TRACKING] Step 4 completed: Reverse lookup created for ${username}`);
134816
- // 5. Create epub index: epubKeys/epub -> userPub (for encryption lookups)
134817
- if (epub) {
134818
- console.log(`[TRACKING] Step 5: Creating epub index for ${username}`);
134819
- const epubIndexResult = await this.createEpubIndex(epub, userPub);
134820
- if (!epubIndexResult) {
134821
- console.error(`[TRACKING] Failed to create epub index for ${username}`);
134822
- return false;
134823
- }
134824
- console.log(`[TRACKING] Step 5 completed: Epub index created for ${username}`);
134825
- }
134826
- else {
134827
- console.log(`[TRACKING] Step 5 skipped: No epub available for ${username}`);
134828
- }
134829
- // 6. Create user metadata in user's own node
134830
- console.log(`[TRACKING] Step 6: Creating user metadata for ${username}`);
134831
- const userMetadataResult = await this.createUserMetadata(username, userPub, epub);
134832
- if (!userMetadataResult) {
134833
- console.error(`[TRACKING] Failed to create user metadata for ${username}`);
134834
- return false;
134835
- }
134836
- console.log(`[TRACKING] Step 6 completed: User metadata created for ${username}`);
134837
- console.log(`[TRACKING] Comprehensive user tracking setup completed successfully for: ${username}`);
134838
- return true;
134839
- }
134840
- catch (error) {
134841
- console.error(`[TRACKING] Error in comprehensive user tracking setup for ${username}:`, error);
134842
- // Don't throw - continue with other operations
134843
- return false;
134844
- }
134845
- }
134846
- /**
134847
- * Creates alias index following GunDB pattern: ~@alias -> userPub
134848
- */
134849
- async createAliasIndex(username, userPub) {
134850
- try {
134851
- const aliasNode = this.gun.get(`~@${username}`);
134852
- // For Gun.js alias validation to pass, the data must be exactly equal to the key
134853
- // The key is `~@${username}`, so we store that as the data
134854
- return new Promise((resolve) => {
134855
- aliasNode.put(`~@${username}`, (ack) => {
134856
- if (ack && ack.err) {
134857
- console.error(`Error creating alias index: ${ack.err}`);
134858
- resolve(false);
134859
- }
134860
- else {
134861
- resolve(true);
134862
- }
134863
- });
134864
- });
134865
- }
134866
- catch (error) {
134867
- console.error(`Error creating alias index: ${error}`);
134868
- return false;
134869
- }
134870
- }
134871
- /**
134872
- * Creates username mapping: usernames/alias -> userPub
134873
- */
134874
- async createUsernameMapping(username, userPub) {
134875
- try {
134876
- return new Promise((resolve) => {
134877
- this.node
134878
- .get("usernames")
134879
- .get(username)
134880
- .put(userPub, (ack) => {
134881
- if (ack && ack.err) {
134882
- console.error(`Error creating username mapping: ${ack.err}`);
134883
- resolve(false);
134884
- }
134885
- else {
134886
- resolve(true);
134887
- }
134888
- });
134889
- });
134890
- }
134891
- catch (error) {
134892
- console.error(`Error creating username mapping: ${error}`);
134893
- return false;
134894
- }
134895
- }
134896
- /**
134897
- * Creates user registry: users/userPub -> user data
134898
- */
134899
- async createUserRegistry(username, userPub, epub) {
134900
- try {
134901
- const userData = {
134902
- username: username,
134903
- userPub: userPub,
134904
- epub: epub,
134905
- registeredAt: Date.now().toString(),
134906
- lastSeen: Date.now().toString(),
134907
- };
134908
- return new Promise((resolve) => {
134909
- this.node
134910
- .get("users")
134911
- .get(userPub)
134912
- .put(userData, (ack) => {
134913
- if (ack && ack.err) {
134914
- console.error(`Error creating user registry: ${ack.err}`);
134915
- resolve(false);
134916
- }
134917
- else {
134918
- resolve(true);
134919
- }
134920
- });
134921
- });
134922
- }
134923
- catch (error) {
134924
- console.error(`Error creating user registry: ${error}`);
134925
- return false;
134926
- }
134927
- }
134928
- /**
134929
- * Creates reverse lookup: userPub -> alias
134930
- */
134931
- async createReverseLookup(username, userPub) {
134932
- try {
134933
- const ack = await this.node
134934
- .get("userAliases")
134935
- .get(userPub)
134936
- .put(username)
134937
- .then();
134938
- if (ack.err) {
134939
- console.error(`Error creating reverse lookup: ${ack.err}`);
134940
- return false;
134941
- }
134942
- else {
134943
- return true;
134944
- }
134945
- }
134946
- catch (error) {
134947
- console.error(`Error creating reverse lookup: ${error}`);
134948
- return false;
134949
- }
134950
- }
134951
- /**
134952
- * Creates epub index: epubKeys/epub -> userPub
134953
- */
134954
- async createEpubIndex(epub, userPub) {
134955
- try {
134956
- return new Promise((resolve) => {
134957
- this.node
134958
- .get("epubKeys")
134959
- .get(epub)
134960
- .put(userPub, (ack) => {
134961
- if (ack && ack.err) {
134962
- console.error(`Error creating epub index: ${ack.err}`);
134963
- resolve(false);
134964
- }
134965
- else {
134966
- resolve(true);
134967
- }
134968
- });
134969
- });
134970
- }
134971
- catch (error) {
134972
- console.error(`Error creating epub index: ${error}`);
134973
- return false;
134974
- }
134975
- }
134976
- /**
134977
- * Creates user metadata in user's own node
134978
- */
134979
- async createUserMetadata(username, userPub, epub) {
134980
- try {
134981
- const userMetadata = {
134982
- username: username,
134983
- epub: epub,
134984
- registeredAt: Date.now(),
134985
- lastSeen: Date.now(),
134986
- };
134987
- return new Promise((resolve) => {
134988
- this.gun.get(userPub).put(userMetadata, (ack) => {
134989
- if (ack && ack.err) {
134990
- console.error(`Error creating user metadata: ${ack.err}`);
134991
- resolve(false);
134992
- }
134993
- else {
134994
- resolve(true);
134995
- }
134996
- });
134997
- });
134998
- }
134999
- catch (error) {
135000
- console.error(`Error creating user metadata: ${error}`);
135001
- return false;
135002
- }
135003
- }
135004
- /**
135005
- * Gets user information by alias using the comprehensive tracking system
135006
- * @param alias Username/alias to lookup
135007
- * @returns Promise resolving to user information or null if not found
135008
- */
135009
- async getUserByAlias(alias) {
135010
- try {
135011
- const normalizedAlias = alias.trim().toLowerCase();
135012
- if (!normalizedAlias) {
135013
- return null;
135014
- }
135015
- // Method 1: Try GunDB standard alias lookup (~@alias)
135016
- try {
135017
- const aliasData = await this.gun.get(`~@${normalizedAlias}`).then();
135018
- if (aliasData && aliasData["~pubKeyOfUser"]) {
135019
- const userPub = aliasData["~pubKeyOfUser"]["#"] || aliasData["~pubKeyOfUser"];
135020
- if (userPub) {
135021
- const userData = await this.getUserDataByPub(userPub);
135022
- if (userData) {
135023
- return userData;
135024
- }
135025
- }
135026
- }
135027
- }
135028
- catch (error) {
135029
- console.error(`GunDB alias lookup failed for ${normalizedAlias}:`, error);
135030
- }
135031
- // Method 2: Try username mapping (usernames/alias -> userPub)
135032
- try {
135033
- const userPub = await this.node
135034
- .get("usernames")
135035
- .get(normalizedAlias)
135036
- .then();
135037
- if (userPub) {
135038
- const userData = await this.getUserDataByPub(userPub);
135039
- if (userData) {
135040
- return userData;
135041
- }
135042
- }
135043
- }
135044
- catch (error) {
135045
- console.error(`Username mapping lookup failed for ${normalizedAlias}:`, error);
135046
- }
135047
- return null;
135048
- }
135049
- catch (error) {
135050
- console.error(`Error looking up user by alias ${alias}:`, error);
135051
- return null;
135052
- }
135053
- }
135054
- /**
135055
- * Gets user information by public key
135056
- * @param userPub User's public key
135057
- * @returns Promise resolving to user information or null if not found
135058
- */
135059
- async getUserDataByPub(userPub) {
135060
- try {
135061
- if (!userPub || typeof userPub !== "string") {
135062
- return null;
135063
- }
135064
- // Method 1: Try user registry (users/userPub -> user data)
135065
- try {
135066
- const userData = await this.node.get("users").get(userPub).then();
135067
- if (userData && userData.username) {
135068
- return {
135069
- userPub: userData.userPub || userPub,
135070
- epub: userData.epub || null,
135071
- username: userData.username,
135072
- registeredAt: userData.registeredAt || 0,
135073
- lastSeen: userData.lastSeen || 0,
135074
- };
135075
- }
135076
- }
135077
- catch (error) {
135078
- console.error(`User registry lookup failed for ${userPub}:`, error);
135079
- }
135080
- // Method 2: Try user's own node
135081
- try {
135082
- const userNodeData = await this.gun.get(userPub).then();
135083
- if (userNodeData && userNodeData.username) {
135084
- return {
135085
- userPub: userPub,
135086
- epub: userNodeData.epub || null,
135087
- username: userNodeData.username,
135088
- registeredAt: userNodeData.registeredAt || 0,
135089
- lastSeen: userNodeData.lastSeen || 0,
135090
- };
135091
- }
135092
- }
135093
- catch (error) {
135094
- console.error(`User node lookup failed for ${userPub}:`, error);
135095
- }
135096
- return null;
135097
- }
135098
- catch (error) {
135099
- console.error(`Error looking up user data by pub ${userPub}:`, error);
135100
- return null;
135101
- }
135102
- }
135103
- /**
135104
- * Gets user public key by encryption public key (epub)
135105
- * @param epub User's encryption public key
135106
- * @returns Promise resolving to user public key or null if not found
135107
- */
135108
- async getUserPubByEpub(epub) {
135109
- try {
135110
- if (!epub || typeof epub !== "string") {
135111
- return null;
135112
- }
135113
- const userPub = await this.node.get("epubKeys").get(epub).then();
135114
- return userPub || null;
135115
- }
135116
- catch (error) {
135117
- console.error(`Error looking up user pub by epub ${epub}:`, error);
135118
- return null;
135119
- }
135120
- }
135121
- /**
135122
- * Gets user alias by public key
135123
- * @param userPub User's public key
135124
- * @returns Promise resolving to user alias or null if not found
135125
- */
135126
- async getUserAliasByPub(userPub) {
135127
- try {
135128
- if (!userPub || typeof userPub !== "string") {
135129
- return null;
135130
- }
135131
- const alias = await this.node.get("userAliases").get(userPub).then();
135132
- return alias || null;
135133
- }
135134
- catch (error) {
135135
- console.error(`Error looking up user alias by pub ${userPub}:`, error);
135136
- return null;
135137
- }
135138
- }
135139
- /**
135140
- * Gets all registered users (for admin purposes)
135141
- * @returns Promise resolving to array of user information
135142
- */
135143
- async getAllRegisteredUsers() {
135144
- try {
135145
- const users = [];
135146
- // Get all users from the users registry
135147
- const usersNode = this.node.get("users");
135148
- // Note: This is a simplified approach. In a real implementation,
135149
- // you might want to use Gun's map functionality or iterate through
135150
- // known user public keys
135151
- return users;
135152
- }
135153
- catch (error) {
135154
- console.error(`Error getting all registered users:`, error);
135155
- return [];
135156
- }
135157
- }
135158
134681
  /**
135159
134682
  * Updates user's last seen timestamp
135160
134683
  * @param userPub User's public key
@@ -135269,17 +134792,7 @@ class DataBase {
135269
134792
  error: "Authentication failed: No user pub returned.",
135270
134793
  };
135271
134794
  }
135272
- // Pass the userPub to runPostAuthOnAuthResult
135273
- try {
135274
- await this.runPostAuthOnAuthResult(alias, userPub, {
135275
- success: true,
135276
- userPub: userPub,
135277
- });
135278
- }
135279
- catch (postAuthError) {
135280
- console.error(`Post-auth error during login: ${postAuthError}`);
135281
- // Continue with login even if post-auth fails
135282
- }
134795
+ console.log(`[LOGIN] Login completed successfully for user: ${username}`);
135283
134796
  // Update user's last seen timestamp
135284
134797
  try {
135285
134798
  await this.updateUserLastSeen(userPub);
@@ -135322,10 +134835,7 @@ class DataBase {
135322
134835
  error: `User '${username}' not found. Please check your username or register first.`,
135323
134836
  };
135324
134837
  }
135325
- await this.runPostAuthOnAuthResult(username, pair.pub || "", {
135326
- success: true,
135327
- userPub: pair.pub,
135328
- });
134838
+ console.log(`[LOGIN] Login with pair completed for user: ${username}`);
135329
134839
  try {
135330
134840
  await this.updateUserLastSeen(pair.pub);
135331
134841
  }