@bagelink/auth 1.1.41 → 1.1.45

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