@oxyhq/services 5.16.44 → 5.17.1

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.
Files changed (512) hide show
  1. package/README.md +29 -10
  2. package/lib/commonjs/core/OxyServices.js +1 -1
  3. package/lib/commonjs/core/index.js +20 -15
  4. package/lib/commonjs/core/index.js.map +1 -1
  5. package/lib/commonjs/core/mixins/OxyServices.auth.js +36 -53
  6. package/lib/commonjs/core/mixins/OxyServices.auth.js.map +1 -1
  7. package/lib/commonjs/core/mixins/OxyServices.user.js +10 -17
  8. package/lib/commonjs/core/mixins/OxyServices.user.js.map +1 -1
  9. package/lib/commonjs/core/services/TokenService.js +27 -13
  10. package/lib/commonjs/core/services/TokenService.js.map +1 -1
  11. package/lib/commonjs/crypto/index.js +0 -16
  12. package/lib/commonjs/crypto/index.js.map +1 -1
  13. package/lib/commonjs/crypto/keyManager.js +21 -22
  14. package/lib/commonjs/crypto/keyManager.js.map +1 -1
  15. package/lib/commonjs/crypto/polyfill.js +1 -10
  16. package/lib/commonjs/crypto/polyfill.js.map +1 -1
  17. package/lib/commonjs/crypto/signatureService.js +18 -32
  18. package/lib/commonjs/crypto/signatureService.js.map +1 -1
  19. package/lib/commonjs/index.js +13 -134
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/commonjs/models/interfaces.js +0 -7
  22. package/lib/commonjs/models/interfaces.js.map +1 -1
  23. package/lib/commonjs/node/index.js +1 -10
  24. package/lib/commonjs/node/index.js.map +1 -1
  25. package/lib/commonjs/ui/components/BottomSheetRouter.js +1 -9
  26. package/lib/commonjs/ui/components/BottomSheetRouter.js.map +1 -1
  27. package/lib/commonjs/ui/components/OxyProvider.js +9 -20
  28. package/lib/commonjs/ui/components/OxyProvider.js.map +1 -1
  29. package/lib/commonjs/ui/context/OxyContext.js +779 -450
  30. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  31. package/lib/commonjs/ui/context/hooks/useAuthOperations.js +551 -0
  32. package/lib/commonjs/ui/context/hooks/useAuthOperations.js.map +1 -0
  33. package/lib/commonjs/ui/context/hooks/useDeviceManagement.js +73 -0
  34. package/lib/commonjs/ui/context/hooks/useDeviceManagement.js.map +1 -0
  35. package/lib/commonjs/ui/context/hooks/useStorage.js +79 -0
  36. package/lib/commonjs/ui/context/hooks/useStorage.js.map +1 -0
  37. package/lib/commonjs/ui/hooks/index.js +0 -20
  38. package/lib/commonjs/ui/hooks/index.js.map +1 -1
  39. package/lib/commonjs/ui/hooks/mutations/index.js +0 -12
  40. package/lib/commonjs/ui/hooks/mutations/index.js.map +1 -1
  41. package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js +23 -74
  42. package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js.map +1 -1
  43. package/lib/commonjs/ui/hooks/queries/index.js +0 -12
  44. package/lib/commonjs/ui/hooks/queries/index.js.map +1 -1
  45. package/lib/commonjs/ui/hooks/queries/queryKeys.js +1 -3
  46. package/lib/commonjs/ui/hooks/queries/queryKeys.js.map +1 -1
  47. package/lib/commonjs/ui/hooks/queries/useAccountQueries.js +28 -64
  48. package/lib/commonjs/ui/hooks/queries/useAccountQueries.js.map +1 -1
  49. package/lib/commonjs/ui/hooks/queries/useServicesQueries.js +6 -4
  50. package/lib/commonjs/ui/hooks/queries/useServicesQueries.js.map +1 -1
  51. package/lib/commonjs/ui/hooks/useDeviceManagement.js +73 -0
  52. package/lib/commonjs/ui/hooks/useDeviceManagement.js.map +1 -0
  53. package/lib/commonjs/ui/hooks/useProfileEditing.js +5 -3
  54. package/lib/commonjs/ui/hooks/useProfileEditing.js.map +1 -1
  55. package/lib/commonjs/ui/hooks/useSessionManagement.js +284 -0
  56. package/lib/commonjs/ui/hooks/useSessionManagement.js.map +1 -0
  57. package/lib/commonjs/ui/index.js +2 -10
  58. package/lib/commonjs/ui/index.js.map +1 -1
  59. package/lib/commonjs/ui/navigation/routes.js +1 -5
  60. package/lib/commonjs/ui/navigation/routes.js.map +1 -1
  61. package/lib/commonjs/ui/screens/AccountCenterScreen.js +4 -9
  62. package/lib/commonjs/ui/screens/AccountCenterScreen.js.map +1 -1
  63. package/lib/commonjs/ui/screens/AccountSettingsScreen.js +19 -37
  64. package/lib/commonjs/ui/screens/AccountSettingsScreen.js.map +1 -1
  65. package/lib/commonjs/ui/screens/AccountSwitcherScreen.js +5 -5
  66. package/lib/commonjs/ui/screens/AccountSwitcherScreen.js.map +1 -1
  67. package/lib/commonjs/ui/screens/OxyAuthScreen.js +15 -2
  68. package/lib/commonjs/ui/screens/OxyAuthScreen.js.map +1 -1
  69. package/lib/commonjs/ui/screens/PrivacySettingsScreen.js +97 -76
  70. package/lib/commonjs/ui/screens/PrivacySettingsScreen.js.map +1 -1
  71. package/lib/commonjs/ui/screens/ProfileScreen.js +6 -6
  72. package/lib/commonjs/ui/screens/ProfileScreen.js.map +1 -1
  73. package/lib/commonjs/ui/stores/authStore.js +6 -54
  74. package/lib/commonjs/ui/stores/authStore.js.map +1 -1
  75. package/lib/commonjs/ui/styles/spacing.js +2 -54
  76. package/lib/commonjs/ui/styles/spacing.js.map +1 -1
  77. package/lib/commonjs/ui/utils/avatarUtils.js +12 -9
  78. package/lib/commonjs/ui/utils/avatarUtils.js.map +1 -1
  79. package/lib/commonjs/ui/utils/sessionHelpers.js +1 -7
  80. package/lib/commonjs/ui/utils/sessionHelpers.js.map +1 -1
  81. package/lib/commonjs/utils/deviceManager.js +177 -0
  82. package/lib/commonjs/utils/deviceManager.js.map +1 -0
  83. package/lib/commonjs/utils/errorUtils.js +0 -13
  84. package/lib/commonjs/utils/errorUtils.js.map +1 -1
  85. package/lib/commonjs/utils/index.js +7 -0
  86. package/lib/commonjs/utils/index.js.map +1 -1
  87. package/lib/commonjs/utils/sessionUtils.js +1 -8
  88. package/lib/commonjs/utils/sessionUtils.js.map +1 -1
  89. package/lib/commonjs/utils/validationUtils.js +1 -15
  90. package/lib/commonjs/utils/validationUtils.js.map +1 -1
  91. package/lib/module/core/OxyServices.js +1 -1
  92. package/lib/module/core/index.js +4 -6
  93. package/lib/module/core/index.js.map +1 -1
  94. package/lib/module/core/mixins/OxyServices.auth.js +36 -53
  95. package/lib/module/core/mixins/OxyServices.auth.js.map +1 -1
  96. package/lib/module/core/mixins/OxyServices.user.js +10 -17
  97. package/lib/module/core/mixins/OxyServices.user.js.map +1 -1
  98. package/lib/module/core/services/TokenService.js +27 -13
  99. package/lib/module/core/services/TokenService.js.map +1 -1
  100. package/lib/module/crypto/index.js +0 -3
  101. package/lib/module/crypto/index.js.map +1 -1
  102. package/lib/module/crypto/keyManager.js +21 -22
  103. package/lib/module/crypto/keyManager.js.map +1 -1
  104. package/lib/module/crypto/polyfill.js +1 -2
  105. package/lib/module/crypto/polyfill.js.map +1 -1
  106. package/lib/module/crypto/signatureService.js +18 -32
  107. package/lib/module/crypto/signatureService.js.map +1 -1
  108. package/lib/module/index.js +7 -19
  109. package/lib/module/index.js.map +1 -1
  110. package/lib/module/models/interfaces.js +0 -7
  111. package/lib/module/models/interfaces.js.map +1 -1
  112. package/lib/module/node/index.js +0 -3
  113. package/lib/module/node/index.js.map +1 -1
  114. package/lib/module/ui/components/BottomSheetRouter.js +2 -6
  115. package/lib/module/ui/components/BottomSheetRouter.js.map +1 -1
  116. package/lib/module/ui/components/OxyProvider.js +9 -20
  117. package/lib/module/ui/components/OxyProvider.js.map +1 -1
  118. package/lib/module/ui/context/OxyContext.js +779 -450
  119. package/lib/module/ui/context/OxyContext.js.map +1 -1
  120. package/lib/module/ui/context/hooks/useAuthOperations.js +545 -0
  121. package/lib/module/ui/context/hooks/useAuthOperations.js.map +1 -0
  122. package/lib/module/ui/context/hooks/useDeviceManagement.js +68 -0
  123. package/lib/module/ui/context/hooks/useDeviceManagement.js.map +1 -0
  124. package/lib/module/ui/context/hooks/useStorage.js +74 -0
  125. package/lib/module/ui/context/hooks/useStorage.js.map +1 -0
  126. package/lib/module/ui/hooks/index.js +0 -1
  127. package/lib/module/ui/hooks/index.js.map +1 -1
  128. package/lib/module/ui/hooks/mutations/index.js +1 -1
  129. package/lib/module/ui/hooks/mutations/index.js.map +1 -1
  130. package/lib/module/ui/hooks/mutations/useAccountMutations.js +21 -71
  131. package/lib/module/ui/hooks/mutations/useAccountMutations.js.map +1 -1
  132. package/lib/module/ui/hooks/queries/index.js +1 -1
  133. package/lib/module/ui/hooks/queries/index.js.map +1 -1
  134. package/lib/module/ui/hooks/queries/queryKeys.js +1 -3
  135. package/lib/module/ui/hooks/queries/queryKeys.js.map +1 -1
  136. package/lib/module/ui/hooks/queries/useAccountQueries.js +27 -61
  137. package/lib/module/ui/hooks/queries/useAccountQueries.js.map +1 -1
  138. package/lib/module/ui/hooks/queries/useServicesQueries.js +6 -4
  139. package/lib/module/ui/hooks/queries/useServicesQueries.js.map +1 -1
  140. package/lib/module/ui/hooks/useDeviceManagement.js +68 -0
  141. package/lib/module/ui/hooks/useDeviceManagement.js.map +1 -0
  142. package/lib/module/ui/hooks/useProfileEditing.js +5 -3
  143. package/lib/module/ui/hooks/useProfileEditing.js.map +1 -1
  144. package/lib/module/ui/hooks/useSessionManagement.js +279 -0
  145. package/lib/module/ui/hooks/useSessionManagement.js.map +1 -0
  146. package/lib/module/ui/index.js +1 -2
  147. package/lib/module/ui/index.js.map +1 -1
  148. package/lib/module/ui/navigation/routes.js +1 -5
  149. package/lib/module/ui/navigation/routes.js.map +1 -1
  150. package/lib/module/ui/screens/AccountCenterScreen.js +4 -9
  151. package/lib/module/ui/screens/AccountCenterScreen.js.map +1 -1
  152. package/lib/module/ui/screens/AccountSettingsScreen.js +19 -37
  153. package/lib/module/ui/screens/AccountSettingsScreen.js.map +1 -1
  154. package/lib/module/ui/screens/AccountSwitcherScreen.js +5 -5
  155. package/lib/module/ui/screens/AccountSwitcherScreen.js.map +1 -1
  156. package/lib/module/ui/screens/OxyAuthScreen.js +15 -2
  157. package/lib/module/ui/screens/OxyAuthScreen.js.map +1 -1
  158. package/lib/module/ui/screens/PrivacySettingsScreen.js +98 -77
  159. package/lib/module/ui/screens/PrivacySettingsScreen.js.map +1 -1
  160. package/lib/module/ui/screens/ProfileScreen.js +6 -6
  161. package/lib/module/ui/screens/ProfileScreen.js.map +1 -1
  162. package/lib/module/ui/stores/authStore.js +6 -54
  163. package/lib/module/ui/stores/authStore.js.map +1 -1
  164. package/lib/module/ui/styles/spacing.js +2 -6
  165. package/lib/module/ui/styles/spacing.js.map +1 -1
  166. package/lib/module/ui/utils/avatarUtils.js +12 -9
  167. package/lib/module/ui/utils/avatarUtils.js.map +1 -1
  168. package/lib/module/ui/utils/sessionHelpers.js +1 -7
  169. package/lib/module/ui/utils/sessionHelpers.js.map +1 -1
  170. package/lib/module/utils/deviceManager.js +171 -0
  171. package/lib/module/utils/deviceManager.js.map +1 -0
  172. package/lib/module/utils/errorUtils.js +0 -7
  173. package/lib/module/utils/errorUtils.js.map +1 -1
  174. package/lib/module/utils/index.js +1 -2
  175. package/lib/module/utils/index.js.map +1 -1
  176. package/lib/module/utils/sessionUtils.js +1 -8
  177. package/lib/module/utils/sessionUtils.js.map +1 -1
  178. package/lib/module/utils/validationUtils.js +0 -13
  179. package/lib/module/utils/validationUtils.js.map +1 -1
  180. package/lib/typescript/core/OxyServices.d.ts +1 -1
  181. package/lib/typescript/core/index.d.ts +3 -3
  182. package/lib/typescript/core/index.d.ts.map +1 -1
  183. package/lib/typescript/core/mixins/OxyServices.auth.d.ts +21 -44
  184. package/lib/typescript/core/mixins/OxyServices.auth.d.ts.map +1 -1
  185. package/lib/typescript/core/mixins/OxyServices.user.d.ts +1 -0
  186. package/lib/typescript/core/mixins/OxyServices.user.d.ts.map +1 -1
  187. package/lib/typescript/core/mixins/index.d.ts +8 -15
  188. package/lib/typescript/core/mixins/index.d.ts.map +1 -1
  189. package/lib/typescript/core/services/TokenService.d.ts.map +1 -1
  190. package/lib/typescript/crypto/index.d.ts +0 -1
  191. package/lib/typescript/crypto/index.d.ts.map +1 -1
  192. package/lib/typescript/crypto/keyManager.d.ts +2 -15
  193. package/lib/typescript/crypto/keyManager.d.ts.map +1 -1
  194. package/lib/typescript/crypto/polyfill.d.ts +1 -2
  195. package/lib/typescript/crypto/polyfill.d.ts.map +1 -1
  196. package/lib/typescript/crypto/signatureService.d.ts +0 -13
  197. package/lib/typescript/crypto/signatureService.d.ts.map +1 -1
  198. package/lib/typescript/index.d.ts +7 -12
  199. package/lib/typescript/index.d.ts.map +1 -1
  200. package/lib/typescript/models/interfaces.d.ts +36 -5
  201. package/lib/typescript/models/interfaces.d.ts.map +1 -1
  202. package/lib/typescript/models/session.d.ts +18 -3
  203. package/lib/typescript/models/session.d.ts.map +1 -1
  204. package/lib/typescript/node/index.d.ts +0 -1
  205. package/lib/typescript/node/index.d.ts.map +1 -1
  206. package/lib/typescript/ui/components/BottomSheetRouter.d.ts +0 -5
  207. package/lib/typescript/ui/components/BottomSheetRouter.d.ts.map +1 -1
  208. package/lib/typescript/ui/components/OxyProvider.d.ts.map +1 -1
  209. package/lib/typescript/ui/components/TextField/Addons/Outline.d.ts +2 -2
  210. package/lib/typescript/ui/components/TextField/helpers.d.ts +2 -2
  211. package/lib/typescript/ui/components/TextField/types.d.ts +0 -1
  212. package/lib/typescript/ui/components/TextField/types.d.ts.map +1 -1
  213. package/lib/typescript/ui/context/OxyContext.d.ts +28 -5
  214. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  215. package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts +59 -0
  216. package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts.map +1 -0
  217. package/lib/typescript/ui/context/hooks/useDeviceManagement.d.ts +27 -0
  218. package/lib/typescript/ui/context/hooks/useDeviceManagement.d.ts.map +1 -0
  219. package/lib/typescript/ui/context/hooks/useStorage.d.ts +22 -0
  220. package/lib/typescript/ui/context/hooks/useStorage.d.ts.map +1 -0
  221. package/lib/typescript/ui/hooks/index.d.ts +0 -1
  222. package/lib/typescript/ui/hooks/index.d.ts.map +1 -1
  223. package/lib/typescript/ui/hooks/mutations/index.d.ts +1 -1
  224. package/lib/typescript/ui/hooks/mutations/index.d.ts.map +1 -1
  225. package/lib/typescript/ui/hooks/mutations/useAccountMutations.d.ts +8 -19
  226. package/lib/typescript/ui/hooks/mutations/useAccountMutations.d.ts.map +1 -1
  227. package/lib/typescript/ui/hooks/mutations/useServicesMutations.d.ts +1 -1
  228. package/lib/typescript/ui/hooks/mutations/useServicesMutations.d.ts.map +1 -1
  229. package/lib/typescript/ui/hooks/queries/index.d.ts +1 -1
  230. package/lib/typescript/ui/hooks/queries/index.d.ts.map +1 -1
  231. package/lib/typescript/ui/hooks/queries/queryKeys.d.ts +0 -2
  232. package/lib/typescript/ui/hooks/queries/queryKeys.d.ts.map +1 -1
  233. package/lib/typescript/ui/hooks/queries/useAccountQueries.d.ts +5 -17
  234. package/lib/typescript/ui/hooks/queries/useAccountQueries.d.ts.map +1 -1
  235. package/lib/typescript/ui/hooks/queries/useServicesQueries.d.ts.map +1 -1
  236. package/lib/typescript/ui/hooks/useDeviceManagement.d.ts +27 -0
  237. package/lib/typescript/ui/hooks/useDeviceManagement.d.ts.map +1 -0
  238. package/lib/typescript/ui/hooks/useProfileEditing.d.ts.map +1 -1
  239. package/lib/typescript/ui/hooks/useSessionManagement.d.ts +41 -0
  240. package/lib/typescript/ui/hooks/useSessionManagement.d.ts.map +1 -0
  241. package/lib/typescript/ui/index.d.ts +0 -1
  242. package/lib/typescript/ui/index.d.ts.map +1 -1
  243. package/lib/typescript/ui/navigation/routes.d.ts +1 -1
  244. package/lib/typescript/ui/navigation/routes.d.ts.map +1 -1
  245. package/lib/typescript/ui/screens/AccountCenterScreen.d.ts.map +1 -1
  246. package/lib/typescript/ui/screens/AccountSettingsScreen.d.ts.map +1 -1
  247. package/lib/typescript/ui/screens/OxyAuthScreen.d.ts.map +1 -1
  248. package/lib/typescript/ui/screens/PrivacySettingsScreen.d.ts.map +1 -1
  249. package/lib/typescript/ui/stores/authStore.d.ts +1 -8
  250. package/lib/typescript/ui/stores/authStore.d.ts.map +1 -1
  251. package/lib/typescript/ui/styles/spacing.d.ts +0 -5
  252. package/lib/typescript/ui/styles/spacing.d.ts.map +1 -1
  253. package/lib/typescript/ui/utils/avatarUtils.d.ts +4 -1
  254. package/lib/typescript/ui/utils/avatarUtils.d.ts.map +1 -1
  255. package/lib/typescript/ui/utils/sessionHelpers.d.ts +0 -1
  256. package/lib/typescript/ui/utils/sessionHelpers.d.ts.map +1 -1
  257. package/lib/typescript/utils/deviceManager.d.ts +66 -0
  258. package/lib/typescript/utils/deviceManager.d.ts.map +1 -0
  259. package/lib/typescript/utils/errorUtils.d.ts +0 -6
  260. package/lib/typescript/utils/errorUtils.d.ts.map +1 -1
  261. package/lib/typescript/utils/index.d.ts +2 -0
  262. package/lib/typescript/utils/index.d.ts.map +1 -1
  263. package/lib/typescript/utils/sessionUtils.d.ts.map +1 -1
  264. package/lib/typescript/utils/validationUtils.d.ts +0 -8
  265. package/lib/typescript/utils/validationUtils.d.ts.map +1 -1
  266. package/package.json +1 -6
  267. package/src/core/OxyServices.ts +1 -1
  268. package/src/core/index.ts +5 -8
  269. package/src/core/mixins/OxyServices.auth.ts +44 -87
  270. package/src/core/mixins/OxyServices.user.ts +10 -18
  271. package/src/core/services/TokenService.ts +27 -16
  272. package/src/crypto/index.ts +1 -3
  273. package/src/crypto/keyManager.ts +21 -30
  274. package/src/crypto/polyfill.ts +1 -2
  275. package/src/crypto/signatureService.ts +19 -43
  276. package/src/index.ts +6 -41
  277. package/src/models/interfaces.ts +39 -12
  278. package/src/models/session.ts +19 -7
  279. package/src/node/index.ts +0 -3
  280. package/src/ui/components/BottomSheetRouter.tsx +1 -6
  281. package/src/ui/components/OxyProvider.tsx +10 -17
  282. package/src/ui/components/TextField/Addons/Outline.tsx +2 -2
  283. package/src/ui/components/TextField/helpers.tsx +2 -2
  284. package/src/ui/components/TextField/types.tsx +1 -1
  285. package/src/ui/context/OxyContext.tsx +831 -463
  286. package/src/ui/context/hooks/useAuthOperations.ts +620 -0
  287. package/src/ui/context/hooks/useDeviceManagement.ts +108 -0
  288. package/src/ui/context/hooks/useStorage.ts +104 -0
  289. package/src/ui/hooks/index.ts +1 -2
  290. package/src/ui/hooks/mutations/index.ts +0 -2
  291. package/src/ui/hooks/mutations/useAccountMutations.ts +20 -66
  292. package/src/ui/hooks/queries/index.ts +0 -2
  293. package/src/ui/hooks/queries/queryKeys.ts +0 -2
  294. package/src/ui/hooks/queries/useAccountQueries.ts +19 -53
  295. package/src/ui/hooks/queries/useServicesQueries.ts +5 -5
  296. package/src/ui/hooks/useDeviceManagement.ts +108 -0
  297. package/src/ui/hooks/useProfileEditing.ts +3 -3
  298. package/src/ui/hooks/useSessionManagement.ts +405 -0
  299. package/src/ui/index.ts +1 -2
  300. package/src/ui/navigation/routes.ts +2 -6
  301. package/src/ui/screens/AccountCenterScreen.tsx +4 -9
  302. package/src/ui/screens/AccountSettingsScreen.tsx +31 -49
  303. package/src/ui/screens/AccountSwitcherScreen.tsx +5 -5
  304. package/src/ui/screens/OxyAuthScreen.tsx +19 -4
  305. package/src/ui/screens/PrivacySettingsScreen.tsx +101 -67
  306. package/src/ui/screens/ProfileScreen.tsx +10 -10
  307. package/src/ui/stores/authStore.ts +8 -48
  308. package/src/ui/styles/spacing.ts +2 -15
  309. package/src/ui/utils/avatarUtils.ts +21 -19
  310. package/src/ui/utils/sessionHelpers.ts +0 -7
  311. package/src/utils/__tests__/validationUtils.test.ts +1 -16
  312. package/src/utils/deviceManager.ts +198 -0
  313. package/src/utils/errorUtils.ts +1 -8
  314. package/src/utils/index.ts +2 -1
  315. package/src/utils/sessionUtils.ts +0 -8
  316. package/src/utils/validationUtils.ts +0 -12
  317. package/lib/commonjs/adapters/expo/crypto.js +0 -56
  318. package/lib/commonjs/adapters/expo/crypto.js.map +0 -1
  319. package/lib/commonjs/adapters/expo/fetch.js +0 -30
  320. package/lib/commonjs/adapters/expo/fetch.js.map +0 -1
  321. package/lib/commonjs/adapters/expo/index.js +0 -48
  322. package/lib/commonjs/adapters/expo/index.js.map +0 -1
  323. package/lib/commonjs/adapters/expo/storage.js +0 -201
  324. package/lib/commonjs/adapters/expo/storage.js.map +0 -1
  325. package/lib/commonjs/adapters/index.js +0 -48
  326. package/lib/commonjs/adapters/index.js.map +0 -1
  327. package/lib/commonjs/adapters/node/crypto.js +0 -40
  328. package/lib/commonjs/adapters/node/crypto.js.map +0 -1
  329. package/lib/commonjs/adapters/node/fetch.js +0 -62
  330. package/lib/commonjs/adapters/node/fetch.js.map +0 -1
  331. package/lib/commonjs/adapters/node/index.js +0 -34
  332. package/lib/commonjs/adapters/node/index.js.map +0 -1
  333. package/lib/commonjs/adapters/node/storage.js +0 -163
  334. package/lib/commonjs/adapters/node/storage.js.map +0 -1
  335. package/lib/commonjs/core/identity-session/DeviceManager.js +0 -237
  336. package/lib/commonjs/core/identity-session/DeviceManager.js.map +0 -1
  337. package/lib/commonjs/core/identity-session/INTEGRATION_GUIDE.md +0 -287
  338. package/lib/commonjs/core/identity-session/IdentityManager.js +0 -400
  339. package/lib/commonjs/core/identity-session/IdentityManager.js.map +0 -1
  340. package/lib/commonjs/core/identity-session/IdentitySessionCore.js +0 -394
  341. package/lib/commonjs/core/identity-session/IdentitySessionCore.js.map +0 -1
  342. package/lib/commonjs/core/identity-session/RefreshManager.js +0 -137
  343. package/lib/commonjs/core/identity-session/RefreshManager.js.map +0 -1
  344. package/lib/commonjs/core/identity-session/SessionManager.js +0 -427
  345. package/lib/commonjs/core/identity-session/SessionManager.js.map +0 -1
  346. package/lib/commonjs/core/identity-session/createIdentitySessionCore.js +0 -24
  347. package/lib/commonjs/core/identity-session/createIdentitySessionCore.js.map +0 -1
  348. package/lib/commonjs/core/identity-session/errors.js +0 -176
  349. package/lib/commonjs/core/identity-session/errors.js.map +0 -1
  350. package/lib/commonjs/core/identity-session/index.js +0 -80
  351. package/lib/commonjs/core/identity-session/index.js.map +0 -1
  352. package/lib/commonjs/core/identity-session/types.js +0 -2
  353. package/lib/commonjs/core/identity-session/types.js.map +0 -1
  354. package/lib/commonjs/crypto/README.md +0 -142
  355. package/lib/commonjs/crypto/core.js +0 -147
  356. package/lib/commonjs/crypto/core.js.map +0 -1
  357. package/lib/commonjs/node/signatureService.js +0 -107
  358. package/lib/commonjs/node/signatureService.js.map +0 -1
  359. package/lib/commonjs/ui/hooks/auth/index.js +0 -37
  360. package/lib/commonjs/ui/hooks/auth/index.js.map +0 -1
  361. package/lib/commonjs/ui/hooks/auth/useUsernameValidation.js +0 -171
  362. package/lib/commonjs/ui/hooks/auth/useUsernameValidation.js.map +0 -1
  363. package/lib/commonjs/ui/hooks/useAvatarPicker.js +0 -52
  364. package/lib/commonjs/ui/hooks/useAvatarPicker.js.map +0 -1
  365. package/lib/commonjs/ui/hooks/useIdentityTransfer.js +0 -125
  366. package/lib/commonjs/ui/hooks/useIdentityTransfer.js.map +0 -1
  367. package/lib/commonjs/ui/hooks/useTransferCodesPersistence.js +0 -81
  368. package/lib/commonjs/ui/hooks/useTransferCodesPersistence.js.map +0 -1
  369. package/lib/commonjs/ui/hooks/useTransferQueries.js +0 -85
  370. package/lib/commonjs/ui/hooks/useTransferQueries.js.map +0 -1
  371. package/lib/commonjs/ui/stores/transferStore.js +0 -157
  372. package/lib/commonjs/ui/stores/transferStore.js.map +0 -1
  373. package/lib/module/adapters/expo/crypto.js +0 -51
  374. package/lib/module/adapters/expo/crypto.js.map +0 -1
  375. package/lib/module/adapters/expo/fetch.js +0 -26
  376. package/lib/module/adapters/expo/fetch.js.map +0 -1
  377. package/lib/module/adapters/expo/index.js +0 -45
  378. package/lib/module/adapters/expo/index.js.map +0 -1
  379. package/lib/module/adapters/expo/storage.js +0 -198
  380. package/lib/module/adapters/expo/storage.js.map +0 -1
  381. package/lib/module/adapters/index.js +0 -45
  382. package/lib/module/adapters/index.js.map +0 -1
  383. package/lib/module/adapters/node/crypto.js +0 -36
  384. package/lib/module/adapters/node/crypto.js.map +0 -1
  385. package/lib/module/adapters/node/fetch.js +0 -57
  386. package/lib/module/adapters/node/fetch.js.map +0 -1
  387. package/lib/module/adapters/node/index.js +0 -31
  388. package/lib/module/adapters/node/index.js.map +0 -1
  389. package/lib/module/adapters/node/storage.js +0 -159
  390. package/lib/module/adapters/node/storage.js.map +0 -1
  391. package/lib/module/core/identity-session/DeviceManager.js +0 -232
  392. package/lib/module/core/identity-session/DeviceManager.js.map +0 -1
  393. package/lib/module/core/identity-session/INTEGRATION_GUIDE.md +0 -287
  394. package/lib/module/core/identity-session/IdentityManager.js +0 -395
  395. package/lib/module/core/identity-session/IdentityManager.js.map +0 -1
  396. package/lib/module/core/identity-session/IdentitySessionCore.js +0 -390
  397. package/lib/module/core/identity-session/IdentitySessionCore.js.map +0 -1
  398. package/lib/module/core/identity-session/RefreshManager.js +0 -132
  399. package/lib/module/core/identity-session/RefreshManager.js.map +0 -1
  400. package/lib/module/core/identity-session/SessionManager.js +0 -422
  401. package/lib/module/core/identity-session/SessionManager.js.map +0 -1
  402. package/lib/module/core/identity-session/createIdentitySessionCore.js +0 -21
  403. package/lib/module/core/identity-session/createIdentitySessionCore.js.map +0 -1
  404. package/lib/module/core/identity-session/errors.js +0 -170
  405. package/lib/module/core/identity-session/errors.js.map +0 -1
  406. package/lib/module/core/identity-session/index.js +0 -17
  407. package/lib/module/core/identity-session/index.js.map +0 -1
  408. package/lib/module/core/identity-session/types.js +0 -2
  409. package/lib/module/core/identity-session/types.js.map +0 -1
  410. package/lib/module/crypto/README.md +0 -142
  411. package/lib/module/crypto/core.js +0 -133
  412. package/lib/module/crypto/core.js.map +0 -1
  413. package/lib/module/node/signatureService.js +0 -101
  414. package/lib/module/node/signatureService.js.map +0 -1
  415. package/lib/module/ui/hooks/auth/index.js +0 -7
  416. package/lib/module/ui/hooks/auth/index.js.map +0 -1
  417. package/lib/module/ui/hooks/auth/useUsernameValidation.js +0 -167
  418. package/lib/module/ui/hooks/auth/useUsernameValidation.js.map +0 -1
  419. package/lib/module/ui/hooks/useAvatarPicker.js +0 -48
  420. package/lib/module/ui/hooks/useAvatarPicker.js.map +0 -1
  421. package/lib/module/ui/hooks/useIdentityTransfer.js +0 -121
  422. package/lib/module/ui/hooks/useIdentityTransfer.js.map +0 -1
  423. package/lib/module/ui/hooks/useTransferCodesPersistence.js +0 -77
  424. package/lib/module/ui/hooks/useTransferCodesPersistence.js.map +0 -1
  425. package/lib/module/ui/hooks/useTransferQueries.js +0 -80
  426. package/lib/module/ui/hooks/useTransferQueries.js.map +0 -1
  427. package/lib/module/ui/stores/transferStore.js +0 -152
  428. package/lib/module/ui/stores/transferStore.js.map +0 -1
  429. package/lib/typescript/adapters/expo/crypto.d.ts +0 -17
  430. package/lib/typescript/adapters/expo/crypto.d.ts.map +0 -1
  431. package/lib/typescript/adapters/expo/fetch.d.ts +0 -16
  432. package/lib/typescript/adapters/expo/fetch.d.ts.map +0 -1
  433. package/lib/typescript/adapters/expo/index.d.ts +0 -23
  434. package/lib/typescript/adapters/expo/index.d.ts.map +0 -1
  435. package/lib/typescript/adapters/expo/storage.d.ts +0 -23
  436. package/lib/typescript/adapters/expo/storage.d.ts.map +0 -1
  437. package/lib/typescript/adapters/index.d.ts +0 -17
  438. package/lib/typescript/adapters/index.d.ts.map +0 -1
  439. package/lib/typescript/adapters/node/crypto.d.ts +0 -17
  440. package/lib/typescript/adapters/node/crypto.d.ts.map +0 -1
  441. package/lib/typescript/adapters/node/fetch.d.ts +0 -16
  442. package/lib/typescript/adapters/node/fetch.d.ts.map +0 -1
  443. package/lib/typescript/adapters/node/index.d.ts +0 -23
  444. package/lib/typescript/adapters/node/index.d.ts.map +0 -1
  445. package/lib/typescript/adapters/node/storage.d.ts +0 -23
  446. package/lib/typescript/adapters/node/storage.d.ts.map +0 -1
  447. package/lib/typescript/core/identity-session/DeviceManager.d.ts +0 -64
  448. package/lib/typescript/core/identity-session/DeviceManager.d.ts.map +0 -1
  449. package/lib/typescript/core/identity-session/IdentityManager.d.ts +0 -88
  450. package/lib/typescript/core/identity-session/IdentityManager.d.ts.map +0 -1
  451. package/lib/typescript/core/identity-session/IdentitySessionCore.d.ts +0 -141
  452. package/lib/typescript/core/identity-session/IdentitySessionCore.d.ts.map +0 -1
  453. package/lib/typescript/core/identity-session/RefreshManager.d.ts +0 -36
  454. package/lib/typescript/core/identity-session/RefreshManager.d.ts.map +0 -1
  455. package/lib/typescript/core/identity-session/SessionManager.d.ts +0 -104
  456. package/lib/typescript/core/identity-session/SessionManager.d.ts.map +0 -1
  457. package/lib/typescript/core/identity-session/createIdentitySessionCore.d.ts +0 -11
  458. package/lib/typescript/core/identity-session/createIdentitySessionCore.d.ts.map +0 -1
  459. package/lib/typescript/core/identity-session/errors.d.ts +0 -63
  460. package/lib/typescript/core/identity-session/errors.d.ts.map +0 -1
  461. package/lib/typescript/core/identity-session/index.d.ts +0 -14
  462. package/lib/typescript/core/identity-session/index.d.ts.map +0 -1
  463. package/lib/typescript/core/identity-session/types.d.ts +0 -196
  464. package/lib/typescript/core/identity-session/types.d.ts.map +0 -1
  465. package/lib/typescript/crypto/core.d.ts +0 -56
  466. package/lib/typescript/crypto/core.d.ts.map +0 -1
  467. package/lib/typescript/node/signatureService.d.ts +0 -55
  468. package/lib/typescript/node/signatureService.d.ts.map +0 -1
  469. package/lib/typescript/ui/hooks/auth/index.d.ts +0 -6
  470. package/lib/typescript/ui/hooks/auth/index.d.ts.map +0 -1
  471. package/lib/typescript/ui/hooks/auth/useUsernameValidation.d.ts +0 -32
  472. package/lib/typescript/ui/hooks/auth/useUsernameValidation.d.ts.map +0 -1
  473. package/lib/typescript/ui/hooks/useAvatarPicker.d.ts +0 -18
  474. package/lib/typescript/ui/hooks/useAvatarPicker.d.ts.map +0 -1
  475. package/lib/typescript/ui/hooks/useIdentityTransfer.d.ts +0 -24
  476. package/lib/typescript/ui/hooks/useIdentityTransfer.d.ts.map +0 -1
  477. package/lib/typescript/ui/hooks/useTransferCodesPersistence.d.ts +0 -6
  478. package/lib/typescript/ui/hooks/useTransferCodesPersistence.d.ts.map +0 -1
  479. package/lib/typescript/ui/hooks/useTransferQueries.d.ts +0 -26
  480. package/lib/typescript/ui/hooks/useTransferQueries.d.ts.map +0 -1
  481. package/lib/typescript/ui/stores/transferStore.d.ts +0 -36
  482. package/lib/typescript/ui/stores/transferStore.d.ts.map +0 -1
  483. package/src/adapters/expo/crypto.ts +0 -55
  484. package/src/adapters/expo/fetch.ts +0 -28
  485. package/src/adapters/expo/index.ts +0 -51
  486. package/src/adapters/expo/storage.ts +0 -228
  487. package/src/adapters/index.ts +0 -48
  488. package/src/adapters/node/crypto.ts +0 -39
  489. package/src/adapters/node/fetch.ts +0 -59
  490. package/src/adapters/node/index.ts +0 -37
  491. package/src/adapters/node/storage.ts +0 -170
  492. package/src/core/identity-session/DeviceManager.ts +0 -273
  493. package/src/core/identity-session/INTEGRATION_GUIDE.md +0 -287
  494. package/src/core/identity-session/IdentityManager.ts +0 -474
  495. package/src/core/identity-session/IdentitySessionCore.ts +0 -464
  496. package/src/core/identity-session/RefreshManager.ts +0 -189
  497. package/src/core/identity-session/SessionManager.ts +0 -500
  498. package/src/core/identity-session/createIdentitySessionCore.ts +0 -19
  499. package/src/core/identity-session/errors.ts +0 -197
  500. package/src/core/identity-session/index.ts +0 -15
  501. package/src/core/identity-session/types.ts +0 -188
  502. package/src/crypto/README.md +0 -142
  503. package/src/crypto/__tests__/core.test.ts +0 -203
  504. package/src/crypto/core.ts +0 -142
  505. package/src/node/signatureService.ts +0 -126
  506. package/src/ui/hooks/auth/index.ts +0 -9
  507. package/src/ui/hooks/auth/useUsernameValidation.ts +0 -177
  508. package/src/ui/hooks/useAvatarPicker.ts +0 -62
  509. package/src/ui/hooks/useIdentityTransfer.ts +0 -135
  510. package/src/ui/hooks/useTransferCodesPersistence.ts +0 -80
  511. package/src/ui/hooks/useTransferQueries.ts +0 -102
  512. package/src/ui/stores/transferStore.ts +0 -201
