@bagelink/auth 1.4.169 → 1.4.174
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/README.md +254 -110
- package/dist/index.cjs +388 -159
- package/dist/index.d.cts +357 -102
- package/dist/index.d.mts +357 -102
- package/dist/index.d.ts +357 -102
- package/dist/index.mjs +389 -161
- package/package.json +1 -1
- package/src/api.ts +230 -72
- package/src/types.ts +278 -71
- package/src/useAuth.ts +212 -110
package/src/api.ts
CHANGED
|
@@ -1,25 +1,34 @@
|
|
|
1
1
|
import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
|
|
2
2
|
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
SanitizedUserOut,
|
|
10
|
-
SanitizedUserList,
|
|
3
|
+
RegisterRequest,
|
|
4
|
+
UpdateAccountRequest,
|
|
5
|
+
ChangePasswordRequest,
|
|
6
|
+
ResetPasswordRequest,
|
|
7
|
+
SendVerificationRequest,
|
|
8
|
+
AuthenticationAccount,
|
|
11
9
|
LoginResponse,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
GetUserResponse,
|
|
15
|
-
UpdateUserResponse,
|
|
16
|
-
DeleteUserResponse,
|
|
17
|
-
GetUsersResponse,
|
|
18
|
-
CreateUserResponse,
|
|
10
|
+
RegisterResponse,
|
|
11
|
+
LogoutResponse,
|
|
19
12
|
GetMeResponse,
|
|
20
13
|
UpdateMeResponse,
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
DeleteMeResponse,
|
|
15
|
+
GetAccountResponse,
|
|
16
|
+
UpdateAccountResponse,
|
|
17
|
+
DeleteAccountResponse,
|
|
18
|
+
ActivateAccountResponse,
|
|
19
|
+
DeactivateAccountResponse,
|
|
20
|
+
ChangePasswordResponse,
|
|
21
|
+
ForgotPasswordResponse,
|
|
22
|
+
ResetPasswordResponse,
|
|
23
|
+
VerifyResetTokenResponse,
|
|
24
|
+
SendVerificationResponse,
|
|
25
|
+
VerifyEmailResponse,
|
|
26
|
+
RefreshSessionResponse,
|
|
27
|
+
GetSessionsResponse,
|
|
28
|
+
DeleteSessionResponse,
|
|
29
|
+
DeleteAllSessionsResponse,
|
|
30
|
+
CleanupSessionsResponse,
|
|
31
|
+
GetMethodsResponse,
|
|
23
32
|
} from './types'
|
|
24
33
|
import { createAxiosInstance } from './utils'
|
|
25
34
|
|
|
@@ -33,98 +42,247 @@ export class AuthApi {
|
|
|
33
42
|
|
|
34
43
|
private setupInterceptors() {
|
|
35
44
|
this.api.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
36
|
-
const
|
|
37
|
-
if (
|
|
38
|
-
config.headers.Authorization = `Bearer ${
|
|
45
|
+
const sessionToken = localStorage.getItem('session_token')
|
|
46
|
+
if (sessionToken !== null && config.headers) {
|
|
47
|
+
config.headers.Authorization = `Bearer ${sessionToken}`
|
|
39
48
|
}
|
|
40
49
|
|
|
50
|
+
// Handle password reset token from URL
|
|
41
51
|
const urlParams = new URLSearchParams(window.location.search)
|
|
42
52
|
const resetToken = urlParams.get('token')
|
|
43
|
-
|
|
44
53
|
if (resetToken !== null && config.headers) {
|
|
45
|
-
config.headers
|
|
54
|
+
config.headers['X-Reset-Token'] = resetToken
|
|
46
55
|
}
|
|
56
|
+
|
|
47
57
|
return config
|
|
48
58
|
})
|
|
49
59
|
}
|
|
50
60
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
61
|
+
// ============================================
|
|
62
|
+
// Authentication Methods
|
|
63
|
+
// ============================================
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get available authentication methods
|
|
67
|
+
*/
|
|
68
|
+
async getAuthMethods(): Promise<GetMethodsResponse> {
|
|
69
|
+
return this.api.get('/authentication/methods')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Register a new account
|
|
74
|
+
*/
|
|
75
|
+
async register(data: RegisterRequest): Promise<RegisterResponse> {
|
|
76
|
+
const response = await this.api.post('/authentication/register', {
|
|
77
|
+
...data,
|
|
78
|
+
email: data.email.toLowerCase(),
|
|
55
79
|
})
|
|
56
|
-
|
|
57
|
-
|
|
80
|
+
|
|
81
|
+
// Store session token if provided
|
|
82
|
+
if (response.data.session_token) {
|
|
83
|
+
localStorage.setItem('session_token', response.data.session_token)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return response
|
|
58
87
|
}
|
|
59
88
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Login with password
|
|
91
|
+
*/
|
|
92
|
+
async login(email: string, password: string): Promise<LoginResponse> {
|
|
93
|
+
const response = await this.api.post('/authentication/login/password', {
|
|
94
|
+
email: email.toLowerCase(),
|
|
95
|
+
password,
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
// Store session token if provided
|
|
99
|
+
if (response.data.session_token) {
|
|
100
|
+
localStorage.setItem('session_token', response.data.session_token)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return response
|
|
63
104
|
}
|
|
64
105
|
|
|
65
|
-
|
|
66
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Logout and clear session
|
|
108
|
+
*/
|
|
109
|
+
async logout(): Promise<LogoutResponse> {
|
|
110
|
+
const response = await this.api.post('/authentication/logout', {})
|
|
111
|
+
localStorage.removeItem('session_token')
|
|
112
|
+
return response
|
|
67
113
|
}
|
|
68
114
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Refresh current session
|
|
117
|
+
*/
|
|
118
|
+
async refreshSession(): Promise<RefreshSessionResponse> {
|
|
119
|
+
const response = await this.api.post('/authentication/refresh', {})
|
|
120
|
+
|
|
121
|
+
// Update session token if provided
|
|
122
|
+
if (response.data.session_token) {
|
|
123
|
+
localStorage.setItem('session_token', response.data.session_token)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return response
|
|
73
127
|
}
|
|
74
128
|
|
|
129
|
+
// ============================================
|
|
130
|
+
// Current User (Me) Methods
|
|
131
|
+
// ============================================
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Get current user account info
|
|
135
|
+
*/
|
|
75
136
|
async getCurrentUser(): Promise<GetMeResponse> {
|
|
76
|
-
return this.api.get
|
|
137
|
+
return this.api.get('/authentication/me')
|
|
77
138
|
}
|
|
78
139
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
last_name: user.last_name,
|
|
85
|
-
})
|
|
140
|
+
/**
|
|
141
|
+
* Update current user profile
|
|
142
|
+
*/
|
|
143
|
+
async updateCurrentUser(data: UpdateAccountRequest): Promise<UpdateMeResponse> {
|
|
144
|
+
return this.api.patch('/authentication/me', data)
|
|
86
145
|
}
|
|
87
146
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Delete current user account
|
|
149
|
+
*/
|
|
150
|
+
async deleteCurrentUser(): Promise<DeleteMeResponse> {
|
|
151
|
+
const response = await this.api.delete('/authentication/me')
|
|
152
|
+
localStorage.removeItem('session_token')
|
|
153
|
+
return response
|
|
95
154
|
}
|
|
96
155
|
|
|
97
|
-
|
|
98
|
-
|
|
156
|
+
// ============================================
|
|
157
|
+
// Account Management (Admin)
|
|
158
|
+
// ============================================
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Get account information by ID
|
|
162
|
+
*/
|
|
163
|
+
async getAccount(accountId: string): Promise<GetAccountResponse> {
|
|
164
|
+
return this.api.get(`/authentication/account/${accountId}`)
|
|
99
165
|
}
|
|
100
166
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
167
|
+
/**
|
|
168
|
+
* Update account by ID
|
|
169
|
+
*/
|
|
170
|
+
async updateAccount(
|
|
171
|
+
accountId: string,
|
|
172
|
+
data: UpdateAccountRequest
|
|
173
|
+
): Promise<UpdateAccountResponse> {
|
|
174
|
+
return this.api.patch(`/authentication/account/${accountId}`, data)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Delete account by ID
|
|
179
|
+
*/
|
|
180
|
+
async deleteAccount(accountId: string): Promise<DeleteAccountResponse> {
|
|
181
|
+
return this.api.delete(`/authentication/account/${accountId}`)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Activate account by ID
|
|
186
|
+
*/
|
|
187
|
+
async activateAccount(accountId: string): Promise<ActivateAccountResponse> {
|
|
188
|
+
return this.api.post(`/authentication/account/${accountId}/activate`, {})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Deactivate account by ID
|
|
193
|
+
*/
|
|
194
|
+
async deactivateAccount(
|
|
195
|
+
accountId: string
|
|
196
|
+
): Promise<DeactivateAccountResponse> {
|
|
197
|
+
return this.api.post(`/authentication/account/${accountId}/deactivate`, {})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ============================================
|
|
201
|
+
// Password Management
|
|
202
|
+
// ============================================
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Change password (requires current password)
|
|
206
|
+
*/
|
|
207
|
+
async changePassword(data: ChangePasswordRequest): Promise<ChangePasswordResponse> {
|
|
208
|
+
return this.api.post('/authentication/password/change', data)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Initiate forgot password flow
|
|
213
|
+
*/
|
|
214
|
+
async forgotPassword(email: string): Promise<ForgotPasswordResponse> {
|
|
215
|
+
return this.api.post('/authentication/password/forgot', {
|
|
216
|
+
email: email.toLowerCase(),
|
|
107
217
|
})
|
|
108
218
|
}
|
|
109
219
|
|
|
110
|
-
|
|
111
|
-
|
|
220
|
+
/**
|
|
221
|
+
* Verify password reset token
|
|
222
|
+
*/
|
|
223
|
+
async verifyResetToken(token: string): Promise<VerifyResetTokenResponse> {
|
|
224
|
+
return this.api.get(`/authentication/password/verify-reset-token/${token}`)
|
|
112
225
|
}
|
|
113
226
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
): Promise<
|
|
118
|
-
return this.api.
|
|
119
|
-
|
|
227
|
+
/**
|
|
228
|
+
* Reset password with token
|
|
229
|
+
*/
|
|
230
|
+
async resetPassword(data: ResetPasswordRequest): Promise<ResetPasswordResponse> {
|
|
231
|
+
return this.api.post('/authentication/password/reset', data)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// ============================================
|
|
235
|
+
// Email Verification
|
|
236
|
+
// ============================================
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Send email verification
|
|
240
|
+
*/
|
|
241
|
+
async sendVerification(
|
|
242
|
+
data: SendVerificationRequest = {},
|
|
243
|
+
user?: AuthenticationAccount
|
|
244
|
+
): Promise<SendVerificationResponse> {
|
|
245
|
+
return this.api.post('/authentication/verify/send', data, {
|
|
246
|
+
params: user ? { user } : undefined,
|
|
120
247
|
})
|
|
121
248
|
}
|
|
122
249
|
|
|
123
|
-
|
|
124
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Verify email with token
|
|
252
|
+
*/
|
|
253
|
+
async verifyEmail(token: string): Promise<VerifyEmailResponse> {
|
|
254
|
+
return this.api.post('/authentication/verify/email', { token })
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ============================================
|
|
258
|
+
// Session Management
|
|
259
|
+
// ============================================
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Get sessions for an account
|
|
263
|
+
*/
|
|
264
|
+
async getSessions(accountId: string): Promise<GetSessionsResponse> {
|
|
265
|
+
return this.api.get(`/authentication/sessions/${accountId}`)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Revoke a specific session
|
|
270
|
+
*/
|
|
271
|
+
async revokeSession(sessionToken: string): Promise<DeleteSessionResponse> {
|
|
272
|
+
return this.api.delete(`/authentication/sessions/${sessionToken}`)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Revoke all sessions for an account
|
|
277
|
+
*/
|
|
278
|
+
async revokeAllSessions(accountId: string): Promise<DeleteAllSessionsResponse> {
|
|
279
|
+
return this.api.delete(`/authentication/sessions/account/${accountId}`)
|
|
125
280
|
}
|
|
126
281
|
|
|
127
|
-
|
|
128
|
-
|
|
282
|
+
/**
|
|
283
|
+
* Cleanup expired sessions (admin)
|
|
284
|
+
*/
|
|
285
|
+
async cleanupSessions(): Promise<CleanupSessionsResponse> {
|
|
286
|
+
return this.api.post('/authentication/cleanup-sessions', {})
|
|
129
287
|
}
|
|
130
288
|
}
|