@bagelink/auth 1.1.41 → 1.1.43

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { AxiosResponse, AxiosInstance } from 'axios';
1
+ import { AxiosResponse } from 'axios';
2
+ import { App } from 'vue';
2
3
 
3
4
  interface User {
4
5
  id: string;
@@ -86,38 +87,33 @@ type UpdateMeResponse = AxiosResponse<SanitizedUserOut>;
86
87
  type UpdatePasswordResponse = AxiosResponse;
87
88
  type SignupResponse = AxiosResponse<SanitizedUserOut>;
88
89
 
89
- declare function login(username: string, password: string): Promise<LoginResponse>;
90
- declare function logout(): void;
91
- declare function passwordRecovery(email?: string): Promise<PasswordRecoveryResponse>;
92
- declare function resetPassword(newPassword: NewPassword['new_password']): Promise<ResetPasswordResponse>;
93
- declare function getCurrentUser(): Promise<GetMeResponse>;
94
- declare function signup(user: NewUser): Promise<SignupResponse>;
95
- declare function updatePassword(form: UpdatePasswordForm): Promise<UpdatePasswordResponse>;
96
- declare function updateUserProfile(user: Partial<User>): Promise<UpdateMeResponse>;
97
- declare function setUserStatus(userId: string, isActive: boolean): Promise<UpdateUserResponse>;
98
- declare function deleteUser(userId: string): Promise<DeleteUserResponse>;
99
- declare function getUsers(limit?: number, skip?: number): Promise<GetUsersResponse>;
100
- declare function createUser(user: UserCreate): Promise<CreateUserResponse>;
101
- declare function getUser(userId: string): Promise<GetUserResponse>;
90
+ declare class AuthApi {
91
+ private api;
92
+ constructor(baseURL?: string);
93
+ private setupInterceptors;
94
+ login(username: string, password: string): Promise<LoginResponse>;
95
+ logout(): void;
96
+ passwordRecovery(email?: string): Promise<PasswordRecoveryResponse>;
97
+ resetPassword(newPassword: NewPassword['new_password']): Promise<ResetPasswordResponse>;
98
+ getCurrentUser(): Promise<GetMeResponse>;
99
+ signup(user: NewUser): Promise<SignupResponse>;
100
+ updatePassword(form: UpdatePasswordForm): Promise<UpdatePasswordResponse>;
101
+ updateUserProfile(user: Partial<User>): Promise<UpdateMeResponse>;
102
+ setUserStatus(userId: string, isActive: boolean): Promise<UpdateUserResponse>;
103
+ deleteUser(userId: string): Promise<DeleteUserResponse>;
104
+ getUsers(limit?: number, skip?: number): Promise<GetUsersResponse>;
105
+ createUser(user: UserCreate): Promise<CreateUserResponse>;
106
+ getUser(userId: string): Promise<GetUserResponse>;
107
+ }
102
108
 
103
- declare function initAuth({ axios, errorHandler, reactive, baseURL }?: {
104
- axios?: AxiosInstance;
105
- errorHandler?: (error: any) => void;
106
- reactive?: ReactiveFactory;
107
- baseURL?: string;
108
- }): {
109
- install(app: any): void;
110
- useAuth: typeof useAuth;
109
+ declare function initAuth(baseURL?: string): {
110
+ install(app: App): void;
111
111
  };
112
112
  declare function useAuth(): {
113
113
  currentUser: {
114
114
  value: User;
115
115
  set: (newValue: User) => void;
116
116
  };
117
- passwordForm: {
118
- value: UpdatePasswordForm;
119
- set: (newValue: UpdatePasswordForm) => void;
120
- };
121
117
  getFullName: () => string;
122
118
  getIsLoggedIn: () => boolean;
123
119
  logout: () => Promise<void>;
@@ -128,11 +124,11 @@ declare function useAuth(): {
128
124
  checkAuth: () => Promise<boolean>;
129
125
  signup: (user: NewUser) => Promise<void>;
130
126
  recoverPassword: (email: string) => Promise<void>;
131
- resetPassword: (form: UpdatePasswordForm) => Promise<void>;
132
- updatePassword: () => Promise<void>;
127
+ resetPassword: (newPassword: string) => Promise<void>;
128
+ updatePassword: (form: UpdatePasswordForm) => Promise<void>;
133
129
  updateProfile: (user: Partial<User>) => Promise<void>;
134
130
  toggleUserStatus: (userId: string, isActive: boolean) => Promise<void>;
135
131
  deleteUser: (userId: string) => Promise<void>;
136
132
  };
137
133
 
138
- export { type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ReactiveFactory, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, createUser, deleteUser, getCurrentUser, getUser, getUsers, initAuth, login, logout, passwordRecovery, resetPassword, setUserStatus, signup, updatePassword, updateUserProfile, useAuth };
134
+ export { AuthApi, type CreateUserResponse, type DeleteUserResponse, type GetMeResponse, type GetUserResponse, type GetUsersResponse, type LoginResponse, type NewPassword, type NewUser, type PasswordRecovery, type PasswordRecoveryResponse, type ReactiveFactory, type ResetPasswordResponse, type SanitizedUserList, type SanitizedUserOut, type SignupResponse, type Token, type UpdateMeResponse, type UpdatePassword, type UpdatePasswordForm, type UpdatePasswordResponse, type UpdateUserResponse, type User, type UserCreate, type UserRegister, type UserUpdate, type UserUpdateMe, initAuth, useAuth };
package/dist/index.mjs CHANGED
@@ -1,180 +1,140 @@
1
1
  import axios from 'axios';
2
2
 
3
- const api$1 = void 0;
4
- function getApi() {
5
- if (!api$1) {
6
- throw new Error("API not initialized. Call initAuth first.");
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField = (obj, key, value) => {
6
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ return value;
8
+ };
9
+ class AuthApi {
10
+ constructor(baseURL) {
11
+ __publicField(this, "api");
12
+ this.api = axios.create({
13
+ baseURL,
14
+ headers: {
15
+ "Content-Type": "application/json"
16
+ }
17
+ });
18
+ this.setupInterceptors();
7
19
  }
8
- return api$1;
9
- }
10
- function createDefaultApi(baseURL) {
11
- return axios.create({
12
- baseURL,
13
- headers: {
14
- "Content-Type": "application/json"
15
- }
16
- });
17
- }
18
-
19
- const ax = getApi();
20
- ax.interceptors.request.use((config) => {
21
- const token = localStorage.getItem("access_token");
22
- if (token !== null && config.headers) {
23
- config.headers.Authorization = `Bearer ${token}`;
20
+ setupInterceptors() {
21
+ this.api.interceptors.request.use((config) => {
22
+ const token = localStorage.getItem("access_token");
23
+ if (token !== null && config.headers) {
24
+ config.headers.Authorization = `Bearer ${token}`;
25
+ }
26
+ const urlParams = new URLSearchParams(window.location.search);
27
+ const resetToken = urlParams.get("token");
28
+ if (resetToken !== null && config.headers) {
29
+ config.headers.Authorization = `Bearer ${resetToken}`;
30
+ }
31
+ return config;
32
+ });
24
33
  }
25
- const urlParams = new URLSearchParams(window.location.search);
26
- const resetToken = urlParams.get("token");
27
- if (resetToken !== null && config.headers) {
28
- config.headers.Authorization = `Bearer ${resetToken}`;
34
+ async login(username, password) {
35
+ const { data } = await this.api.post("/auth/login", {
36
+ username: username.toLowerCase(),
37
+ password
38
+ });
39
+ localStorage.setItem("access_token", data.access_token);
40
+ return { data };
41
+ }
42
+ logout() {
43
+ localStorage.removeItem("access_token");
44
+ window.location.reload();
45
+ }
46
+ async passwordRecovery(email) {
47
+ return this.api.post("/auth/password-recovery", { email });
48
+ }
49
+ async resetPassword(newPassword) {
50
+ return this.api.post("/auth/reset-password", { new_password: newPassword });
51
+ }
52
+ async getCurrentUser() {
53
+ return this.api.get("/users/me");
54
+ }
55
+ async signup(user) {
56
+ return this.api.post("/users/signup", {
57
+ email: user.email.toLowerCase(),
58
+ password: user.password,
59
+ first_name: user.first_name,
60
+ last_name: user.last_name
61
+ });
62
+ }
63
+ async updatePassword(form) {
64
+ return this.api.patch("/users/me/password", {
65
+ current_password: form.current_password,
66
+ new_password: form.new_password
67
+ });
68
+ }
69
+ async updateUserProfile(user) {
70
+ return this.api.patch("/users/me", user);
71
+ }
72
+ async setUserStatus(userId, isActive) {
73
+ return this.api.patch(`/users/${userId}`, { is_active: isActive });
74
+ }
75
+ async deleteUser(userId) {
76
+ return this.api.delete(`/users/${userId}`);
77
+ }
78
+ async getUsers(limit = 100, skip) {
79
+ return this.api.get("/users/", { params: { skip, limit } });
80
+ }
81
+ async createUser(user) {
82
+ return this.api.post("/users/", user);
83
+ }
84
+ async getUser(userId) {
85
+ return this.api.get(`/users/${userId}`);
29
86
  }
30
- return config;
31
- });
32
- async function login(username, password) {
33
- const { data } = await ax.post("/auth/login", {
34
- username: username.toLowerCase(),
35
- password
36
- });
37
- localStorage.setItem("access_token", data.access_token);
38
- return { data };
39
- }
40
- function logout() {
41
- localStorage.removeItem("access_token");
42
- window.location.reload();
43
- }
44
- async function passwordRecovery(email) {
45
- return ax.post("/auth/password-recovery", { email });
46
- }
47
- async function resetPassword(newPassword) {
48
- return ax.post("/auth/reset-password", { new_password: newPassword });
49
- }
50
- async function getCurrentUser() {
51
- return ax.get("/users/me");
52
- }
53
- async function signup(user) {
54
- return ax.post("/users/signup", {
55
- email: user.email.toLowerCase(),
56
- password: user.password,
57
- first_name: user.first_name,
58
- last_name: user.last_name
59
- });
60
- }
61
- async function updatePassword(form) {
62
- return ax.patch("/users/me/password", {
63
- current_password: form.current_password,
64
- new_password: form.new_password
65
- });
66
- }
67
- async function updateUserProfile(user) {
68
- return ax.patch("/users/me", user);
69
- }
70
- async function setUserStatus(userId, isActive) {
71
- return ax.patch(`/users/${userId}`, { is_active: isActive });
72
- }
73
- async function deleteUser(userId) {
74
- return ax.delete(`/users/${userId}`);
75
- }
76
- async function getUsers(limit = 100, skip) {
77
- return ax.get("/users/", { params: { skip, limit } });
78
- }
79
- async function createUser(user) {
80
- return ax.post("/users/", user);
81
- }
82
- async function getUser(userId) {
83
- return ax.get(`/users/${userId}`);
84
87
  }
85
88
 
86
- let api;
87
- let onError;
88
- let createRef;
89
- const defaultReactiveFactory = (initial) => {
90
- let value = initial;
91
- return {
92
- get value() {
93
- return value;
94
- },
95
- set: (newValue) => {
96
- value = newValue;
97
- }
98
- };
89
+ let authApi = null;
90
+ const currentUser = {
91
+ value: {
92
+ id: "",
93
+ email: "",
94
+ first_name: "",
95
+ last_name: "",
96
+ is_superuser: false,
97
+ is_active: false
98
+ },
99
+ set: (newValue) => {
100
+ currentUser.value = newValue;
101
+ }
99
102
  };
100
- function createVueReactiveFactory(app) {
101
- return (initial) => {
102
- const refValue = app.ref(initial);
103
- return {
104
- get value() {
105
- return refValue.value;
106
- },
107
- set: (v) => {
108
- refValue.value = v;
109
- }
110
- };
111
- };
112
- }
113
- function initAuth({
114
- axios,
115
- errorHandler,
116
- reactive = defaultReactiveFactory,
117
- baseURL
118
- } = {}) {
119
- api = axios || createDefaultApi(baseURL);
120
- if (errorHandler)
121
- onError = errorHandler;
122
- createRef = reactive;
103
+ function initAuth(baseURL) {
104
+ if (!authApi) {
105
+ authApi = new AuthApi(baseURL);
106
+ }
123
107
  return {
124
108
  install(app) {
125
- if (app.ref) {
126
- createRef = createVueReactiveFactory(app);
127
- }
128
- },
129
- useAuth
109
+ app.config.globalProperties.$auth = useAuth();
110
+ }
130
111
  };
131
112
  }
132
113
  function useAuth() {
133
- if (!api || !createRef) {
134
- throw new Error("Auth composable not initialized. Call initAuth first.");
114
+ if (!authApi) {
115
+ throw new Error("Auth not initialized. Call initAuth first.");
135
116
  }
136
- const currentUser = createRef({
137
- id: "",
138
- email: "",
139
- first_name: "",
140
- last_name: "",
141
- is_superuser: false,
142
- is_active: false
143
- });
144
- const passwordForm = createRef({
145
- current_password: "",
146
- new_password: "",
147
- confirmNewPassword: ""
148
- });
149
117
  const getFullName = () => `${currentUser.value.first_name} ${currentUser.value.last_name}`;
150
118
  const getIsLoggedIn = () => currentUser.value.id.length > 0;
151
- function handleError(error) {
152
- if (onError) {
153
- onError(error);
154
- }
155
- throw error;
156
- }
157
- async function logout$1() {
119
+ async function logout() {
158
120
  try {
159
- await logout();
121
+ await authApi.logout();
160
122
  } catch (error) {
161
- handleError(error);
123
+ throw error;
162
124
  }
163
125
  }
164
- async function login$1(credentials) {
165
- const email = credentials.email.toLowerCase();
166
- const { password } = credentials;
126
+ async function login(credentials) {
167
127
  try {
168
- await login(email, password);
128
+ await authApi.login(credentials.email.toLowerCase(), credentials.password);
169
129
  await checkAuth();
170
130
  } catch (error) {
171
- handleError(error);
131
+ throw error;
172
132
  }
173
133
  }
174
134
  async function checkAuth() {
175
135
  try {
176
136
  if (!getIsLoggedIn()) {
177
- const { data } = await getCurrentUser();
137
+ const { data } = await authApi.getCurrentUser();
178
138
  currentUser.set(data);
179
139
  }
180
140
  } catch (error) {
@@ -182,95 +142,81 @@ function useAuth() {
182
142
  }
183
143
  return getIsLoggedIn();
184
144
  }
185
- async function signup$1(user) {
145
+ async function signup(user) {
186
146
  try {
187
147
  if (user.password !== user.confirmPassword) {
188
148
  throw new Error("Passwords do not match");
189
149
  }
190
- const { data } = await signup(user);
150
+ const { data } = await authApi.signup(user);
191
151
  currentUser.set(data);
192
152
  } catch (error) {
193
- handleError(error);
153
+ throw error;
194
154
  }
195
155
  }
196
156
  async function recoverPassword(email) {
197
157
  try {
198
- await passwordRecovery(email);
158
+ await authApi.passwordRecovery(email);
199
159
  } catch (error) {
200
- handleError(error);
160
+ throw error;
201
161
  }
202
162
  }
203
- async function resetPassword$1(form) {
163
+ async function resetPassword(newPassword) {
204
164
  try {
205
- if (form.new_password !== form.confirmNewPassword) {
206
- throw new Error("Passwords do not match");
207
- }
208
- await resetPassword(form.new_password);
209
- form = {
210
- current_password: "",
211
- new_password: "",
212
- confirmNewPassword: ""
213
- };
165
+ await authApi.resetPassword(newPassword);
214
166
  } catch (error) {
215
- handleError(error);
167
+ throw error;
216
168
  }
217
169
  }
218
- async function updatePassword$1() {
170
+ async function updatePassword(form) {
219
171
  try {
220
- if (passwordForm.value.new_password !== passwordForm.value.confirmNewPassword) {
172
+ if (form.new_password !== form.confirmNewPassword) {
221
173
  throw new Error("Passwords do not match");
222
174
  }
223
- await updatePassword(passwordForm.value);
224
- passwordForm.set({
225
- current_password: "",
226
- new_password: "",
227
- confirmNewPassword: ""
228
- });
175
+ await authApi.updatePassword(form);
229
176
  } catch (error) {
230
- handleError(error);
177
+ throw error;
231
178
  }
232
179
  }
233
180
  async function updateProfile(user) {
234
181
  try {
235
- const { data } = await updateUserProfile(user);
182
+ const { data } = await authApi.updateUserProfile(user);
236
183
  currentUser.set({ ...currentUser.value, ...data });
237
184
  } catch (error) {
238
- handleError(error);
185
+ throw error;
239
186
  }
240
187
  }
241
188
  async function toggleUserStatus(userId, isActive) {
242
189
  try {
243
- await setUserStatus(userId, isActive);
190
+ await authApi.setUserStatus(userId, isActive);
244
191
  } catch (error) {
245
- handleError(error);
192
+ throw error;
246
193
  }
247
194
  }
248
- async function deleteUser$1(userId) {
195
+ async function deleteUser(userId) {
249
196
  try {
250
- await deleteUser(userId);
197
+ await authApi.deleteUser(userId);
251
198
  } catch (error) {
252
- handleError(error);
199
+ throw error;
253
200
  }
254
201
  }
255
202
  return {
256
203
  // State
257
204
  currentUser,
258
- passwordForm,
259
205
  // Getters
260
206
  getFullName,
261
207
  getIsLoggedIn,
262
208
  // Actions
263
- logout: logout$1,
264
- login: login$1,
209
+ logout,
210
+ login,
265
211
  checkAuth,
266
- signup: signup$1,
212
+ signup,
267
213
  recoverPassword,
268
- resetPassword: resetPassword$1,
269
- updatePassword: updatePassword$1,
214
+ resetPassword,
215
+ updatePassword,
270
216
  updateProfile,
271
217
  toggleUserStatus,
272
- deleteUser: deleteUser$1
218
+ deleteUser
273
219
  };
274
220
  }
275
221
 
276
- export { createUser, deleteUser, getCurrentUser, getUser, getUsers, initAuth, login, logout, passwordRecovery, resetPassword, setUserStatus, signup, updatePassword, updateUserProfile, useAuth };
222
+ export { AuthApi, initAuth, useAuth };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/auth",
3
3
  "type": "module",
4
- "version": "1.1.41",
4
+ "version": "1.1.43",
5
5
  "description": "Bagelink auth package",
6
6
  "author": {
7
7
  "name": "Bagel Studio",
@@ -41,12 +41,22 @@
41
41
  "devDependencies": {
42
42
  "@types/node": "^20.0.0",
43
43
  "typescript": "^5.0.0",
44
- "unbuild": "^2.0.0"
44
+ "unbuild": "^2.0.0",
45
+ "@typescript-eslint/eslint-plugin": "^7.0.1",
46
+ "@typescript-eslint/parser": "^7.0.1",
47
+ "eslint": "^8.56.0",
48
+ "rimraf": "^5.0.5",
49
+ "tsup": "^8.0.2"
50
+ },
51
+ "peerDependencies": {
52
+ "vue": "^3.0.0"
45
53
  },
46
54
  "scripts": {
47
55
  "dev": "unbuild --stub",
48
56
  "build": "unbuild",
49
57
  "start": "tsx src/index.ts",
50
- "watch": "tsx watch src/index.ts"
58
+ "watch": "tsx watch src/index.ts",
59
+ "lint": "eslint . --ext .ts",
60
+ "clean": "rimraf dist"
51
61
  }
52
62
  }