@bagelink/auth 1.7.94 → 1.7.98

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/api.d.ts CHANGED
@@ -1,7 +1,16 @@
1
- import { RegisterRequest, UpdateAccountRequest, ChangePasswordRequest, ResetPasswordRequest, SendVerificationRequest, AuthenticationAccount, LoginResponse, RegisterResponse, LogoutResponse, GetMeResponse, UpdateMeResponse, DeleteMeResponse, GetAccountResponse, UpdateAccountResponse, DeleteAccountResponse, ActivateAccountResponse, DeactivateAccountResponse, ChangePasswordResponse, ForgotPasswordResponse, ResetPasswordResponse, VerifyResetTokenResponse, SendVerificationResponse, VerifyEmailResponse, RefreshSessionResponse, GetSessionsResponse, DeleteSessionResponse, DeleteAllSessionsResponse, CleanupSessionsResponse, GetMethodsResponse, SSOProvider, SSOInitiateRequest, SSOCallbackRequest, SSOLinkRequest, SSOInitiateResponse, SSOCallbackResponse, SSOLinkResponse, SSOUnlinkResponse } from './types';
1
+ import { RegisterRequest, UpdateAccountRequest, ChangePasswordRequest, ResetPasswordRequest, SendVerificationRequest, AuthenticationAccount, LoginResponse, RegisterResponse, LogoutResponse, GetMeResponse, UpdateMeResponse, DeleteMeResponse, GetAccountResponse, UpdateAccountResponse, DeleteAccountResponse, ActivateAccountResponse, DeactivateAccountResponse, ChangePasswordResponse, ForgotPasswordResponse, ResetPasswordResponse, VerifyResetTokenResponse, SendVerificationResponse, VerifyEmailResponse, RefreshSessionResponse, GetSessionsResponse, DeleteSessionResponse, DeleteAllSessionsResponse, CleanupSessionsResponse, GetMethodsResponse, SSOProvider, SSOInitiateRequest, SSOCallbackRequest, SSOLinkRequest, SSOInitiateResponse, SSOCallbackResponse, SSOLinkResponse, SSOUnlinkResponse, GetTenantsResponse } from './types';
2
2
  export declare class AuthApi {
3
3
  private api;
4
+ private currentTenantId;
4
5
  constructor(baseURL?: string);
6
+ /**
7
+ * Set the current tenant ID for multi-tenant requests
8
+ */
9
+ setTenantId(tenantId: string | null): void;
10
+ /**
11
+ * Get the current tenant ID
12
+ */
13
+ getTenantId(): string | null;
5
14
  private setupInterceptors;
6
15
  /**
7
16
  * Get available authentication methods
@@ -112,4 +121,8 @@ export declare class AuthApi {
112
121
  * Cleanup expired sessions (admin)
113
122
  */
114
123
  cleanupSessions(): Promise<CleanupSessionsResponse>;
124
+ /**
125
+ * Get list of tenants the authenticated user belongs to
126
+ */
127
+ getTenants(): Promise<GetTenantsResponse>;
115
128
  }
