@digilogiclabs/platform-core 1.18.0 → 1.20.0

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.
package/dist/auth.mjs CHANGED
@@ -1749,6 +1749,83 @@ function createSecureHandlerFactory(factoryConfig) {
1749
1749
  };
1750
1750
  }
1751
1751
 
1752
+ // src/auth/ensure-user.ts
1753
+ function createEnsureUser(config) {
1754
+ return async function ensureUser(keycloakId, email, name) {
1755
+ if (!keycloakId) {
1756
+ throw new Error(
1757
+ "createEnsureUser: keycloakId is required (got empty string / null / undefined)"
1758
+ );
1759
+ }
1760
+ const normalizedEmail = normalizeEmail(email);
1761
+ const normalizedName = normalizeName(name);
1762
+ const byKeycloak = await config.findByKeycloakId(keycloakId);
1763
+ if (byKeycloak) {
1764
+ if (config.onClaimRefresh) {
1765
+ const delta = computeClaimDelta(
1766
+ byKeycloak,
1767
+ normalizedEmail,
1768
+ normalizedName
1769
+ );
1770
+ if (delta) {
1771
+ await config.onClaimRefresh(byKeycloak.id, delta);
1772
+ }
1773
+ }
1774
+ return byKeycloak.id;
1775
+ }
1776
+ if (normalizedEmail) {
1777
+ const byEmail = await config.findByEmailLower(
1778
+ normalizedEmail.toLowerCase()
1779
+ );
1780
+ if (byEmail) {
1781
+ await config.rebindKeycloakId(byEmail.id, keycloakId, normalizedName);
1782
+ return byEmail.id;
1783
+ }
1784
+ }
1785
+ const inserted = await config.insertNewUser({
1786
+ keycloakId,
1787
+ email: normalizedEmail,
1788
+ name: normalizedName
1789
+ });
1790
+ if (config.onNewUser) {
1791
+ try {
1792
+ await config.onNewUser(inserted.id, normalizedEmail);
1793
+ } catch {
1794
+ }
1795
+ }
1796
+ return inserted.id;
1797
+ };
1798
+ }
1799
+ function normalizeEmail(email) {
1800
+ if (email === void 0 || email === null) return null;
1801
+ const trimmed = email.trim();
1802
+ return trimmed === "" ? null : trimmed;
1803
+ }
1804
+ function normalizeName(name) {
1805
+ if (name === void 0 || name === null) return void 0;
1806
+ const trimmed = name.trim();
1807
+ return trimmed === "" ? void 0 : trimmed;
1808
+ }
1809
+ function computeClaimDelta(current, newEmail, newName) {
1810
+ const delta = {};
1811
+ let changed = false;
1812
+ if (current.email !== void 0) {
1813
+ const currentLower = current.email == null ? null : current.email.toLowerCase();
1814
+ const newLower = newEmail == null ? null : newEmail.toLowerCase();
1815
+ if (currentLower !== newLower) {
1816
+ delta.email = newEmail;
1817
+ changed = true;
1818
+ }
1819
+ }
1820
+ if (newName !== void 0 && current.displayName !== void 0) {
1821
+ if (current.displayName !== newName) {
1822
+ delta.displayName = newName;
1823
+ changed = true;
1824
+ }
1825
+ }
1826
+ return changed ? delta : null;
1827
+ }
1828
+
1752
1829
  // src/env.ts
1753
1830
  function getRequiredEnv(key) {
1754
1831
  const value = process.env[key];
@@ -1879,6 +1956,7 @@ export {
1879
1956
  createAuditActor,
1880
1957
  createAuditLogger,
1881
1958
  createBetaClient,
1959
+ createEnsureUser,
1882
1960
  createFeatureFlags,
1883
1961
  createLazyRateLimitStore,
1884
1962
  createMemoryRateLimitStore,