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