@bagelink/auth 1.4.167 → 1.4.171

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/index.mjs CHANGED
@@ -49,74 +49,199 @@ class AuthApi {
49
49
  }
50
50
  setupInterceptors() {
51
51
  this.api.interceptors.request.use((config) => {
52
- const token = localStorage.getItem("access_token");
53
- if (token !== null && config.headers) {
54
- config.headers.Authorization = `Bearer ${token}`;
52
+ const sessionToken = localStorage.getItem("session_token");
53
+ if (sessionToken !== null && config.headers) {
54
+ config.headers.Authorization = `Bearer ${sessionToken}`;
55
55
  }
56
56
  const urlParams = new URLSearchParams(window.location.search);
57
57
  const resetToken = urlParams.get("token");
58
58
  if (resetToken !== null && config.headers) {
59
- config.headers.Authorization = `Bearer ${resetToken}`;
59
+ config.headers["X-Reset-Token"] = resetToken;
60
60
  }
61
61
  return config;
62
62
  });
63
63
  }
64
- async login(username, password) {
65
- const { data } = await this.api.post("/auth/login", {
66
- username: username.toLowerCase(),
64
+ // ============================================
65
+ // Authentication Methods
66
+ // ============================================
67
+ /**
68
+ * Get available authentication methods
69
+ */
70
+ async getAuthMethods() {
71
+ return this.api.get("/authentication/methods");
72
+ }
73
+ /**
74
+ * Register a new account
75
+ */
76
+ async register(data) {
77
+ const response = await this.api.post("/authentication/register", {
78
+ ...data,
79
+ email: data.email.toLowerCase()
80
+ });
81
+ if (response.data.session_token) {
82
+ localStorage.setItem("session_token", response.data.session_token);
83
+ }
84
+ return response;
85
+ }
86
+ /**
87
+ * Login with password
88
+ */
89
+ async login(email, password) {
90
+ const response = await this.api.post("/authentication/login/password", {
91
+ email: email.toLowerCase(),
67
92
  password
68
93
  });
69
- localStorage.setItem("access_token", data.access_token);
70
- return { data };
71
- }
72
- logout() {
73
- localStorage.removeItem("access_token");
74
- window.location.reload();
75
- }
76
- async passwordRecovery(email) {
77
- return this.api.post("/auth/password-recovery", { email });
78
- }
79
- async resetPassword(newPassword) {
80
- return this.api.post("/auth/reset-password", { new_password: newPassword });
81
- }
94
+ if (response.data.session_token) {
95
+ localStorage.setItem("session_token", response.data.session_token);
96
+ }
97
+ return response;
98
+ }
99
+ /**
100
+ * Logout and clear session
101
+ */
102
+ async logout() {
103
+ const response = await this.api.post("/authentication/logout", {});
104
+ localStorage.removeItem("session_token");
105
+ return response;
106
+ }
107
+ /**
108
+ * Refresh current session
109
+ */
110
+ async refreshSession() {
111
+ const response = await this.api.post("/authentication/refresh", {});
112
+ if (response.data.session_token) {
113
+ localStorage.setItem("session_token", response.data.session_token);
114
+ }
115
+ return response;
116
+ }
117
+ // ============================================
118
+ // Current User (Me) Methods
119
+ // ============================================
120
+ /**
121
+ * Get current user account info
122
+ */
82
123
  async getCurrentUser() {
83
- return this.api.get("/users/me");
84
- }
85
- async signup(user) {
86
- return this.api.post("/users/signup", {
87
- email: user.email.toLowerCase(),
88
- password: user.password,
89
- first_name: user.first_name,
90
- last_name: user.last_name
91
- });
92
- }
93
- async updatePassword(form) {
94
- return this.api.patch("/users/me/password", {
95
- current_password: form.current_password,
96
- new_password: form.new_password
97
- });
98
- }
99
- async updateUserProfile(user) {
100
- return this.api.patch("/users/me", user);
101
- }
102
- async setUserStatus(userId, isActive) {
103
- return this.api.patch(`/users/${userId}`, {
104
- is_active: isActive
124
+ return this.api.get("/authentication/me");
125
+ }
126
+ /**
127
+ * Update current user profile
128
+ */
129
+ async updateCurrentUser(data) {
130
+ return this.api.patch("/authentication/me", data);
131
+ }
132
+ /**
133
+ * Delete current user account
134
+ */
135
+ async deleteCurrentUser() {
136
+ const response = await this.api.delete("/authentication/me");
137
+ localStorage.removeItem("session_token");
138
+ return response;
139
+ }
140
+ // ============================================
141
+ // Account Management (Admin)
142
+ // ============================================
143
+ /**
144
+ * Get account information by ID
145
+ */
146
+ async getAccount(accountId) {
147
+ return this.api.get(`/authentication/account/${accountId}`);
148
+ }
149
+ /**
150
+ * Update account by ID
151
+ */
152
+ async updateAccount(accountId, data) {
153
+ return this.api.patch(`/authentication/account/${accountId}`, data);
154
+ }
155
+ /**
156
+ * Delete account by ID
157
+ */
158
+ async deleteAccount(accountId) {
159
+ return this.api.delete(`/authentication/account/${accountId}`);
160
+ }
161
+ /**
162
+ * Activate account by ID
163
+ */
164
+ async activateAccount(accountId) {
165
+ return this.api.post(`/authentication/account/${accountId}/activate`, {});
166
+ }
167
+ /**
168
+ * Deactivate account by ID
169
+ */
170
+ async deactivateAccount(accountId) {
171
+ return this.api.post(`/authentication/account/${accountId}/deactivate`, {});
172
+ }
173
+ // ============================================
174
+ // Password Management
175
+ // ============================================
176
+ /**
177
+ * Change password (requires current password)
178
+ */
179
+ async changePassword(data) {
180
+ return this.api.post("/authentication/password/change", data);
181
+ }
182
+ /**
183
+ * Initiate forgot password flow
184
+ */
185
+ async forgotPassword(email) {
186
+ return this.api.post("/authentication/password/forgot", {
187
+ email: email.toLowerCase()
105
188
  });
106
189
  }
107
- async deleteUser(userId) {
108
- return this.api.delete(`/users/${userId}`);
109
- }
110
- async getUsers(limit = 100, skip) {
111
- return this.api.get("/users/", {
112
- params: { skip, limit }
190
+ /**
191
+ * Verify password reset token
192
+ */
193
+ async verifyResetToken(token) {
194
+ return this.api.get(`/authentication/password/verify-reset-token/${token}`);
195
+ }
196
+ /**
197
+ * Reset password with token
198
+ */
199
+ async resetPassword(data) {
200
+ return this.api.post("/authentication/password/reset", data);
201
+ }
202
+ // ============================================
203
+ // Email Verification
204
+ // ============================================
205
+ /**
206
+ * Send email verification
207
+ */
208
+ async sendVerification(data = {}, user) {
209
+ return this.api.post("/authentication/verify/send", data, {
210
+ params: user ? { user } : void 0
113
211
  });
114
212
  }
115
- async createUser(user) {
116
- return this.api.post("/users/", user);
117
- }
118
- async getUser(userId) {
119
- return this.api.get(`/users/${userId}`);
213
+ /**
214
+ * Verify email with token
215
+ */
216
+ async verifyEmail(token) {
217
+ return this.api.post("/authentication/verify/email", { token });
218
+ }
219
+ // ============================================
220
+ // Session Management
221
+ // ============================================
222
+ /**
223
+ * Get sessions for an account
224
+ */
225
+ async getSessions(accountId) {
226
+ return this.api.get(`/authentication/sessions/${accountId}`);
227
+ }
228
+ /**
229
+ * Revoke a specific session
230
+ */
231
+ async revokeSession(sessionToken) {
232
+ return this.api.delete(`/authentication/sessions/${sessionToken}`);
233
+ }
234
+ /**
235
+ * Revoke all sessions for an account
236
+ */
237
+ async revokeAllSessions(accountId) {
238
+ return this.api.delete(`/authentication/sessions/account/${accountId}`);
239
+ }
240
+ /**
241
+ * Cleanup expired sessions (admin)
242
+ */
243
+ async cleanupSessions() {
244
+ return this.api.post("/authentication/cleanup-sessions", {});
120
245
  }
121
246
  }
122
247
 
@@ -125,41 +250,43 @@ var AuthState = /* @__PURE__ */ ((AuthState2) => {
125
250
  AuthState2["LOGOUT"] = "logout";
126
251
  AuthState2["SIGNUP"] = "signup";
127
252
  AuthState2["PASSWORD_RESET"] = "password_reset";
253
+ AuthState2["PASSWORD_CHANGE"] = "password_change";
128
254
  AuthState2["PROFILE_UPDATE"] = "profile_update";
129
255
  AuthState2["AUTH_CHECK"] = "auth_check";
256
+ AuthState2["EMAIL_VERIFIED"] = "email_verified";
257
+ AuthState2["SESSION_REFRESH"] = "session_refresh";
130
258
  return AuthState2;
131
259
  })(AuthState || {});
132
260
 
133
261
  let authApi = null;
134
262
  let eventEmitter = null;
135
- const currentUser = ref({
136
- id: "",
137
- email: "",
138
- first_name: "",
139
- last_name: "",
140
- is_superuser: false,
141
- is_active: false
142
- });
263
+ const currentUser = ref(null);
143
264
  function initAuth({
144
265
  axios,
145
266
  baseURL
146
267
  }) {
147
- if (!authApi) {
268
+ if (authApi === null) {
148
269
  authApi = new AuthApi(axios, baseURL);
149
270
  }
150
- if (!eventEmitter) {
271
+ if (eventEmitter === null) {
151
272
  eventEmitter = new EventEmitter();
152
273
  }
153
274
  return {
154
275
  // Event listener methods
155
276
  on(event, handler) {
156
- eventEmitter.on(event, handler);
277
+ if (eventEmitter) {
278
+ eventEmitter.on(event, handler);
279
+ }
157
280
  },
158
281
  off(event, handler) {
159
- eventEmitter.off(event, handler);
282
+ if (eventEmitter) {
283
+ eventEmitter.off(event, handler);
284
+ }
160
285
  },
161
286
  removeAllListeners(event) {
162
- eventEmitter.removeAllListeners(event);
287
+ if (eventEmitter) {
288
+ eventEmitter.removeAllListeners(event);
289
+ }
163
290
  },
164
291
  install(app) {
165
292
  app.config.globalProperties.$auth = useAuth();
@@ -167,113 +294,149 @@ function initAuth({
167
294
  };
168
295
  }
169
296
  function useAuth() {
170
- if (!authApi) {
297
+ if (authApi === null) {
171
298
  throw new Error("Auth not initialized. Call initAuth first.");
172
299
  }
173
- const getFullName = () => `${currentUser.value.first_name} ${currentUser.value.last_name}`;
174
- const getIsLoggedIn = () => currentUser.value.id.length > 0;
175
- async function logout() {
176
- try {
177
- await authApi.logout();
178
- currentUser.value = {
179
- id: "",
180
- email: "",
181
- first_name: "",
182
- last_name: "",
183
- is_superuser: false,
184
- is_active: false
185
- };
186
- eventEmitter.emit(AuthState.LOGOUT);
187
- } catch (error) {
188
- throw error;
300
+ if (eventEmitter === null) {
301
+ throw new Error("Event emitter not initialized. Call initAuth first.");
302
+ }
303
+ const api = authApi;
304
+ const emitter = eventEmitter;
305
+ const getFullName = () => {
306
+ const user = currentUser.value;
307
+ if (user === null) return "";
308
+ if (user.person !== void 0) return user.person.name;
309
+ return user.display_name ?? "";
310
+ };
311
+ const getIsLoggedIn = () => {
312
+ const user = currentUser.value;
313
+ return user !== null && user.id.length > 0;
314
+ };
315
+ const getEmail = () => {
316
+ const user = currentUser.value;
317
+ if (user === null) return "";
318
+ if (user.person?.email !== void 0) {
319
+ return user.person.email;
189
320
  }
321
+ const emailMethod = user.authentication_methods?.find(
322
+ (m) => m.type === "password" || m.type === "email_token"
323
+ );
324
+ return emailMethod?.identifier ?? "";
325
+ };
326
+ async function logout() {
327
+ const logoutPromise = api.logout();
328
+ await logoutPromise.catch(() => {
329
+ });
330
+ currentUser.value = null;
331
+ emitter.emit(AuthState.LOGOUT);
190
332
  }
191
333
  async function login(credentials) {
192
- try {
193
- await authApi.login(
194
- credentials.email.toLowerCase(),
195
- credentials.password
196
- );
334
+ const { data } = await api.login(
335
+ credentials.email.toLowerCase(),
336
+ credentials.password
337
+ );
338
+ if (data.success === true && data.requires_verification !== true) {
197
339
  await checkAuth();
198
- eventEmitter.emit(AuthState.LOGIN);
199
- } catch (error) {
200
- throw error;
201
340
  }
341
+ emitter.emit(AuthState.LOGIN);
342
+ return data;
202
343
  }
203
344
  async function checkAuth() {
204
345
  try {
205
- if (!getIsLoggedIn()) {
206
- const { data } = await authApi.getCurrentUser();
207
- currentUser.value = data;
208
- if (getIsLoggedIn()) {
209
- eventEmitter.emit(AuthState.AUTH_CHECK);
210
- }
346
+ const { data } = await api.getCurrentUser();
347
+ currentUser.value = data;
348
+ if (getIsLoggedIn()) {
349
+ emitter.emit(AuthState.AUTH_CHECK);
211
350
  }
212
- } catch (error) {
351
+ return true;
352
+ } catch {
353
+ currentUser.value = null;
213
354
  return false;
214
355
  }
215
- return getIsLoggedIn();
216
356
  }
217
357
  async function signup(user) {
218
- try {
219
- if (user.password !== user.confirmPassword) {
220
- throw new Error("Passwords do not match");
221
- }
222
- const { data } = await authApi.signup(user);
223
- currentUser.value = data;
224
- eventEmitter.emit(AuthState.SIGNUP);
225
- } catch (error) {
226
- throw error;
358
+ if (user.password && user.password !== user.confirmPassword) {
359
+ throw new Error("Passwords do not match");
227
360
  }
228
- }
229
- async function recoverPassword(email) {
230
- try {
231
- await authApi.passwordRecovery(email);
232
- } catch (error) {
233
- throw error;
361
+ const { data } = await api.register({
362
+ email: user.email,
363
+ first_name: user.first_name,
364
+ last_name: user.last_name,
365
+ phone_number: user.phone_number,
366
+ password: user.password
367
+ });
368
+ if (data.success === true && data.requires_verification !== true) {
369
+ await checkAuth();
234
370
  }
371
+ emitter.emit(AuthState.SIGNUP);
372
+ return data;
235
373
  }
236
- async function resetPassword(newPassword) {
237
- try {
238
- await authApi.resetPassword(newPassword);
239
- eventEmitter.emit(AuthState.PASSWORD_RESET);
240
- } catch (error) {
241
- throw error;
242
- }
374
+ async function forgotPassword(email) {
375
+ await api.forgotPassword(email);
243
376
  }
244
- async function updatePassword(form) {
245
- try {
246
- if (form.new_password !== form.confirmNewPassword) {
247
- throw new Error("Passwords do not match");
248
- }
249
- await authApi.updatePassword(form);
250
- eventEmitter.emit(AuthState.PASSWORD_RESET);
251
- } catch (error) {
252
- throw error;
253
- }
377
+ async function verifyResetToken(token) {
378
+ await api.verifyResetToken(token);
254
379
  }
255
- async function updateProfile(user) {
256
- try {
257
- const { data } = await authApi.updateUserProfile(user);
258
- currentUser.value = { ...currentUser.value, ...data };
259
- eventEmitter.emit(AuthState.PROFILE_UPDATE);
260
- } catch (error) {
261
- throw error;
380
+ async function resetPassword(token, newPassword) {
381
+ await api.resetPassword({ token, new_password: newPassword });
382
+ emitter.emit(AuthState.PASSWORD_RESET);
383
+ }
384
+ async function changePassword(form) {
385
+ if (form.new_password !== form.confirmNewPassword) {
386
+ throw new Error("Passwords do not match");
262
387
  }
388
+ await api.changePassword({
389
+ current_password: form.current_password,
390
+ new_password: form.new_password
391
+ });
392
+ emitter.emit(AuthState.PASSWORD_CHANGE);
263
393
  }
264
- async function toggleUserStatus(userId, isActive) {
265
- try {
266
- await authApi.setUserStatus(userId, isActive);
267
- } catch (error) {
268
- throw error;
394
+ async function updateProfile(updates) {
395
+ const { data } = await api.updateCurrentUser(updates);
396
+ currentUser.value = data;
397
+ emitter.emit(AuthState.PROFILE_UPDATE);
398
+ }
399
+ async function activateAccount(accountId) {
400
+ await api.activateAccount(accountId);
401
+ }
402
+ async function deactivateAccount(accountId) {
403
+ await api.deactivateAccount(accountId);
404
+ }
405
+ async function deleteAccount(accountId) {
406
+ await api.deleteAccount(accountId);
407
+ }
408
+ async function deleteCurrentUser() {
409
+ await api.deleteCurrentUser();
410
+ currentUser.value = null;
411
+ }
412
+ async function sendVerification(email) {
413
+ await api.sendVerification({ email });
414
+ }
415
+ async function verifyEmail(token) {
416
+ await api.verifyEmail(token);
417
+ await checkAuth();
418
+ emitter.emit(AuthState.EMAIL_VERIFIED);
419
+ }
420
+ async function refreshSession() {
421
+ await api.refreshSession();
422
+ emitter.emit(AuthState.SESSION_REFRESH);
423
+ }
424
+ async function getSessions(accountId) {
425
+ const id = accountId ?? currentUser.value?.id;
426
+ if (id === void 0 || id === "") {
427
+ throw new Error("No account ID available");
269
428
  }
429
+ return api.getSessions(id);
270
430
  }
271
- async function deleteUser(userId) {
272
- try {
273
- await authApi.deleteUser(userId);
274
- } catch (error) {
275
- throw error;
431
+ async function revokeSession(sessionToken) {
432
+ await api.revokeSession(sessionToken);
433
+ }
434
+ async function revokeAllSessions(accountId) {
435
+ const id = accountId ?? currentUser.value?.id;
436
+ if (id === void 0 || id === "") {
437
+ throw new Error("No account ID available");
276
438
  }
439
+ await api.revokeAllSessions(id);
277
440
  }
278
441
  return {
279
442
  // State
@@ -281,17 +444,32 @@ function useAuth() {
281
444
  // Getters
282
445
  getFullName,
283
446
  getIsLoggedIn,
284
- // Actions
285
- logout,
447
+ getEmail,
448
+ // Authentication Actions
286
449
  login,
287
- checkAuth,
450
+ logout,
288
451
  signup,
289
- recoverPassword,
290
- resetPassword,
291
- updatePassword,
452
+ checkAuth,
453
+ refreshSession,
454
+ // Profile Actions
292
455
  updateProfile,
293
- toggleUserStatus,
294
- deleteUser
456
+ deleteCurrentUser,
457
+ // Password Actions
458
+ changePassword,
459
+ forgotPassword,
460
+ verifyResetToken,
461
+ resetPassword,
462
+ // Email Verification Actions
463
+ sendVerification,
464
+ verifyEmail,
465
+ // Admin Actions
466
+ activateAccount,
467
+ deactivateAccount,
468
+ deleteAccount,
469
+ // Session Management
470
+ getSessions,
471
+ revokeSession,
472
+ revokeAllSessions
295
473
  };
296
474
  }
297
475
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/auth",
3
3
  "type": "module",
4
- "version": "1.4.167",
4
+ "version": "1.4.171",
5
5
  "description": "Bagelink auth package",
6
6
  "author": {
7
7
  "name": "Bagel Studio",