@thetechfossil/auth2 1.2.1 → 1.2.3
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.NEW.md +365 -0
- package/dist/index.components.d.ts +112 -2
- package/dist/index.components.js +1996 -20
- package/dist/index.components.js.map +1 -1
- package/dist/index.components.mjs +1987 -22
- package/dist/index.components.mjs.map +1 -1
- package/dist/index.d.ts +252 -13
- package/dist/index.js +2393 -132
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2377 -134
- package/dist/index.mjs.map +1 -1
- package/dist/index.next.d.ts +219 -25
- package/dist/index.next.js +2362 -262
- package/dist/index.next.js.map +1 -1
- package/dist/index.next.mjs +2350 -262
- package/dist/index.next.mjs.map +1 -1
- package/dist/index.next.server.d.ts +107 -2
- package/dist/index.next.server.js +329 -20
- package/dist/index.next.server.js.map +1 -1
- package/dist/index.next.server.mjs +322 -21
- package/dist/index.next.server.mjs.map +1 -1
- package/dist/index.node.d.ts +70 -2
- package/dist/index.node.js +184 -18
- package/dist/index.node.js.map +1 -1
- package/dist/index.node.mjs +184 -18
- package/dist/index.node.mjs.map +1 -1
- package/next/index.js +2 -0
- package/next/index.mjs +2 -0
- package/next/package.json +5 -0
- package/next/server/package.json +5 -0
- package/next/server.js +2 -0
- package/next/server.mjs +2 -0
- package/package.json +34 -10
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
|
|
1
3
|
interface LinkedAccount {
|
|
2
4
|
provider: 'google' | 'github';
|
|
3
5
|
providerId: string;
|
|
@@ -33,7 +35,6 @@ interface RegisterData {
|
|
|
33
35
|
name: string;
|
|
34
36
|
email: string;
|
|
35
37
|
password: string;
|
|
36
|
-
frontendBaseUrl?: string;
|
|
37
38
|
}
|
|
38
39
|
interface UpdateUserData {
|
|
39
40
|
name: string;
|
|
@@ -53,6 +54,30 @@ interface AuthConfig {
|
|
|
53
54
|
token?: string;
|
|
54
55
|
csrfEnabled?: boolean;
|
|
55
56
|
}
|
|
57
|
+
interface Session {
|
|
58
|
+
id: string;
|
|
59
|
+
userId?: string;
|
|
60
|
+
token?: string;
|
|
61
|
+
userAgent?: string;
|
|
62
|
+
ipAddress?: string;
|
|
63
|
+
expiresAt: string;
|
|
64
|
+
createdAt: string;
|
|
65
|
+
updatedAt?: string;
|
|
66
|
+
}
|
|
67
|
+
interface MFASetup {
|
|
68
|
+
success: boolean;
|
|
69
|
+
qrCode?: string;
|
|
70
|
+
secret?: string;
|
|
71
|
+
message: string;
|
|
72
|
+
}
|
|
73
|
+
interface AuditLog {
|
|
74
|
+
id: string;
|
|
75
|
+
userId: string;
|
|
76
|
+
action: string;
|
|
77
|
+
details?: any;
|
|
78
|
+
ipAddress?: string;
|
|
79
|
+
timestamp: string;
|
|
80
|
+
}
|
|
56
81
|
interface UseAuthReturn {
|
|
57
82
|
user: User | null;
|
|
58
83
|
isAuthenticated: boolean;
|
|
@@ -71,6 +96,20 @@ interface UseAuthReturn {
|
|
|
71
96
|
unlinkOAuthProvider: (provider: OAuthProvider) => Promise<AuthResponse>;
|
|
72
97
|
csrfToken: string | null;
|
|
73
98
|
refreshCsrfToken: () => Promise<void>;
|
|
99
|
+
changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
|
|
100
|
+
updateAvatar: (avatar: string) => Promise<AuthResponse>;
|
|
101
|
+
requestEmailChange: (newEmail: string) => Promise<AuthResponse>;
|
|
102
|
+
verifyEmailChange: (token: string) => Promise<AuthResponse>;
|
|
103
|
+
generate2FA: () => Promise<MFASetup>;
|
|
104
|
+
enable2FA: (token: string) => Promise<AuthResponse>;
|
|
105
|
+
disable2FA: (token: string) => Promise<AuthResponse>;
|
|
106
|
+
validate2FA: (token: string) => Promise<AuthResponse>;
|
|
107
|
+
getSessions: () => Promise<{
|
|
108
|
+
success: boolean;
|
|
109
|
+
sessions: Session[];
|
|
110
|
+
}>;
|
|
111
|
+
revokeSession: (sessionId: string) => Promise<AuthResponse>;
|
|
112
|
+
revokeAllSessions: () => Promise<AuthResponse>;
|
|
74
113
|
}
|
|
75
114
|
|
|
76
115
|
declare class AuthService {
|
|
@@ -100,11 +139,39 @@ declare class AuthService {
|
|
|
100
139
|
getUserById(id: string): Promise<User>;
|
|
101
140
|
forgotPassword(email: string): Promise<AuthResponse>;
|
|
102
141
|
resetPassword(token: string, password: string): Promise<AuthResponse>;
|
|
142
|
+
changePassword(oldPassword: string, newPassword: string): Promise<AuthResponse>;
|
|
143
|
+
updateAvatar(avatar: string): Promise<AuthResponse>;
|
|
144
|
+
requestEmailChange(newEmail: string): Promise<AuthResponse>;
|
|
145
|
+
verifyEmailChange(token: string): Promise<AuthResponse>;
|
|
146
|
+
generate2FA(): Promise<{
|
|
147
|
+
success: boolean;
|
|
148
|
+
qrCode?: string;
|
|
149
|
+
secret?: string;
|
|
150
|
+
message: string;
|
|
151
|
+
}>;
|
|
152
|
+
enable2FA(token: string): Promise<AuthResponse>;
|
|
153
|
+
disable2FA(token: string): Promise<AuthResponse>;
|
|
154
|
+
validate2FA(token: string): Promise<AuthResponse>;
|
|
155
|
+
getSessions(): Promise<{
|
|
156
|
+
success: boolean;
|
|
157
|
+
sessions: any[];
|
|
158
|
+
}>;
|
|
159
|
+
revokeSession(sessionId: string): Promise<AuthResponse>;
|
|
160
|
+
revokeAllSessions(): Promise<AuthResponse>;
|
|
161
|
+
getAuditLogs(filters?: any): Promise<{
|
|
162
|
+
success: boolean;
|
|
163
|
+
logs: any[];
|
|
164
|
+
}>;
|
|
165
|
+
adminVerifyUser(userId: string): Promise<AuthResponse>;
|
|
166
|
+
adminForcePasswordReset(userId: string): Promise<AuthResponse>;
|
|
167
|
+
adminSuspendUser(userId: string): Promise<AuthResponse>;
|
|
168
|
+
adminActivateUser(userId: string): Promise<AuthResponse>;
|
|
103
169
|
}
|
|
104
170
|
|
|
105
171
|
declare class HttpClient {
|
|
106
172
|
private axiosInstance;
|
|
107
173
|
private csrfToken;
|
|
174
|
+
private frontendBaseUrl;
|
|
108
175
|
constructor(baseUrl: string, defaultHeaders?: Record<string, string>);
|
|
109
176
|
get<T>(endpoint: string, headers?: Record<string, string>): Promise<T>;
|
|
110
177
|
post<T>(endpoint: string, data?: any, headers?: Record<string, string>): Promise<T>;
|
|
@@ -115,6 +182,9 @@ declare class HttpClient {
|
|
|
115
182
|
setCsrfToken(token: string): void;
|
|
116
183
|
getCsrfToken(): string | null;
|
|
117
184
|
removeCsrfToken(): void;
|
|
185
|
+
setFrontendBaseUrl(url: string): void;
|
|
186
|
+
getFrontendBaseUrl(): string | null;
|
|
187
|
+
removeFrontendBaseUrl(): void;
|
|
118
188
|
private refreshCsrfToken;
|
|
119
189
|
}
|
|
120
190
|
|
|
@@ -139,4 +209,39 @@ declare class NextServerAuth extends AuthClient {
|
|
|
139
209
|
static createAuthenticatedClient(config: AuthConfig, token: string): NextServerAuth;
|
|
140
210
|
}
|
|
141
211
|
|
|
142
|
-
|
|
212
|
+
interface AuthServerConfig {
|
|
213
|
+
authApiUrl?: string;
|
|
214
|
+
tokenCookieName?: string;
|
|
215
|
+
}
|
|
216
|
+
declare class AuthServer {
|
|
217
|
+
private config;
|
|
218
|
+
constructor(config?: AuthServerConfig);
|
|
219
|
+
getToken(): Promise<string | null>;
|
|
220
|
+
getCurrentUser(): Promise<User | null>;
|
|
221
|
+
isAuthenticated(): Promise<boolean>;
|
|
222
|
+
requireAuth(redirectTo?: string): Promise<User>;
|
|
223
|
+
redirectIfAuthenticated(redirectTo?: string): Promise<void>;
|
|
224
|
+
getProfile(): Promise<User | null>;
|
|
225
|
+
}
|
|
226
|
+
declare function getAuthServer(config?: AuthServerConfig): AuthServer;
|
|
227
|
+
declare function currentUser(): Promise<User | null>;
|
|
228
|
+
declare function auth(): Promise<{
|
|
229
|
+
user: User | null;
|
|
230
|
+
userId: string | null;
|
|
231
|
+
isAuthenticated: boolean;
|
|
232
|
+
token: string | null;
|
|
233
|
+
}>;
|
|
234
|
+
declare function requireAuth(redirectTo?: string): Promise<User>;
|
|
235
|
+
declare function redirectIfAuthenticated(redirectTo?: string): Promise<void>;
|
|
236
|
+
|
|
237
|
+
interface AuthMiddlewareConfig {
|
|
238
|
+
publicRoutes?: string[];
|
|
239
|
+
protectedRoutes?: string[];
|
|
240
|
+
loginUrl?: string;
|
|
241
|
+
afterLoginUrl?: string;
|
|
242
|
+
tokenCookieName?: string;
|
|
243
|
+
}
|
|
244
|
+
declare function authMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => NextResponse<unknown>;
|
|
245
|
+
declare function createAuthMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => NextResponse<unknown>;
|
|
246
|
+
|
|
247
|
+
export { AuditLog, AuthClient, AuthConfig, AuthResponse, AuthServer, AuthService, CsrfTokenResponse, HttpClient, LinkedAccount, LoginData, MFASetup, NextServerAuth, OAuthConfig, OAuthProvider, RegisterData, Session, UpdateUserData, UseAuthReturn, User, VerifyData, auth, authMiddleware, createAuthMiddleware, currentUser, getAuthServer, redirectIfAuthenticated, requireAuth };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var axios = require('axios');
|
|
4
|
+
var headers = require('next/headers');
|
|
5
|
+
var navigation = require('next/navigation');
|
|
6
|
+
var server = require('next/server');
|
|
4
7
|
|
|
5
8
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
9
|
|
|
@@ -10,6 +13,7 @@ var axios__default = /*#__PURE__*/_interopDefault(axios);
|
|
|
10
13
|
var HttpClient = class {
|
|
11
14
|
constructor(baseUrl, defaultHeaders = {}) {
|
|
12
15
|
this.csrfToken = null;
|
|
16
|
+
this.frontendBaseUrl = null;
|
|
13
17
|
this.axiosInstance = axios__default.default.create({
|
|
14
18
|
baseURL: baseUrl.replace(/\/$/, ""),
|
|
15
19
|
headers: {
|
|
@@ -26,6 +30,9 @@ var HttpClient = class {
|
|
|
26
30
|
if (this.csrfToken && ["post", "put", "delete", "patch"].includes(config.method?.toLowerCase() || "")) {
|
|
27
31
|
config.headers["x-csrf-token"] = this.csrfToken;
|
|
28
32
|
}
|
|
33
|
+
if (this.frontendBaseUrl) {
|
|
34
|
+
config.headers["X-Frontend-URL"] = this.frontendBaseUrl;
|
|
35
|
+
}
|
|
29
36
|
return config;
|
|
30
37
|
},
|
|
31
38
|
(error) => Promise.reject(error)
|
|
@@ -81,6 +88,15 @@ var HttpClient = class {
|
|
|
81
88
|
removeCsrfToken() {
|
|
82
89
|
this.csrfToken = null;
|
|
83
90
|
}
|
|
91
|
+
setFrontendBaseUrl(url) {
|
|
92
|
+
this.frontendBaseUrl = url;
|
|
93
|
+
}
|
|
94
|
+
getFrontendBaseUrl() {
|
|
95
|
+
return this.frontendBaseUrl;
|
|
96
|
+
}
|
|
97
|
+
removeFrontendBaseUrl() {
|
|
98
|
+
this.frontendBaseUrl = null;
|
|
99
|
+
}
|
|
84
100
|
async refreshCsrfToken() {
|
|
85
101
|
try {
|
|
86
102
|
const response = await this.axiosInstance.get("/api/v1/auth/csrf-token");
|
|
@@ -103,6 +119,12 @@ var AuthService = class {
|
|
|
103
119
|
};
|
|
104
120
|
this.httpClient = new HttpClient(this.config.baseUrl);
|
|
105
121
|
this.loadTokenFromStorage();
|
|
122
|
+
if (typeof window !== "undefined") {
|
|
123
|
+
const frontendBaseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
|
|
124
|
+
if (frontendBaseUrl) {
|
|
125
|
+
this.httpClient.setFrontendBaseUrl(frontendBaseUrl);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
106
128
|
if (this.config.csrfEnabled && typeof window !== "undefined") {
|
|
107
129
|
this.refreshCsrfToken();
|
|
108
130
|
}
|
|
@@ -219,11 +241,7 @@ var AuthService = class {
|
|
|
219
241
|
throw new Error(response.message || "Login failed");
|
|
220
242
|
}
|
|
221
243
|
async register(data) {
|
|
222
|
-
const
|
|
223
|
-
if (!registerData.frontendBaseUrl && typeof window !== "undefined") {
|
|
224
|
-
registerData.frontendBaseUrl = process.env.FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
|
|
225
|
-
}
|
|
226
|
-
const response = await this.httpClient.post("/api/v1/auth/register", registerData);
|
|
244
|
+
const response = await this.httpClient.post("/api/v1/auth/register", data);
|
|
227
245
|
if (response.success && response.message === "Registration data saved. Verification email sent. Please check your inbox.") {
|
|
228
246
|
return response;
|
|
229
247
|
}
|
|
@@ -239,15 +257,33 @@ var AuthService = class {
|
|
|
239
257
|
return response;
|
|
240
258
|
}
|
|
241
259
|
async verifyEmailToken(token) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
260
|
+
try {
|
|
261
|
+
const response = await this.httpClient.get(`/api/v1/auth/verify-email?token=${token}`);
|
|
262
|
+
if (response.success && response.token) {
|
|
263
|
+
this.token = response.token;
|
|
264
|
+
this.httpClient.setAuthToken(response.token);
|
|
265
|
+
this.saveTokenToStorage(response.token);
|
|
266
|
+
}
|
|
267
|
+
return response;
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (error.response?.data) {
|
|
270
|
+
return {
|
|
271
|
+
success: false,
|
|
272
|
+
message: error.response.data.message || "Email verification failed"
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
success: false,
|
|
277
|
+
message: error.message || "Network error occurred"
|
|
278
|
+
};
|
|
247
279
|
}
|
|
248
|
-
return response;
|
|
249
280
|
}
|
|
250
281
|
async logout() {
|
|
282
|
+
try {
|
|
283
|
+
await this.httpClient.post("/api/v1/auth/logout", {});
|
|
284
|
+
} catch (error) {
|
|
285
|
+
console.warn("Failed to call logout endpoint:", error);
|
|
286
|
+
}
|
|
251
287
|
this.token = null;
|
|
252
288
|
this.httpClient.removeAuthToken();
|
|
253
289
|
this.httpClient.removeCsrfToken();
|
|
@@ -284,9 +320,6 @@ var AuthService = class {
|
|
|
284
320
|
return response.user;
|
|
285
321
|
}
|
|
286
322
|
async forgotPassword(email) {
|
|
287
|
-
if (typeof window !== "undefined") {
|
|
288
|
-
process.env.FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
|
|
289
|
-
}
|
|
290
323
|
const response = await this.httpClient.post("/api/v1/auth/forgot-password", { email });
|
|
291
324
|
return response;
|
|
292
325
|
}
|
|
@@ -294,6 +327,142 @@ var AuthService = class {
|
|
|
294
327
|
const response = await this.httpClient.post("/api/v1/auth/reset-password", { token, password });
|
|
295
328
|
return response;
|
|
296
329
|
}
|
|
330
|
+
async changePassword(oldPassword, newPassword) {
|
|
331
|
+
if (!this.token) {
|
|
332
|
+
throw new Error("Not authenticated");
|
|
333
|
+
}
|
|
334
|
+
const response = await this.httpClient.post("/api/v1/user/change-password", {
|
|
335
|
+
oldPassword,
|
|
336
|
+
newPassword
|
|
337
|
+
});
|
|
338
|
+
return response;
|
|
339
|
+
}
|
|
340
|
+
async updateAvatar(avatar) {
|
|
341
|
+
if (!this.token) {
|
|
342
|
+
throw new Error("Not authenticated");
|
|
343
|
+
}
|
|
344
|
+
const response = await this.httpClient.post("/api/v1/user/update/avatar", { avatar });
|
|
345
|
+
if (response.success && response.token) {
|
|
346
|
+
this.token = response.token;
|
|
347
|
+
this.httpClient.setAuthToken(response.token);
|
|
348
|
+
this.saveTokenToStorage(response.token);
|
|
349
|
+
}
|
|
350
|
+
return response;
|
|
351
|
+
}
|
|
352
|
+
async requestEmailChange(newEmail) {
|
|
353
|
+
if (!this.token) {
|
|
354
|
+
throw new Error("Not authenticated");
|
|
355
|
+
}
|
|
356
|
+
const response = await this.httpClient.post("/api/v1/user/request-email-change", {
|
|
357
|
+
newEmail
|
|
358
|
+
});
|
|
359
|
+
return response;
|
|
360
|
+
}
|
|
361
|
+
async verifyEmailChange(token) {
|
|
362
|
+
const response = await this.httpClient.get(`/api/v1/user/verify-email-change?token=${token}`);
|
|
363
|
+
if (response.success && response.token) {
|
|
364
|
+
this.token = response.token;
|
|
365
|
+
this.httpClient.setAuthToken(response.token);
|
|
366
|
+
this.saveTokenToStorage(response.token);
|
|
367
|
+
}
|
|
368
|
+
return response;
|
|
369
|
+
}
|
|
370
|
+
// 2FA / MFA Methods
|
|
371
|
+
async generate2FA() {
|
|
372
|
+
if (!this.token) {
|
|
373
|
+
throw new Error("Not authenticated");
|
|
374
|
+
}
|
|
375
|
+
const response = await this.httpClient.post(
|
|
376
|
+
"/api/v1/mfa/generate",
|
|
377
|
+
{}
|
|
378
|
+
);
|
|
379
|
+
return response;
|
|
380
|
+
}
|
|
381
|
+
async enable2FA(token) {
|
|
382
|
+
if (!this.token) {
|
|
383
|
+
throw new Error("Not authenticated");
|
|
384
|
+
}
|
|
385
|
+
const response = await this.httpClient.post("/api/v1/mfa/enable", { token });
|
|
386
|
+
return response;
|
|
387
|
+
}
|
|
388
|
+
async disable2FA(token) {
|
|
389
|
+
if (!this.token) {
|
|
390
|
+
throw new Error("Not authenticated");
|
|
391
|
+
}
|
|
392
|
+
const response = await this.httpClient.post("/api/v1/mfa/disable", { token });
|
|
393
|
+
return response;
|
|
394
|
+
}
|
|
395
|
+
async validate2FA(token) {
|
|
396
|
+
if (!this.token) {
|
|
397
|
+
throw new Error("Not authenticated");
|
|
398
|
+
}
|
|
399
|
+
const response = await this.httpClient.post("/api/v1/mfa/validate", { token });
|
|
400
|
+
return response;
|
|
401
|
+
}
|
|
402
|
+
// Session Management Methods
|
|
403
|
+
async getSessions() {
|
|
404
|
+
if (!this.token) {
|
|
405
|
+
throw new Error("Not authenticated");
|
|
406
|
+
}
|
|
407
|
+
const response = await this.httpClient.get("/api/v1/sessions");
|
|
408
|
+
return response;
|
|
409
|
+
}
|
|
410
|
+
async revokeSession(sessionId) {
|
|
411
|
+
if (!this.token) {
|
|
412
|
+
throw new Error("Not authenticated");
|
|
413
|
+
}
|
|
414
|
+
const response = await this.httpClient.delete(`/api/v1/sessions/${sessionId}`);
|
|
415
|
+
return response;
|
|
416
|
+
}
|
|
417
|
+
async revokeAllSessions() {
|
|
418
|
+
if (!this.token) {
|
|
419
|
+
throw new Error("Not authenticated");
|
|
420
|
+
}
|
|
421
|
+
const response = await this.httpClient.delete("/api/v1/sessions/revoke/all");
|
|
422
|
+
this.token = null;
|
|
423
|
+
this.httpClient.removeAuthToken();
|
|
424
|
+
this.removeTokenFromStorage();
|
|
425
|
+
return response;
|
|
426
|
+
}
|
|
427
|
+
// Admin Methods
|
|
428
|
+
async getAuditLogs(filters) {
|
|
429
|
+
if (!this.token) {
|
|
430
|
+
throw new Error("Not authenticated");
|
|
431
|
+
}
|
|
432
|
+
const response = await this.httpClient.get(
|
|
433
|
+
"/api/v1/admin/audit-logs",
|
|
434
|
+
filters
|
|
435
|
+
);
|
|
436
|
+
return response;
|
|
437
|
+
}
|
|
438
|
+
async adminVerifyUser(userId) {
|
|
439
|
+
if (!this.token) {
|
|
440
|
+
throw new Error("Not authenticated");
|
|
441
|
+
}
|
|
442
|
+
const response = await this.httpClient.post(`/api/v1/admin/verify-user/${userId}`, {});
|
|
443
|
+
return response;
|
|
444
|
+
}
|
|
445
|
+
async adminForcePasswordReset(userId) {
|
|
446
|
+
if (!this.token) {
|
|
447
|
+
throw new Error("Not authenticated");
|
|
448
|
+
}
|
|
449
|
+
const response = await this.httpClient.post(`/api/v1/admin/force-password-reset/${userId}`, {});
|
|
450
|
+
return response;
|
|
451
|
+
}
|
|
452
|
+
async adminSuspendUser(userId) {
|
|
453
|
+
if (!this.token) {
|
|
454
|
+
throw new Error("Not authenticated");
|
|
455
|
+
}
|
|
456
|
+
const response = await this.httpClient.post(`/api/v1/admin/suspend-user/${userId}`, {});
|
|
457
|
+
return response;
|
|
458
|
+
}
|
|
459
|
+
async adminActivateUser(userId) {
|
|
460
|
+
if (!this.token) {
|
|
461
|
+
throw new Error("Not authenticated");
|
|
462
|
+
}
|
|
463
|
+
const response = await this.httpClient.post(`/api/v1/admin/activate-user/${userId}`, {});
|
|
464
|
+
return response;
|
|
465
|
+
}
|
|
297
466
|
};
|
|
298
467
|
|
|
299
468
|
// src/node/auth-client.ts
|
|
@@ -304,11 +473,11 @@ var AuthClient = class extends AuthService {
|
|
|
304
473
|
// Override methods that require browser-specific features
|
|
305
474
|
// For Node.js, token persistence must be handled manually
|
|
306
475
|
async register(data) {
|
|
307
|
-
const
|
|
308
|
-
if (
|
|
309
|
-
|
|
476
|
+
const frontendBaseUrl = process.env.FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL;
|
|
477
|
+
if (frontendBaseUrl) {
|
|
478
|
+
this["httpClient"].setFrontendBaseUrl(frontendBaseUrl);
|
|
310
479
|
}
|
|
311
|
-
const response = await this["httpClient"].post("/api/v1/auth/register",
|
|
480
|
+
const response = await this["httpClient"].post("/api/v1/auth/register", data);
|
|
312
481
|
if (response.success && response.message === "Registration data saved. Verification email sent. Please check your inbox.") {
|
|
313
482
|
return response;
|
|
314
483
|
}
|
|
@@ -388,8 +557,8 @@ var NextServerAuth = class extends AuthClient {
|
|
|
388
557
|
return authHeader.substring(7);
|
|
389
558
|
}
|
|
390
559
|
// Parse token from cookies
|
|
391
|
-
static parseTokenFromCookies(
|
|
392
|
-
const cookieArray =
|
|
560
|
+
static parseTokenFromCookies(cookies2) {
|
|
561
|
+
const cookieArray = cookies2.split(";");
|
|
393
562
|
for (const cookie of cookieArray) {
|
|
394
563
|
const [name, value] = cookie.trim().split("=");
|
|
395
564
|
if (name === "auth_token") {
|
|
@@ -433,10 +602,150 @@ var NextServerAuth = class extends AuthClient {
|
|
|
433
602
|
return client;
|
|
434
603
|
}
|
|
435
604
|
};
|
|
605
|
+
var AuthServer = class {
|
|
606
|
+
constructor(config) {
|
|
607
|
+
this.config = {
|
|
608
|
+
authApiUrl: config?.authApiUrl || process.env.AUTH_API_URL || "http://localhost:7000",
|
|
609
|
+
tokenCookieName: config?.tokenCookieName || "auth_token"
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
async getToken() {
|
|
613
|
+
const cookieStore = await headers.cookies();
|
|
614
|
+
const token = cookieStore.get(this.config.tokenCookieName);
|
|
615
|
+
return token?.value || null;
|
|
616
|
+
}
|
|
617
|
+
async getCurrentUser() {
|
|
618
|
+
const token = await this.getToken();
|
|
619
|
+
if (!token)
|
|
620
|
+
return null;
|
|
621
|
+
try {
|
|
622
|
+
const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
|
|
623
|
+
return payload.user || null;
|
|
624
|
+
} catch (error) {
|
|
625
|
+
console.error("Failed to parse user from token:", error);
|
|
626
|
+
return null;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
async isAuthenticated() {
|
|
630
|
+
const token = await this.getToken();
|
|
631
|
+
return !!token;
|
|
632
|
+
}
|
|
633
|
+
async requireAuth(redirectTo) {
|
|
634
|
+
const user = await this.getCurrentUser();
|
|
635
|
+
if (!user) {
|
|
636
|
+
const loginPath = redirectTo || process.env.NEXT_PUBLIC_AUTH_REDIRECT_TO_LOGIN || "/auth/login";
|
|
637
|
+
navigation.redirect(loginPath);
|
|
638
|
+
}
|
|
639
|
+
return user;
|
|
640
|
+
}
|
|
641
|
+
async redirectIfAuthenticated(redirectTo) {
|
|
642
|
+
const isAuth = await this.isAuthenticated();
|
|
643
|
+
if (isAuth) {
|
|
644
|
+
const dashboardPath = redirectTo || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGIN || "/dashboard";
|
|
645
|
+
navigation.redirect(dashboardPath);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
async getProfile() {
|
|
649
|
+
const token = await this.getToken();
|
|
650
|
+
if (!token)
|
|
651
|
+
return null;
|
|
652
|
+
try {
|
|
653
|
+
const response = await fetch(`${this.config.authApiUrl}/api/v1/user/me`, {
|
|
654
|
+
headers: {
|
|
655
|
+
"Authorization": `Bearer ${token}`
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
if (!response.ok) {
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
const data = await response.json();
|
|
662
|
+
return data.user;
|
|
663
|
+
} catch (error) {
|
|
664
|
+
console.error("Failed to fetch profile:", error);
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
var authServerInstance = null;
|
|
670
|
+
function getAuthServer(config) {
|
|
671
|
+
if (!authServerInstance) {
|
|
672
|
+
authServerInstance = new AuthServer(config);
|
|
673
|
+
}
|
|
674
|
+
return authServerInstance;
|
|
675
|
+
}
|
|
676
|
+
async function currentUser() {
|
|
677
|
+
const auth2 = getAuthServer();
|
|
678
|
+
return auth2.getCurrentUser();
|
|
679
|
+
}
|
|
680
|
+
async function auth() {
|
|
681
|
+
const authServer = getAuthServer();
|
|
682
|
+
const user = await authServer.getCurrentUser();
|
|
683
|
+
const token = await authServer.getToken();
|
|
684
|
+
return {
|
|
685
|
+
user,
|
|
686
|
+
userId: user?._id || null,
|
|
687
|
+
isAuthenticated: !!user,
|
|
688
|
+
token
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
async function requireAuth(redirectTo) {
|
|
692
|
+
const authServer = getAuthServer();
|
|
693
|
+
return authServer.requireAuth(redirectTo);
|
|
694
|
+
}
|
|
695
|
+
async function redirectIfAuthenticated(redirectTo) {
|
|
696
|
+
const authServer = getAuthServer();
|
|
697
|
+
return authServer.redirectIfAuthenticated(redirectTo);
|
|
698
|
+
}
|
|
699
|
+
function authMiddleware(config) {
|
|
700
|
+
const {
|
|
701
|
+
publicRoutes = ["/auth/login", "/auth/register", "/auth/verify-email", "/auth/forgot-password", "/auth/reset-password"],
|
|
702
|
+
protectedRoutes = ["/dashboard"],
|
|
703
|
+
loginUrl = "/auth/login",
|
|
704
|
+
afterLoginUrl = "/dashboard",
|
|
705
|
+
tokenCookieName = "auth_token"
|
|
706
|
+
} = config || {};
|
|
707
|
+
return function middleware(request) {
|
|
708
|
+
const { pathname } = request.nextUrl;
|
|
709
|
+
const token = request.cookies.get(tokenCookieName)?.value;
|
|
710
|
+
const isAuthenticated = !!token;
|
|
711
|
+
const isPublicRoute = publicRoutes.some((route) => {
|
|
712
|
+
if (route.endsWith("*")) {
|
|
713
|
+
return pathname.startsWith(route.slice(0, -1));
|
|
714
|
+
}
|
|
715
|
+
return pathname === route || pathname.startsWith(route + "/");
|
|
716
|
+
});
|
|
717
|
+
const isProtectedRoute = protectedRoutes.some((route) => {
|
|
718
|
+
if (route.endsWith("*")) {
|
|
719
|
+
return pathname.startsWith(route.slice(0, -1));
|
|
720
|
+
}
|
|
721
|
+
return pathname === route || pathname.startsWith(route + "/");
|
|
722
|
+
});
|
|
723
|
+
if (isAuthenticated && isPublicRoute) {
|
|
724
|
+
return server.NextResponse.redirect(new URL(afterLoginUrl, request.url));
|
|
725
|
+
}
|
|
726
|
+
if (!isAuthenticated && isProtectedRoute) {
|
|
727
|
+
const loginUrlWithRedirect = new URL(loginUrl, request.url);
|
|
728
|
+
loginUrlWithRedirect.searchParams.set("redirect", pathname);
|
|
729
|
+
return server.NextResponse.redirect(loginUrlWithRedirect);
|
|
730
|
+
}
|
|
731
|
+
return server.NextResponse.next();
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
function createAuthMiddleware(config) {
|
|
735
|
+
return authMiddleware(config);
|
|
736
|
+
}
|
|
436
737
|
|
|
437
738
|
exports.AuthClient = AuthClient;
|
|
739
|
+
exports.AuthServer = AuthServer;
|
|
438
740
|
exports.AuthService = AuthService;
|
|
439
741
|
exports.HttpClient = HttpClient;
|
|
440
742
|
exports.NextServerAuth = NextServerAuth;
|
|
743
|
+
exports.auth = auth;
|
|
744
|
+
exports.authMiddleware = authMiddleware;
|
|
745
|
+
exports.createAuthMiddleware = createAuthMiddleware;
|
|
746
|
+
exports.currentUser = currentUser;
|
|
747
|
+
exports.getAuthServer = getAuthServer;
|
|
748
|
+
exports.redirectIfAuthenticated = redirectIfAuthenticated;
|
|
749
|
+
exports.requireAuth = requireAuth;
|
|
441
750
|
//# sourceMappingURL=out.js.map
|
|
442
751
|
//# sourceMappingURL=index.next.server.js.map
|