@@ -0,0 +1,620 @@
1
+ import { useCallback } from 'react';
2
+ import type { ApiError, User } from '../../../models/interfaces';
3
+ import type { AuthState } from '../../stores/authStore';
4
+ import type { ClientSession, SessionLoginResponse } from '../../../models/session';
5
+ import { DeviceManager } from '../../../utils/deviceManager';
6
+ import { fetchSessionsWithFallback, mapSessionsToClient } from '../../utils/sessionHelpers';
7
+ import { handleAuthError, isInvalidSessionError } from '../../utils/errorHandlers';
8
+ import type { StorageInterface } from '../../utils/storageHelpers';
9
+ import type { OxyServices } from '../../../core';
10
+ import { KeyManager, SignatureService, type BackupData } from '../../../crypto';
11
+
12
+ export interface UseAuthOperationsOptions {
13
+ oxyServices: OxyServices;
14
+ storage: StorageInterface | null;
15
+ sessions: ClientSession[];
16
+ activeSessionId: string | null;
17
+ setActiveSessionId: (sessionId: string | null) => void;
18
+ updateSessions: (sessions: ClientSession[], options?: { merge?: boolean }) => void;
19
+ saveActiveSessionId: (sessionId: string) => Promise<void>;
20
+ clearSessionState: () => Promise<void>;
21
+ switchSession: (sessionId: string) => Promise<User>;
22
+ applyLanguagePreference: (user: User) => Promise<void>;
23
+ onAuthStateChange?: (user: User | null) => void;
24
+ onError?: (error: ApiError) => void;
25
+ loginSuccess: () => void;
26
+ loginFailure: (message: string) => void;
27
+ logoutStore: () => void;
28
+ setAuthState: (state: Partial<AuthState>) => void;
29
+ // Identity sync store actions
30
+ setIdentitySynced: (synced: boolean) => void;
31
+ setSyncing: (syncing: boolean) => void;
32
+ logger?: (message: string, error?: unknown) => void;
33
+ }
34
+
35
+ export interface UseAuthOperationsResult {
36
+ /** Create a new identity locally (offline-first) and optionally sync with server */
37
+ createIdentity: () => Promise<{ synced: boolean }>;
38
+ /** Import an existing identity from backup file data */
39
+ importIdentity: (backupData: BackupData, password: string) => Promise<{ synced: boolean }>;
40
+ /** Sign in with existing identity on device */
41
+ signIn: (deviceName?: string) => Promise<User>;
42
+ /** Logout from current session */
43
+ logout: (targetSessionId?: string) => Promise<void>;
44
+ /** Logout from all sessions */
45
+ logoutAll: () => Promise<void>;
46
+ /** Check if device has an identity stored */
47
+ hasIdentity: () => Promise<boolean>;
48
+ /** Get the public key of the stored identity */
49
+ getPublicKey: () => Promise<string | null>;
50
+ /** Check if identity is synced with server */
51
+ isIdentitySynced: () => Promise<boolean>;
52
+ /** Sync local identity with server (when online) */
53
+ syncIdentity: () => Promise<User>;
54
+ }
55
+
56
+ const LOGIN_ERROR_CODE = 'LOGIN_ERROR';
57
+ const REGISTER_ERROR_CODE = 'REGISTER_ERROR';
58
+ const LOGOUT_ERROR_CODE = 'LOGOUT_ERROR';
59
+ const LOGOUT_ALL_ERROR_CODE = 'LOGOUT_ALL_ERROR';
60
+
61
+ /**
62
+ * Authentication operations using public key cryptography.
63
+ * No passwords required - identity is based on ECDSA key pairs.
64
+ */
65
+ export const useAuthOperations = ({
66
+ oxyServices,
67
+ storage,
68
+ sessions,
69
+ activeSessionId,
70
+ setActiveSessionId,
71
+ updateSessions,
72
+ saveActiveSessionId,
73
+ clearSessionState,
74
+ switchSession,
75
+ applyLanguagePreference,
76
+ onAuthStateChange,
77
+ onError,
78
+ loginSuccess,
79
+ loginFailure,
80
+ logoutStore,
81
+ setAuthState,
82
+ setIdentitySynced,
83
+ setSyncing,
84
+ logger,
85
+ }: UseAuthOperationsOptions): UseAuthOperationsResult => {
86
+
87
+ /**
88
+ * Internal function to perform challenge-response sign in (works offline)
89
+ */
90
+ const performSignIn = useCallback(
91
+ async (publicKey: string): Promise<User> => {
92
+ const deviceFingerprintObj = DeviceManager.getDeviceFingerprint();
93
+ const deviceFingerprint = JSON.stringify(deviceFingerprintObj);
94
+ const deviceInfo = await DeviceManager.getDeviceInfo();
95
+ const deviceName = deviceInfo.deviceName || DeviceManager.getDefaultDeviceName();
96
+ const USER_ID_STORAGE_KEY = 'oxy_user_id';
97
+
98
+ // Online-only sign-in: require backend availability
99
+ // First, resolve userId (prefer locally stored value)
100
+ let userId: string | null = null;
101
+ if (storage) {
102
+ userId = await storage.getItem(USER_ID_STORAGE_KEY);
103
+ }
104
+
105
+ if (!userId) {
106
+ const userLookup = await oxyServices.getUserByPublicKey(publicKey);
107
+ userId = userLookup.id;
108
+ if (storage && userId) {
109
+ await storage.setItem(USER_ID_STORAGE_KEY, userId).catch(() => {});
110
+ }
111
+ }
112
+
113
+ const challengeResponse = await oxyServices.requestChallenge(userId);
114
+ const challenge = challengeResponse.challenge;
115
+
116
+ // Note: Biometric authentication check should be handled by the app layer
117
+ // (e.g., accounts app) before calling signIn. The biometric preference is stored
118
+ // in local storage as 'oxy_biometric_enabled' and can be checked there.
119
+
120
+ // Sign the challenge
121
+ const { challenge: signature, timestamp } = await SignatureService.signChallenge(challenge);
122
+
123
+ // Online sign-in: use normal flow
124
+ if (!userId) {
125
+ throw new Error('User ID not found');
126
+ }
127
+
128
+ // Verify and create session using userId
129
+ const sessionResponse = await oxyServices.verifyChallenge(
130
+ userId,
131
+ challenge,
132
+ signature,
133
+ timestamp,
134
+ deviceName,
135
+ deviceFingerprint,
136
+ );
137
+
138
+ // Store tokens immediately (no extra round-trip)
139
+ oxyServices.setTokens(sessionResponse.accessToken, sessionResponse.refreshToken);
140
+
141
+ // Get full user data
142
+ const fullUser = await oxyServices.getUserBySession(sessionResponse.sessionId);
143
+
144
+ // IMPORTANT: user.id should be MongoDB ObjectId, not publicKey
145
+ // The API should return the correct id (ObjectId) from the database
146
+ // If it doesn't, we need to fix the API, not work around it here
147
+ // Validate that id is ObjectId format (24 hex characters)
148
+ if (fullUser.id && !/^[0-9a-fA-F]{24}$/.test(fullUser.id)) {
149
+ console.warn('[useAuthOperations] User.id is not MongoDB ObjectId format:', {
150
+ id: fullUser.id.substring(0, 20),
151
+ publicKey: fullUser.publicKey.substring(0, 20),
152
+ message: 'API should return MongoDB ObjectId as user.id, not publicKey'
153
+ });
154
+ // Don't override - let the API fix this issue
155
+ }
156
+
157
+ // Fetch device sessions
158
+ let allDeviceSessions: ClientSession[] = [];
159
+ try {
160
+ allDeviceSessions = await fetchSessionsWithFallback(oxyServices, sessionResponse.sessionId, {
161
+ fallbackDeviceId: sessionResponse.deviceId,
162
+ fallbackUserId: fullUser.id,
163
+ logger,
164
+ });
165
+ } catch (error) {
166
+ if (__DEV__) {
167
+ console.warn('Failed to fetch device sessions after login:', error);
168
+ }
169
+ }
170
+
171
+ // Check for existing session for same user
172
+ const existingSession = allDeviceSessions.find(
173
+ (session) =>
174
+ session.userId?.toString() === fullUser.id?.toString() &&
175
+ session.sessionId !== sessionResponse.sessionId,
176
+ );
177
+
178
+ if (existingSession) {
179
+ // Logout duplicate session
180
+ try {
181
+ await oxyServices.logoutSession(sessionResponse.sessionId, sessionResponse.sessionId);
182
+ } catch (logoutError) {
183
+ if (__DEV__) {
184
+ console.warn('Failed to logout duplicate session:', logoutError);
185
+ }
186
+ }
187
+ await switchSession(existingSession.sessionId);
188
+ updateSessions(
189
+ allDeviceSessions.filter((session) => session.sessionId !== sessionResponse.sessionId),
190
+ { merge: false },
191
+ );
192
+ onAuthStateChange?.(fullUser);
193
+ return fullUser;
194
+ }
195
+
196
+ setActiveSessionId(sessionResponse.sessionId);
197
+ await saveActiveSessionId(sessionResponse.sessionId);
198
+ updateSessions(allDeviceSessions, { merge: true });
199
+
200
+ await applyLanguagePreference(fullUser);
201
+ loginSuccess(); // Services never caches profile - only tokens
202
+ onAuthStateChange?.(fullUser);
203
+ if (storage) {
204
+ await storage.setItem('oxy_identity_synced', 'true').catch(() => {});
205
+ }
206
+ setIdentitySynced(true);
207
+
208
+ return fullUser;
209
+ },
210
+ [
211
+ applyLanguagePreference,
212
+ logger,
213
+ loginSuccess,
214
+ onAuthStateChange,
215
+ oxyServices,
216
+ saveActiveSessionId,
217
+ setActiveSessionId,
218
+ setIdentitySynced,
219
+ switchSession,
220
+ updateSessions,
221
+ storage,
222
+ ],
223
+ );
224
+
225
+ /**
226
+ * Create a new identity (offline-first)
227
+ * Identity is purely cryptographic - no username or email required
228
+ */
229
+ const createIdentity = useCallback(
230
+ async (): Promise<{ synced: boolean }> => {
231
+ if (!storage) throw new Error('Storage not initialized');
232
+
233
+ setAuthState({ isLoading: true, error: null });
234
+
235
+ try {
236
+ // Generate new key pair directly (works offline)
237
+ const { publicKey, privateKey } = await KeyManager.generateKeyPair();
238
+ await KeyManager.importKeyPair(privateKey);
239
+
240
+ // Mark as not synced
241
+ // Note: createIdentity only creates the key locally (offline-first)
242
+ // Registration with server (registerIdentity) must be done by the app (Accounts) with profile data
243
+ await storage.setItem('oxy_identity_synced', 'false');
244
+ setIdentitySynced(false);
245
+
246
+ return {
247
+ synced: false, // Always false - registration must be done separately by Accounts app
248
+ };
249
+ } catch (error) {
250
+ // CRITICAL: Never delete identity on error - it may have been successfully created
251
+ // Only log the error and let the user recover using their backup file
252
+ // Identity deletion should ONLY happen when explicitly requested by the user
253
+ if (__DEV__ && logger) {
254
+ logger('Error during identity creation (identity may still exist):', error);
255
+ }
256
+
257
+ // Check if identity was actually created (keys exist)
258
+ const hasIdentity = await KeyManager.hasIdentity().catch(() => false);
259
+ if (hasIdentity) {
260
+ // Identity exists - don't delete it! Just mark as not synced
261
+ await storage.setItem('oxy_identity_synced', 'false').catch(() => {});
262
+ setIdentitySynced(false);
263
+ if (__DEV__ && logger) {
264
+ logger('Identity was created but sync failed - user can sync later using backup file');
265
+ }
266
+ } else {
267
+ // No identity exists - this was a generation failure, safe to clean up sync flag
268
+ await storage.removeItem('oxy_identity_synced').catch(() => {});
269
+ setIdentitySynced(false);
270
+ }
271
+
272
+ const message = handleAuthError(error, {
273
+ defaultMessage: 'Failed to create identity',
274
+ code: REGISTER_ERROR_CODE,
275
+ onError,
276
+ setAuthError: (msg: string) => setAuthState({ error: msg }),
277
+ logger,
278
+ });
279
+ loginFailure(message);
280
+ throw error;
281
+ } finally {
282
+ setAuthState({ isLoading: false });
283
+ }
284
+ },
285
+ [oxyServices, storage, setAuthState, loginFailure, onError, logger, setIdentitySynced],
286
+ );
287
+
288
+ /**
289
+ * Check if identity is synced with server (reads from storage for persistence)
290
+ */
291
+ const isIdentitySyncedFn = useCallback(async (): Promise<boolean> => {
292
+ if (!storage) return true;
293
+ const synced = await storage.getItem('oxy_identity_synced');
294
+ const isSynced = synced !== 'false';
295
+ setIdentitySynced(isSynced);
296
+ return isSynced;
297
+ }, [storage, setIdentitySynced]);
298
+
299
+ /**
300
+ * Sync local identity with server (call when online)
301
+ * TanStack Query handles offline mutations automatically
302
+ */
303
+ const syncIdentity = useCallback(
304
+ async (): Promise<User> => {
305
+ if (!storage) throw new Error('Storage not initialized');
306
+
307
+ setAuthState({ isLoading: true, error: null });
308
+ setSyncing(true);
309
+
310
+ try {
311
+ const publicKey = await KeyManager.getPublicKey();
312
+ if (!publicKey) {
313
+ throw new Error('No identity found on this device');
314
+ }
315
+
316
+ // Check if already synced
317
+ const alreadySynced = await storage.getItem('oxy_identity_synced');
318
+ if (alreadySynced === 'true') {
319
+ setIdentitySynced(true);
320
+ return await performSignIn(publicKey);
321
+ }
322
+
323
+ // Check if already registered on server
324
+ const { registered } = await oxyServices.checkPublicKeyRegistered(publicKey);
325
+
326
+ if (!registered) {
327
+ // Identity is not registered - registration must be done by Accounts app with profile data
328
+ // syncIdentity only syncs already-registered identities
329
+ throw new Error('Identity is not registered. Please register your identity first using the Accounts app.');
330
+ }
331
+
332
+ // Mark as synced (Zustand store + storage)
333
+ await storage.setItem('oxy_identity_synced', 'true');
334
+ setIdentitySynced(true);
335
+
336
+ // Sign in (Services never caches profile - only tokens)
337
+ const user = await performSignIn(publicKey);
338
+
339
+ // Check if user has username - required for syncing
340
+ if (!user.username) {
341
+ const usernameError = new Error('USERNAME_REQUIRED');
342
+ (usernameError as any).code = 'USERNAME_REQUIRED';
343
+ throw usernameError;
344
+ }
345
+
346
+ // TanStack Query will automatically retry any pending mutations
347
+
348
+ return user;
349
+ } catch (error) {
350
+ const message = handleAuthError(error, {
351
+ defaultMessage: 'Failed to sync identity',
352
+ code: REGISTER_ERROR_CODE,
353
+ onError,
354
+ setAuthError: (msg: string) => setAuthState({ error: msg }),
355
+ logger,
356
+ });
357
+ loginFailure(message);
358
+ throw error;
359
+ } finally {
360
+ setAuthState({ isLoading: false });
361
+ setSyncing(false);
362
+ }
363
+ },
364
+ [oxyServices, storage, setAuthState, performSignIn, loginFailure, onError, logger, setSyncing, setIdentitySynced],
365
+ );
366
+
367
+ /**
368
+ * Import identity from backup file data (offline-first)
369
+ */
370
+ const importIdentity = useCallback(
371
+ async (backupData: BackupData, password: string): Promise<{ synced: boolean }> => {
372
+ if (!storage) throw new Error('Storage not initialized');
373
+
374
+ // Validate arguments - ensure backupData is an object, not a string (old signature)
375
+ if (!backupData || typeof backupData !== 'object' || Array.isArray(backupData)) {
376
+ throw new Error('Invalid backup data. Please use the backup file import feature.');
377
+ }
378
+
379
+ if (!backupData.encrypted || !backupData.salt || !backupData.iv || !backupData.publicKey) {
380
+ throw new Error('Invalid backup data structure. Missing required fields.');
381
+ }
382
+
383
+ if (!password || typeof password !== 'string') {
384
+ throw new Error('Password is required for backup file import.');
385
+ }
386
+
387
+ setAuthState({ isLoading: true, error: null });
388
+
389
+ try {
390
+ // Decrypt private key from backup data
391
+ const Crypto = await import('expo-crypto');
392
+
393
+ // Convert hex strings to Uint8Array
394
+ const saltBytes = new Uint8Array(
395
+ backupData.salt.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) || []
396
+ );
397
+ const ivBytes = new Uint8Array(
398
+ backupData.iv.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) || []
399
+ );
400
+
401
+ // Derive key from password (same algorithm as EncryptedBackupGenerator)
402
+ const saltHex = Array.from(saltBytes).map(b => b.toString(16).padStart(2, '0')).join('');
403
+ let key = password + saltHex;
404
+ for (let i = 0; i < 10000; i++) {
405
+ key = await Crypto.digestStringAsync(
406
+ Crypto.CryptoDigestAlgorithm.SHA256,
407
+ key
408
+ );
409
+ }
410
+ const keyBytes = new Uint8Array(32);
411
+ for (let i = 0; i < 64 && i < key.length; i += 2) {
412
+ keyBytes[i / 2] = parseInt(key.substring(i, i + 2), 16);
413
+ }
414
+
415
+ // Decrypt private key (XOR decryption - same as encryption)
416
+ const encryptedBytes = Buffer.from(backupData.encrypted, 'base64');
417
+ const decryptedBytes = new Uint8Array(encryptedBytes.length);
418
+ for (let i = 0; i < encryptedBytes.length; i++) {
419
+ decryptedBytes[i] = encryptedBytes[i] ^ keyBytes[i % keyBytes.length] ^ ivBytes[i % ivBytes.length];
420
+ }
421
+ const privateKey = new TextDecoder().decode(decryptedBytes);
422
+
423
+ // Import the key pair
424
+ const publicKey = await KeyManager.importKeyPair(privateKey);
425
+
426
+ // Verify public key matches
427
+ if (publicKey !== backupData.publicKey) {
428
+ throw new Error('Backup file is corrupted or password is incorrect');
429
+ }
430
+
431
+ // Mark as not synced
432
+ await storage.setItem('oxy_identity_synced', 'false');
433
+ setIdentitySynced(false);
434
+
435
+ // Try to sync with server
436
+ try {
437
+ // Check if this identity is already registered
438
+ const { registered } = await oxyServices.checkPublicKeyRegistered(publicKey);
439
+
440
+ if (registered) {
441
+ // Identity exists, mark as synced
442
+ await storage.setItem('oxy_identity_synced', 'true');
443
+ setIdentitySynced(true);
444
+ return { synced: true };
445
+ } else {
446
+ // Identity is not registered - registration must be done by Accounts app with profile data
447
+ // importIdentity only imports already-registered identities
448
+ await storage.setItem('oxy_identity_synced', 'false');
449
+ setIdentitySynced(false);
450
+ return { synced: false };
451
+ }
452
+ } catch (syncError) {
453
+ // Offline - identity restored locally but not synced
454
+ if (__DEV__) {
455
+ console.log('[Auth] Identity imported locally, will sync when online:', syncError);
456
+ }
457
+ return { synced: false };
458
+ }
459
+ } catch (error) {
460
+ const message = handleAuthError(error, {
461
+ defaultMessage: 'Failed to import identity. Please check your password and backup file.',
462
+ code: REGISTER_ERROR_CODE,
463
+ onError,
464
+ setAuthError: (msg: string) => setAuthState({ error: msg }),
465
+ logger,
466
+ });
467
+ loginFailure(message);
468
+ throw error;
469
+ } finally {
470
+ setAuthState({ isLoading: false });
471
+ }
472
+ },
473
+ [oxyServices, storage, setAuthState, loginFailure, onError, logger, setIdentitySynced],
474
+ );
475
+
476
+ /**
477
+ * Sign in with existing identity on device
478
+ */
479
+ const signIn = useCallback(
480
+ async (deviceName?: string): Promise<User> => {
481
+ if (!storage) throw new Error('Storage not initialized');
482
+
483
+ setAuthState({ isLoading: true, error: null });
484
+
485
+ try {
486
+ // Get stored public key
487
+ const publicKey = await KeyManager.getPublicKey();
488
+ if (!publicKey) {
489
+ throw new Error('No identity found on this device. Please create or import an identity.');
490
+ }
491
+
492
+ // Ensure identity is registered before attempting sign-in
493
+ const { registered } = await oxyServices.checkPublicKeyRegistered(publicKey);
494
+ if (!registered) {
495
+ throw new Error('Identity is not registered. Please register your identity in the Accounts app before signing in.');
496
+ }
497
+
498
+ return await performSignIn(publicKey);
499
+ } catch (error) {
500
+ const message = handleAuthError(error, {
501
+ defaultMessage: 'Sign in failed',
502
+ code: LOGIN_ERROR_CODE,
503
+ onError,
504
+ setAuthError: (msg: string) => setAuthState({ error: msg }),
505
+ logger,
506
+ });
507
+ loginFailure(message);
508
+ throw error;
509
+ } finally {
510
+ setAuthState({ isLoading: false });
511
+ }
512
+ },
513
+ [storage, setAuthState, performSignIn, loginFailure, onError, logger, oxyServices],
514
+ );
515
+
516
+ /**
517
+ * Logout from session
518
+ */
519
+ const logout = useCallback(
520
+ async (targetSessionId?: string): Promise<void> => {
521
+ if (!activeSessionId) return;
522
+
523
+ try {
524
+ const sessionToLogout = targetSessionId || activeSessionId;
525
+ await oxyServices.logoutSession(activeSessionId, sessionToLogout);
526
+
527
+ const filteredSessions = sessions.filter((session) => session.sessionId !== sessionToLogout);
528
+ updateSessions(filteredSessions, { merge: false });
529
+
530
+ if (sessionToLogout === activeSessionId) {
531
+ if (filteredSessions.length > 0) {
532
+ await switchSession(filteredSessions[0].sessionId);
533
+ } else {
534
+ await clearSessionState();
535
+ return;
536
+ }
537
+ }
538
+ } catch (error) {
539
+ const isInvalid = isInvalidSessionError(error);
540
+
541
+ if (isInvalid && targetSessionId === activeSessionId) {
542
+ await clearSessionState();
543
+ return;
544
+ }
545
+
546
+ handleAuthError(error, {
547
+ defaultMessage: 'Logout failed',
548
+ code: LOGOUT_ERROR_CODE,
549
+ onError,
550
+ setAuthError: (msg: string) => setAuthState({ error: msg }),
551
+ logger,
552
+ status: isInvalid ? 401 : undefined,
553
+ });
554
+ }
555
+ },
556
+ [
557
+ activeSessionId,
558
+ clearSessionState,
559
+ logger,
560
+ onError,
561
+ oxyServices,
562
+ sessions,
563
+ setAuthState,
564
+ switchSession,
565
+ updateSessions,
566
+ ],
567
+ );
568
+
569
+ /**
570
+ * Logout from all sessions
571
+ */
572
+ const logoutAll = useCallback(async (): Promise<void> => {
573
+ if (!activeSessionId) {
574
+ const error = new Error('No active session found');
575
+ setAuthState({ error: error.message });
576
+ onError?.({ message: error.message, code: LOGOUT_ALL_ERROR_CODE, status: 404 });
577
+ throw error;
578
+ }
579
+
580
+ try {
581
+ await oxyServices.logoutAllSessions(activeSessionId);
582
+ await clearSessionState();
583
+ } catch (error) {
584
+ handleAuthError(error, {
585
+ defaultMessage: 'Logout all failed',
586
+ code: LOGOUT_ALL_ERROR_CODE,
587
+ onError,
588
+ setAuthError: (msg: string) => setAuthState({ error: msg }),
589
+ logger,
590
+ });
591
+ throw error instanceof Error ? error : new Error('Logout all failed');
592
+ }
593
+ }, [activeSessionId, clearSessionState, logger, onError, oxyServices, setAuthState]);
594
+
595
+ /**
596
+ * Check if device has an identity stored
597
+ */
598
+ const hasIdentity = useCallback(async (): Promise<boolean> => {
599
+ return KeyManager.hasIdentity();
600
+ }, []);
601
+
602
+ /**
603
+ * Get the public key of the stored identity
604
+ */
605
+ const getPublicKey = useCallback(async (): Promise<string | null> => {
606
+ return KeyManager.getPublicKey();
607
+ }, []);
608
+
609
+ return {
610
+ createIdentity,
611
+ importIdentity,
612
+ signIn,
613
+ logout,
614
+ logoutAll,
615
+ hasIdentity,
616
+ getPublicKey,
617
+ isIdentitySynced: isIdentitySyncedFn,
618
+ syncIdentity,
619
+ };
620
+ };