package/dist/index.cjs CHANGED
@@ -65,9 +65,22 @@ function queryParams() {
65
65
  class AuthApi {
66
66
  constructor(baseURL = "") {
67
67
  __publicField(this, "api");
68
+ __publicField(this, "currentTenantId", null);
68
69
  this.api = createAxiosInstance(baseURL);
69
70
  this.setupInterceptors();
70
71
  }
72
+ /**
73
+ * Set the current tenant ID for multi-tenant requests
74
+ */
75
+ setTenantId(tenantId) {
76
+ this.currentTenantId = tenantId;
77
+ }
78
+ /**
79
+ * Get the current tenant ID
80
+ */
81
+ getTenantId() {
82
+ return this.currentTenantId;
83
+ }
71
84
  setupInterceptors() {
72
85
  this.api.interceptors.request.use((config) => {
73
86
  const urlParams = new URLSearchParams(window.location.search);
@@ -75,6 +88,9 @@ class AuthApi {
75
88
  if (resetToken !== null) {
76
89
  config.headers["X-Reset-Token"] = resetToken;
77
90
  }
91
+ if (this.currentTenantId !== null) {
92
+ config.headers["X-Tenant-ID"] = this.currentTenantId;
93
+ }
78
94
  return config;
79
95
  });
80
96
  }
@@ -85,13 +101,13 @@ class AuthApi {
85
101
  * Get available authentication methods
86
102
  */
87
103
  async getAuthMethods() {
88
- return this.api.get("/authentication/methods");
104
+ return this.api.get("authentication/methods");
89
105
  }
90
106
  /**
91
107
  * Register a new account
92
108
  */
93
109
  async register(data) {
94
- return this.api.post("/authentication/register", {
110
+ return this.api.post("authentication/register", {
95
111
  ...data,
96
112
  email: data.email.toLowerCase()
97
113
  });
@@ -100,7 +116,7 @@ class AuthApi {
100
116
  * Login with password
101
117
  */
102
118
  async login(email, password) {
103
- return this.api.post("/authentication/login/password", {
119
+ return this.api.post("authentication/login/password", {
104
120
  email: email.toLowerCase(),
105
121
  password
106
122
  });
@@ -109,13 +125,13 @@ class AuthApi {
109
125
  * Logout and clear session
110
126
  */
111
127
  async logout() {
112
- return this.api.post("/authentication/logout", {});
128
+ return this.api.post("authentication/logout", {});
113
129
  }
114
130
  /**
115
131
  * Refresh current session
116
132
  */
117
133
  async refreshSession() {
118
- return this.api.post("/authentication/refresh", {});
134
+ return this.api.post("authentication/refresh", {});
119
135
  }
120
136
  // ============================================
121
137
  // SSO Authentication Methods
@@ -125,7 +141,7 @@ class AuthApi {
125
141
  * Returns authorization URL to redirect user to
126
142
  */
127
143
  async initiateSSO(data) {
128
- return this.api.post(`/authentication/sso/${data.provider}/initiate`, {
144
+ return this.api.post(`authentication/sso/${data.provider}/initiate`, {
129
145
  redirect_uri: data.redirect_uri,
130
146
  state: data.state
131
147
  });
@@ -134,7 +150,7 @@ class AuthApi {
134
150
  * Complete SSO login after callback from provider
135
151
  */
136
152
  async ssoCallback(data) {
137
- return this.api.post(`/authentication/sso/${data.provider}/callback`, {
153
+ return this.api.post(`authentication/sso/${data.provider}/callback`, {
138
154
  code: data.code,
139
155
  state: data.state
140
156
  });
@@ -143,7 +159,7 @@ class AuthApi {
143
159
  * Link an SSO provider to existing account
144
160
  */
145
161
  async linkSSOProvider(data) {
146
- return this.api.post(`/authentication/sso/${data.provider}/link`, {
162
+ return this.api.post(`authentication/sso/${data.provider}/link`, {
147
163
  code: data.code,
148
164
  state: data.state
149
165
  });
@@ -152,7 +168,7 @@ class AuthApi {
152
168
  * Unlink an SSO provider from account
153
169
  */
154
170
  async unlinkSSOProvider(provider) {
155
- return this.api.delete(`/authentication/sso/${provider}/unlink`);
171
+ return this.api.delete(`authentication/sso/${provider}/unlink`);
156
172
  }
157
173
  // ============================================
158
174
  // Current User (Me) Methods
@@ -161,19 +177,19 @@ class AuthApi {
161
177
  * Get current user account info
162
178
  */
163
179
  async getCurrentUser() {
164
- return this.api.get("/authentication/me");
180
+ return this.api.get("authentication/me");
165
181
  }
166
182
  /**
167
183
  * Update current user profile
168
184
  */
169
185
  async updateCurrentUser(data) {
170
- return this.api.patch("/authentication/me", data);
186
+ return this.api.patch("authentication/me", data);
171
187
  }
172
188
  /**
173
189
  * Delete current user account
174
190
  */
175
191
  async deleteCurrentUser() {
176
- return this.api.delete("/authentication/me");
192
+ return this.api.delete("authentication/me");
177
193
  }
178
194
  // ============================================
179
195
  // Account Management (Admin)
@@ -182,31 +198,31 @@ class AuthApi {
182
198
  * Get account information by ID
183
199
  */
184
200
  async getAccount(accountId) {
185
- return this.api.get(`/authentication/account/${accountId}`);
201
+ return this.api.get(`authentication/account/${accountId}`);
186
202
  }
187
203
  /**
188
204
  * Update account by ID
189
205
  */
190
206
  async updateAccount(accountId, data) {
191
- return this.api.patch(`/authentication/account/${accountId}`, data);
207
+ return this.api.patch(`authentication/account/${accountId}`, data);
192
208
  }
193
209
  /**
194
210
  * Delete account by ID
195
211
  */
196
212
  async deleteAccount(accountId) {
197
- return this.api.delete(`/authentication/account/${accountId}`);
213
+ return this.api.delete(`authentication/account/${accountId}`);
198
214
  }
199
215
  /**
200
216
  * Activate account by ID
201
217
  */
202
218
  async activateAccount(accountId) {
203
- return this.api.post(`/authentication/account/${accountId}/activate`, {});
219
+ return this.api.post(`authentication/account/${accountId}/activate`, {});
204
220
  }
205
221
  /**
206
222
  * Deactivate account by ID
207
223
  */
208
224
  async deactivateAccount(accountId) {
209
- return this.api.post(`/authentication/account/${accountId}/deactivate`, {});
225
+ return this.api.post(`authentication/account/${accountId}/deactivate`, {});
210
226
  }
211
227
  // ============================================
212
228
  // Password Management
@@ -215,13 +231,13 @@ class AuthApi {
215
231
  * Change password (requires current password)
216
232
  */
217
233
  async changePassword(data) {
218
- return this.api.post("/authentication/password/change", data);
234
+ return this.api.post("authentication/password/change", data);
219
235
  }
220
236
  /**
221
237
  * Initiate forgot password flow
222
238
  */
223
239
  async forgotPassword(email) {
224
- return this.api.post("/authentication/password/forgot", {
240
+ return this.api.post("authentication/password/forgot", {
225
241
  email: email.toLowerCase()
226
242
  });
227
243
  }
@@ -229,13 +245,13 @@ class AuthApi {
229
245
  * Verify password reset token
230
246
  */
231
247
  async verifyResetToken(token) {
232
- return this.api.get(`/authentication/password/verify-reset-token/${token}`);
248
+ return this.api.get(`authentication/password/verify-reset-token/${token}`);
233
249
  }
234
250
  /**
235
251
  * Reset password with token
236
252
  */
237
253
  async resetPassword(data) {
238
- return this.api.post("/authentication/password/reset", data);
254
+ return this.api.post("authentication/password/reset", data);
239
255
  }
240
256
  // ============================================
241
257
  // Email Verification
@@ -244,7 +260,7 @@ class AuthApi {
244
260
  * Send email verification
245
261
  */
246
262
  async sendVerification(data = {}, user) {
247
- return this.api.post("/authentication/verify/send", data, {
263
+ return this.api.post("authentication/verify/send", data, {
248
264
  params: user ? { user } : void 0
249
265
  });
250
266
  }
@@ -252,7 +268,7 @@ class AuthApi {
252
268
  * Verify email with token
253
269
  */
254
270
  async verifyEmail(token) {
255
- return this.api.post("/authentication/verify/email", { token });
271
+ return this.api.post("authentication/verify/email", { token });
256
272
  }
257
273
  // ============================================
258
274
  // Session Management
@@ -261,25 +277,34 @@ class AuthApi {
261
277
  * Get sessions for an account
262
278
  */
263
279
  async getSessions(accountId) {
264
- return this.api.get(`/authentication/sessions/${accountId}`);
280
+ return this.api.get(`authentication/sessions/${accountId}`);
265
281
  }
266
282
  /**
267
283
  * Revoke a specific session
268
284
  */
269
285
  async revokeSession(sessionToken) {
270
- return this.api.delete(`/authentication/sessions/${sessionToken}`);
286
+ return this.api.delete(`authentication/sessions/${sessionToken}`);
271
287
  }
272
288
  /**
273
289
  * Revoke all sessions for an account
274
290
  */
275
291
  async revokeAllSessions(accountId) {
276
- return this.api.delete(`/authentication/sessions/account/${accountId}`);
292
+ return this.api.delete(`authentication/sessions/account/${accountId}`);
277
293
  }
278
294
  /**
279
295
  * Cleanup expired sessions (admin)
280
296
  */
281
297
  async cleanupSessions() {
282
- return this.api.post("/authentication/cleanup-sessions", {});
298
+ return this.api.post("authentication/cleanup-sessions", {});
299
+ }
300
+ // ============================================
301
+ // Multi-Tenancy Methods
302
+ // ============================================
303
+ /**
304
+ * Get list of tenants the authenticated user belongs to
305
+ */
306
+ async getTenants() {
307
+ return this.api.get("tenants");
283
308
  }
284
309
  }
285
310
  const _hoisted_1$8 = { class: "txt20 bold mb-1" };
@@ -1902,35 +1927,24 @@ function accountToUser(account) {
1902
1927
  if (account === null) {
1903
1928
  return null;
1904
1929
  }
1905
- if (account.person !== void 0) {
1930
+ const hasPersonLinked = account.person !== void 0 && account.person !== null;
1931
+ if (hasPersonLinked) {
1906
1932
  return {
1907
1933
  id: account.person.id,
1908
1934
  accountId: account.id,
1909
1935
  name: account.person.name,
1910
- email: account.person.email,
1936
+ email: account.person.email ?? void 0,
1911
1937
  type: account.account_type,
1912
1938
  roles: account.person.roles,
1913
1939
  isActive: account.is_active,
1914
1940
  isVerified: account.is_verified,
1915
1941
  person: account.person,
1916
- lastLogin: account.last_login
1917
- };
1918
- }
1919
- if (account.entity !== void 0) {
1920
- return {
1921
- id: account.entity.id,
1922
- accountId: account.id,
1923
- name: account.entity.name,
1924
- type: account.account_type,
1925
- isActive: account.is_active,
1926
- isVerified: account.is_verified,
1927
- lastLogin: account.last_login,
1928
- entityType: account.entity.type,
1929
- metadata: account.entity.metadata
1942
+ lastLogin: account.last_login ?? void 0,
1943
+ hasPersonLinked: true
1930
1944
  };
1931
1945
  }
1932
1946
  const emailMethod = account.authentication_methods.find(
1933
- (m) => m.type === "password" || m.type === "email_token"
1947
+ (m) => m.type === "password" || m.type === "email_token" || m.type === "sso"
1934
1948
  );
1935
1949
  return {
1936
1950
  id: account.id,
@@ -1940,7 +1954,8 @@ function accountToUser(account) {
1940
1954
  type: account.account_type,
1941
1955
  isActive: account.is_active,
1942
1956
  isVerified: account.is_verified,
1943
- lastLogin: account.last_login
1957
+ lastLogin: account.last_login ?? void 0,
1958
+ hasPersonLinked: false
1944
1959
  };
1945
1960
  }
1946
1961
  const DEFAULT_REDIRECT_CONFIG = {
@@ -1965,6 +1980,8 @@ let redirectConfig = null;
1965
1980
  let autoRedirectRouter = null;
1966
1981
  let cachedAuthGuard = null;
1967
1982
  const accountInfo = vue.ref(null);
1983
+ const tenants = vue.ref([]);
1984
+ const currentTenant = vue.ref(null);
1968
1985
  function getRedirectConfig() {
1969
1986
  if (!redirectConfig) {
1970
1987
  throw new Error("Redirect config not initialized. Did you call createAuth with redirect config?");
@@ -2135,15 +2152,11 @@ function useAuth() {
2135
2152
  };
2136
2153
  const getAccountType = () => {
2137
2154
  var _a;
2138
- return ((_a = user.value) == null ? void 0 : _a.type) ?? "person";
2155
+ return ((_a = user.value) == null ? void 0 : _a.type) ?? "identity";
2139
2156
  };
2140
2157
  const isPersonAccount = () => {
2141
2158
  var _a;
2142
- return ((_a = user.value) == null ? void 0 : _a.type) === "person";
2143
- };
2144
- const isEntityAccount = () => {
2145
- var _a;
2146
- return ((_a = user.value) == null ? void 0 : _a.type) === "entity";
2159
+ return ((_a = user.value) == null ? void 0 : _a.hasPersonLinked) === true;
2147
2160
  };
2148
2161
  async function logout() {
2149
2162
  const logoutPromise = api.logout();
@@ -2176,6 +2189,45 @@ function useAuth() {
2176
2189
  return false;
2177
2190
  }
2178
2191
  }
2192
+ async function loadTenants() {
2193
+ try {
2194
+ const { data } = await api.getTenants();
2195
+ tenants.value = data;
2196
+ if (currentTenant.value === null && tenants.value.length > 0) {
2197
+ const firstActiveTenant = tenants.value.find((t) => t.status === "active");
2198
+ if (firstActiveTenant !== void 0) {
2199
+ setTenant(firstActiveTenant.id);
2200
+ }
2201
+ }
2202
+ return tenants.value;
2203
+ } catch {
2204
+ tenants.value = [];
2205
+ return [];
2206
+ }
2207
+ }
2208
+ function setTenant(tenantId) {
2209
+ if (tenantId === null) {
2210
+ currentTenant.value = null;
2211
+ api.setTenantId(null);
2212
+ return;
2213
+ }
2214
+ const tenant = tenants.value.find((t) => t.id === tenantId);
2215
+ if (tenant !== void 0) {
2216
+ currentTenant.value = tenant;
2217
+ api.setTenantId(tenantId);
2218
+ } else {
2219
+ throw new Error(`Tenant with ID ${tenantId} not found`);
2220
+ }
2221
+ }
2222
+ function switchTenant(tenantId) {
2223
+ setTenant(tenantId);
2224
+ }
2225
+ function getTenants() {
2226
+ return tenants.value;
2227
+ }
2228
+ function getCurrentTenant() {
2229
+ return currentTenant.value;
2230
+ }
2179
2231
  async function signup(newUser) {
2180
2232
  const hasPassword = newUser.password !== void 0 && newUser.password.length > 0;
2181
2233
  if (hasPassword && newUser.password !== newUser.confirmPassword) {
@@ -2290,6 +2342,9 @@ function useAuth() {
2290
2342
  accountInfo,
2291
2343
  // SSO Providers (ready to use!)
2292
2344
  sso,
2345
+ // Multi-Tenancy State
2346
+ tenants,
2347
+ currentTenant,
2293
2348
  // Getters
2294
2349
  getFullName,
2295
2350
  getIsLoggedIn,
@@ -2297,7 +2352,8 @@ function useAuth() {
2297
2352
  getRoles,
2298
2353
  getAccountType,
2299
2354
  isPersonAccount,
2300
- isEntityAccount,
2355
+ getTenants,
2356
+ getCurrentTenant,
2301
2357
  // Authentication Actions
2302
2358
  login,
2303
2359
  logout,
@@ -2327,7 +2383,11 @@ function useAuth() {
2327
2383
  // Session Management
2328
2384
  getSessions,
2329
2385
  revokeSession,
2330
- revokeAllSessions
2386
+ revokeAllSessions,
2387
+ // Multi-Tenancy Actions
2388
+ loadTenants,
2389
+ setTenant,
2390
+ switchTenant
2331
2391
  };
2332
2392
  }
2333
2393
  const useAuth$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({