@thetechfossil/auth2 1.2.2 → 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/dist/index.components.js +4 -4
- package/dist/index.components.js.map +1 -1
- package/dist/index.components.mjs +4 -4
- package/dist/index.components.mjs.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -3
- package/dist/index.mjs.map +1 -1
- package/dist/index.next.d.ts +6 -61
- package/dist/index.next.js +20 -301
- package/dist/index.next.js.map +1 -1
- package/dist/index.next.mjs +21 -293
- package/dist/index.next.mjs.map +1 -1
- package/dist/index.next.server.d.ts +43 -4
- package/dist/index.next.server.js +148 -5
- package/dist/index.next.server.js.map +1 -1
- package/dist/index.next.server.mjs +141 -6
- package/dist/index.next.server.mjs.map +1 -1
- package/dist/index.node.d.ts +5 -3
- package/dist/index.node.js +3 -3
- package/dist/index.node.js.map +1 -1
- package/dist/index.node.mjs +3 -3
- 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 +19 -17
package/dist/index.next.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import React, { ReactNode } from 'react';
|
|
2
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
3
2
|
|
|
4
3
|
interface LinkedAccount {
|
|
5
4
|
provider: 'google' | 'github';
|
|
@@ -57,11 +56,13 @@ interface AuthConfig {
|
|
|
57
56
|
}
|
|
58
57
|
interface Session {
|
|
59
58
|
id: string;
|
|
60
|
-
userId
|
|
61
|
-
|
|
59
|
+
userId?: string;
|
|
60
|
+
token?: string;
|
|
61
|
+
userAgent?: string;
|
|
62
62
|
ipAddress?: string;
|
|
63
|
-
|
|
63
|
+
expiresAt: string;
|
|
64
64
|
createdAt: string;
|
|
65
|
+
updatedAt?: string;
|
|
65
66
|
}
|
|
66
67
|
interface MFASetup {
|
|
67
68
|
success: boolean;
|
|
@@ -422,60 +423,4 @@ interface UseNextAuthReturn {
|
|
|
422
423
|
}
|
|
423
424
|
declare const useNextAuth: (config: AuthConfig) => UseNextAuthReturn;
|
|
424
425
|
|
|
425
|
-
|
|
426
|
-
constructor(config: AuthConfig);
|
|
427
|
-
register(data: RegisterData): Promise<AuthResponse>;
|
|
428
|
-
login(data: LoginData): Promise<AuthResponse>;
|
|
429
|
-
verify(data: VerifyData): Promise<AuthResponse>;
|
|
430
|
-
logout(): Promise<void>;
|
|
431
|
-
getProfile(): Promise<User>;
|
|
432
|
-
getUserById(id: string): Promise<User>;
|
|
433
|
-
updateProfile(data: UpdateUserData): Promise<AuthResponse>;
|
|
434
|
-
getAllUsers(): Promise<User[]>;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
declare class NextServerAuth extends AuthClient {
|
|
438
|
-
constructor(config: AuthConfig);
|
|
439
|
-
static parseTokenFromHeaders(headers: Headers): string | null;
|
|
440
|
-
static parseTokenFromCookies(cookies: string): string | null;
|
|
441
|
-
static parseTokenFromRequest(req: any): string | null;
|
|
442
|
-
verifyToken(token: string): Promise<User | null>;
|
|
443
|
-
static createAuthenticatedClient(config: AuthConfig, token: string): NextServerAuth;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
interface AuthServerConfig {
|
|
447
|
-
authApiUrl?: string;
|
|
448
|
-
tokenCookieName?: string;
|
|
449
|
-
}
|
|
450
|
-
declare class AuthServer {
|
|
451
|
-
private config;
|
|
452
|
-
constructor(config?: AuthServerConfig);
|
|
453
|
-
getToken(): Promise<string | null>;
|
|
454
|
-
getCurrentUser(): Promise<User | null>;
|
|
455
|
-
isAuthenticated(): Promise<boolean>;
|
|
456
|
-
requireAuth(redirectTo?: string): Promise<User>;
|
|
457
|
-
redirectIfAuthenticated(redirectTo?: string): Promise<void>;
|
|
458
|
-
getProfile(): Promise<User | null>;
|
|
459
|
-
}
|
|
460
|
-
declare function getAuthServer(config?: AuthServerConfig): AuthServer;
|
|
461
|
-
declare function currentUser(): Promise<User | null>;
|
|
462
|
-
declare function auth(): Promise<{
|
|
463
|
-
user: User | null;
|
|
464
|
-
userId: string | null;
|
|
465
|
-
isAuthenticated: boolean;
|
|
466
|
-
token: string | null;
|
|
467
|
-
}>;
|
|
468
|
-
declare function requireAuth(redirectTo?: string): Promise<User>;
|
|
469
|
-
declare function redirectIfAuthenticated(redirectTo?: string): Promise<void>;
|
|
470
|
-
|
|
471
|
-
interface AuthMiddlewareConfig {
|
|
472
|
-
publicRoutes?: string[];
|
|
473
|
-
protectedRoutes?: string[];
|
|
474
|
-
loginUrl?: string;
|
|
475
|
-
afterLoginUrl?: string;
|
|
476
|
-
tokenCookieName?: string;
|
|
477
|
-
}
|
|
478
|
-
declare function authMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => NextResponse<unknown>;
|
|
479
|
-
declare function createAuthMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => NextResponse<unknown>;
|
|
480
|
-
|
|
481
|
-
export { AuditLog, AuthConfig, AuthFlow, AuthProvider, AuthResponse, AuthServer, AuthService, ChangePassword, CsrfTokenResponse, EmailVerificationPage, ForgotPassword, HttpClient, LinkedAccount, LoginData, LoginForm, MFASetup, NextServerAuth, OAuthConfig, OAuthProvider, OtpForm, ProtectedRoute, PublicRoute, RegisterData, RegisterForm, ResetPassword, Session, SignIn, SignOut, SignUp, UpdateUserData, UseAuthReturn, User, UserButton, UserProfile, VerifyData, VerifyEmail, auth, authMiddleware, createAuthMiddleware, currentUser, getAuthServer, redirectIfAuthenticated, requireAuth, useAuth, useAuth$1 as useAuthLegacy, useNextAuth };
|
|
426
|
+
export { AuditLog, AuthConfig, AuthFlow, AuthProvider, AuthResponse, AuthService, ChangePassword, CsrfTokenResponse, EmailVerificationPage, ForgotPassword, HttpClient, LinkedAccount, LoginData, LoginForm, MFASetup, OAuthConfig, OAuthProvider, OtpForm, ProtectedRoute, PublicRoute, RegisterData, RegisterForm, ResetPassword, Session, SignIn, SignOut, SignUp, UpdateUserData, UseAuthReturn, User, UserButton, UserProfile, VerifyData, VerifyEmail, useAuth, useAuth$1 as useAuthLegacy, useNextAuth };
|
package/dist/index.next.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
+
"use client";
|
|
1
2
|
'use strict';
|
|
2
3
|
|
|
3
4
|
var axios = require('axios');
|
|
4
5
|
var react = require('react');
|
|
5
6
|
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
-
var headers = require('next/headers');
|
|
7
|
-
var navigation = require('next/navigation');
|
|
8
|
-
var server = require('next/server');
|
|
9
7
|
|
|
10
8
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
9
|
|
|
@@ -406,21 +404,21 @@ var AuthService = class {
|
|
|
406
404
|
if (!this.token) {
|
|
407
405
|
throw new Error("Not authenticated");
|
|
408
406
|
}
|
|
409
|
-
const response = await this.httpClient.get("/api/v1/
|
|
407
|
+
const response = await this.httpClient.get("/api/v1/sessions");
|
|
410
408
|
return response;
|
|
411
409
|
}
|
|
412
410
|
async revokeSession(sessionId) {
|
|
413
411
|
if (!this.token) {
|
|
414
412
|
throw new Error("Not authenticated");
|
|
415
413
|
}
|
|
416
|
-
const response = await this.httpClient.delete(`/api/v1/
|
|
414
|
+
const response = await this.httpClient.delete(`/api/v1/sessions/${sessionId}`);
|
|
417
415
|
return response;
|
|
418
416
|
}
|
|
419
417
|
async revokeAllSessions() {
|
|
420
418
|
if (!this.token) {
|
|
421
419
|
throw new Error("Not authenticated");
|
|
422
420
|
}
|
|
423
|
-
const response = await this.httpClient.delete("/api/v1/
|
|
421
|
+
const response = await this.httpClient.delete("/api/v1/sessions/revoke/all");
|
|
424
422
|
this.token = null;
|
|
425
423
|
this.httpClient.removeAuthToken();
|
|
426
424
|
this.removeTokenFromStorage();
|
|
@@ -475,8 +473,8 @@ var useAuth = (config) => {
|
|
|
475
473
|
const authenticated = authService.isAuthenticated();
|
|
476
474
|
setIsAuthenticated(authenticated);
|
|
477
475
|
if (authenticated) {
|
|
478
|
-
const
|
|
479
|
-
setUser(
|
|
476
|
+
const currentUser = authService.getCurrentUser();
|
|
477
|
+
setUser(currentUser);
|
|
480
478
|
} else {
|
|
481
479
|
setUser(null);
|
|
482
480
|
}
|
|
@@ -611,8 +609,8 @@ var AuthProvider = ({ children, config }) => {
|
|
|
611
609
|
const authenticated = authService.isAuthenticated();
|
|
612
610
|
if (authenticated) {
|
|
613
611
|
try {
|
|
614
|
-
const
|
|
615
|
-
setUser(
|
|
612
|
+
const currentUser = authService.getCurrentUser();
|
|
613
|
+
setUser(currentUser);
|
|
616
614
|
} catch (error) {
|
|
617
615
|
console.error("Failed to get current user:", error);
|
|
618
616
|
setUser(null);
|
|
@@ -2195,9 +2193,9 @@ var SignIn = ({ redirectUrl, appearance }) => {
|
|
|
2195
2193
|
const [success, setSuccess] = react.useState(null);
|
|
2196
2194
|
react.useEffect(() => {
|
|
2197
2195
|
if (isSignedIn && redirectUrl) {
|
|
2198
|
-
const
|
|
2196
|
+
const redirect = redirectUrl || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGIN || process.env.REACT_APP_AUTH_REDIRECT_AFTER_LOGIN || "/dashboard";
|
|
2199
2197
|
const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
|
|
2200
|
-
window.location.href = `${baseUrl}${
|
|
2198
|
+
window.location.href = `${baseUrl}${redirect}`;
|
|
2201
2199
|
}
|
|
2202
2200
|
}, [isSignedIn, redirectUrl]);
|
|
2203
2201
|
const handleSubmit = async (e) => {
|
|
@@ -2466,9 +2464,9 @@ var SignUp = ({ redirectUrl, appearance }) => {
|
|
|
2466
2464
|
const [success, setSuccess] = react.useState(null);
|
|
2467
2465
|
react.useEffect(() => {
|
|
2468
2466
|
if (isSignedIn && redirectUrl) {
|
|
2469
|
-
const
|
|
2467
|
+
const redirect = redirectUrl || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_REGISTER || process.env.REACT_APP_AUTH_REDIRECT_AFTER_REGISTER || "/dashboard";
|
|
2470
2468
|
const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
|
|
2471
|
-
window.location.href = `${baseUrl}${
|
|
2469
|
+
window.location.href = `${baseUrl}${redirect}`;
|
|
2472
2470
|
}
|
|
2473
2471
|
}, [isSignedIn, redirectUrl]);
|
|
2474
2472
|
const getPasswordStrength = (pwd) => {
|
|
@@ -2745,9 +2743,9 @@ var SignOut = ({ redirectUrl }) => {
|
|
|
2745
2743
|
react.useEffect(() => {
|
|
2746
2744
|
const performSignOut = async () => {
|
|
2747
2745
|
await signOut();
|
|
2748
|
-
const
|
|
2746
|
+
const redirect = redirectUrl || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGOUT || process.env.REACT_APP_AUTH_REDIRECT_AFTER_LOGOUT || "/";
|
|
2749
2747
|
const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
|
|
2750
|
-
window.location.href = `${baseUrl}${
|
|
2748
|
+
window.location.href = `${baseUrl}${redirect}`;
|
|
2751
2749
|
};
|
|
2752
2750
|
performSignOut();
|
|
2753
2751
|
}, [signOut, redirectUrl]);
|
|
@@ -2773,9 +2771,9 @@ var UserButton = ({ showName = false, appearance }) => {
|
|
|
2773
2771
|
};
|
|
2774
2772
|
const handleSignOut = async () => {
|
|
2775
2773
|
await signOut();
|
|
2776
|
-
const
|
|
2774
|
+
const redirect = process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGOUT || process.env.REACT_APP_AUTH_REDIRECT_AFTER_LOGOUT || "/";
|
|
2777
2775
|
const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
|
|
2778
|
-
window.location.href = `${baseUrl}${
|
|
2776
|
+
window.location.href = `${baseUrl}${redirect}`;
|
|
2779
2777
|
};
|
|
2780
2778
|
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { position: "relative", ...appearance?.elements?.userButtonBox }, ref: dropdownRef, children: [
|
|
2781
2779
|
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
@@ -2968,9 +2966,9 @@ var VerifyEmail = ({ token, onSuccess, onError }) => {
|
|
|
2968
2966
|
setMessage("Email verified successfully! Redirecting...");
|
|
2969
2967
|
onSuccess?.();
|
|
2970
2968
|
setTimeout(() => {
|
|
2971
|
-
const
|
|
2969
|
+
const redirect = process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_VERIFY || process.env.REACT_APP_AUTH_REDIRECT_AFTER_VERIFY || "/dashboard";
|
|
2972
2970
|
const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
|
|
2973
|
-
window.location.href = `${baseUrl}${
|
|
2971
|
+
window.location.href = `${baseUrl}${redirect}`;
|
|
2974
2972
|
}, 2e3);
|
|
2975
2973
|
} else {
|
|
2976
2974
|
setStatus("error");
|
|
@@ -4010,8 +4008,8 @@ var useNextAuth = (config) => {
|
|
|
4010
4008
|
const authenticated = authService.isAuthenticated();
|
|
4011
4009
|
setIsAuthenticated(authenticated);
|
|
4012
4010
|
if (authenticated) {
|
|
4013
|
-
const
|
|
4014
|
-
setUser(
|
|
4011
|
+
const currentUser = authService.getCurrentUser();
|
|
4012
|
+
setUser(currentUser);
|
|
4015
4013
|
} else {
|
|
4016
4014
|
setUser(null);
|
|
4017
4015
|
}
|
|
@@ -4169,286 +4167,14 @@ var useNextAuth = (config) => {
|
|
|
4169
4167
|
};
|
|
4170
4168
|
};
|
|
4171
4169
|
|
|
4172
|
-
// src/node/auth-client.ts
|
|
4173
|
-
var AuthClient = class extends AuthService {
|
|
4174
|
-
constructor(config) {
|
|
4175
|
-
super(config);
|
|
4176
|
-
}
|
|
4177
|
-
// Override methods that require browser-specific features
|
|
4178
|
-
// For Node.js, token persistence must be handled manually
|
|
4179
|
-
async register(data) {
|
|
4180
|
-
const frontendBaseUrl = process.env.FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL;
|
|
4181
|
-
if (frontendBaseUrl) {
|
|
4182
|
-
this["httpClient"].setFrontendBaseUrl(frontendBaseUrl);
|
|
4183
|
-
}
|
|
4184
|
-
const response = await this["httpClient"].post("/api/v1/auth/register", data);
|
|
4185
|
-
if (response.success && response.message === "Registration data saved. Verification email sent. Please check your inbox.") {
|
|
4186
|
-
return response;
|
|
4187
|
-
}
|
|
4188
|
-
throw new Error(response.message || "Registration failed");
|
|
4189
|
-
}
|
|
4190
|
-
async login(data) {
|
|
4191
|
-
const response = await this["httpClient"].post("/api/v1/auth/login", data);
|
|
4192
|
-
if (response.success && response.token) {
|
|
4193
|
-
this["token"] = response.token;
|
|
4194
|
-
this["httpClient"].setAuthToken(response.token);
|
|
4195
|
-
return response;
|
|
4196
|
-
}
|
|
4197
|
-
if (response.success && response.message === "OTP sent to your email.") {
|
|
4198
|
-
return response;
|
|
4199
|
-
}
|
|
4200
|
-
if (response.success && response.message === "OTP verified successfully." && response.token) {
|
|
4201
|
-
this["token"] = response.token;
|
|
4202
|
-
this["httpClient"].setAuthToken(response.token);
|
|
4203
|
-
return response;
|
|
4204
|
-
}
|
|
4205
|
-
throw new Error(response.message || "Login failed");
|
|
4206
|
-
}
|
|
4207
|
-
async verify(data) {
|
|
4208
|
-
const response = await this["httpClient"].post("/api/v1/auth/verify", data);
|
|
4209
|
-
if (response.success && response.token) {
|
|
4210
|
-
this["token"] = response.token;
|
|
4211
|
-
this["httpClient"].setAuthToken(response.token);
|
|
4212
|
-
}
|
|
4213
|
-
return response;
|
|
4214
|
-
}
|
|
4215
|
-
async logout() {
|
|
4216
|
-
this["token"] = null;
|
|
4217
|
-
this["httpClient"].removeAuthToken();
|
|
4218
|
-
}
|
|
4219
|
-
async getProfile() {
|
|
4220
|
-
if (!this["token"]) {
|
|
4221
|
-
throw new Error("Not authenticated");
|
|
4222
|
-
}
|
|
4223
|
-
const response = await this["httpClient"].get("/api/v1/user/me");
|
|
4224
|
-
return response.user;
|
|
4225
|
-
}
|
|
4226
|
-
async getUserById(id) {
|
|
4227
|
-
const response = await this["httpClient"].get(`/api/v1/user/${id}`);
|
|
4228
|
-
return response.user;
|
|
4229
|
-
}
|
|
4230
|
-
async updateProfile(data) {
|
|
4231
|
-
if (!this["token"]) {
|
|
4232
|
-
throw new Error("Not authenticated");
|
|
4233
|
-
}
|
|
4234
|
-
const response = await this["httpClient"].post("/api/v1/user/update/name", data);
|
|
4235
|
-
if (response.success && response.token) {
|
|
4236
|
-
this["token"] = response.token;
|
|
4237
|
-
this["httpClient"].setAuthToken(response.token);
|
|
4238
|
-
}
|
|
4239
|
-
return response;
|
|
4240
|
-
}
|
|
4241
|
-
async getAllUsers() {
|
|
4242
|
-
if (!this["token"]) {
|
|
4243
|
-
throw new Error("Not authenticated");
|
|
4244
|
-
}
|
|
4245
|
-
const response = await this["httpClient"].get("/api/v1/user/all");
|
|
4246
|
-
return response.users;
|
|
4247
|
-
}
|
|
4248
|
-
};
|
|
4249
|
-
|
|
4250
|
-
// src/nextjs/server-auth.ts
|
|
4251
|
-
var NextServerAuth = class extends AuthClient {
|
|
4252
|
-
constructor(config) {
|
|
4253
|
-
super(config);
|
|
4254
|
-
}
|
|
4255
|
-
// Parse token from request headers
|
|
4256
|
-
static parseTokenFromHeaders(headers) {
|
|
4257
|
-
const authHeader = headers.get("authorization");
|
|
4258
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
4259
|
-
return null;
|
|
4260
|
-
}
|
|
4261
|
-
return authHeader.substring(7);
|
|
4262
|
-
}
|
|
4263
|
-
// Parse token from cookies
|
|
4264
|
-
static parseTokenFromCookies(cookies2) {
|
|
4265
|
-
const cookieArray = cookies2.split(";");
|
|
4266
|
-
for (const cookie of cookieArray) {
|
|
4267
|
-
const [name, value] = cookie.trim().split("=");
|
|
4268
|
-
if (name === "auth_token") {
|
|
4269
|
-
return decodeURIComponent(value);
|
|
4270
|
-
}
|
|
4271
|
-
}
|
|
4272
|
-
return null;
|
|
4273
|
-
}
|
|
4274
|
-
// Parse token from Next.js request object
|
|
4275
|
-
static parseTokenFromRequest(req) {
|
|
4276
|
-
if (req.headers) {
|
|
4277
|
-
const authHeader = req.headers.authorization || req.headers.Authorization;
|
|
4278
|
-
if (authHeader && authHeader.startsWith("Bearer ")) {
|
|
4279
|
-
return authHeader.substring(7);
|
|
4280
|
-
}
|
|
4281
|
-
}
|
|
4282
|
-
if (req.cookies) {
|
|
4283
|
-
return req.cookies.auth_token || null;
|
|
4284
|
-
}
|
|
4285
|
-
if (req.headers && req.headers.cookie) {
|
|
4286
|
-
return this.parseTokenFromCookies(req.headers.cookie);
|
|
4287
|
-
}
|
|
4288
|
-
return null;
|
|
4289
|
-
}
|
|
4290
|
-
// Verify token and get user
|
|
4291
|
-
async verifyToken(token) {
|
|
4292
|
-
try {
|
|
4293
|
-
this["httpClient"].setAuthToken(token);
|
|
4294
|
-
const user = await this.getProfile();
|
|
4295
|
-
return user;
|
|
4296
|
-
} catch (error) {
|
|
4297
|
-
console.error("Token verification failed:", error);
|
|
4298
|
-
return null;
|
|
4299
|
-
}
|
|
4300
|
-
}
|
|
4301
|
-
// Create authenticated client with token
|
|
4302
|
-
static createAuthenticatedClient(config, token) {
|
|
4303
|
-
const client = new NextServerAuth(config);
|
|
4304
|
-
client["httpClient"].setAuthToken(token);
|
|
4305
|
-
client["token"] = token;
|
|
4306
|
-
return client;
|
|
4307
|
-
}
|
|
4308
|
-
};
|
|
4309
|
-
var AuthServer = class {
|
|
4310
|
-
constructor(config) {
|
|
4311
|
-
this.config = {
|
|
4312
|
-
authApiUrl: config?.authApiUrl || process.env.AUTH_API_URL || "http://localhost:7000",
|
|
4313
|
-
tokenCookieName: config?.tokenCookieName || "auth_token"
|
|
4314
|
-
};
|
|
4315
|
-
}
|
|
4316
|
-
async getToken() {
|
|
4317
|
-
const cookieStore = await headers.cookies();
|
|
4318
|
-
const token = cookieStore.get(this.config.tokenCookieName);
|
|
4319
|
-
return token?.value || null;
|
|
4320
|
-
}
|
|
4321
|
-
async getCurrentUser() {
|
|
4322
|
-
const token = await this.getToken();
|
|
4323
|
-
if (!token)
|
|
4324
|
-
return null;
|
|
4325
|
-
try {
|
|
4326
|
-
const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
|
|
4327
|
-
return payload.user || null;
|
|
4328
|
-
} catch (error) {
|
|
4329
|
-
console.error("Failed to parse user from token:", error);
|
|
4330
|
-
return null;
|
|
4331
|
-
}
|
|
4332
|
-
}
|
|
4333
|
-
async isAuthenticated() {
|
|
4334
|
-
const token = await this.getToken();
|
|
4335
|
-
return !!token;
|
|
4336
|
-
}
|
|
4337
|
-
async requireAuth(redirectTo) {
|
|
4338
|
-
const user = await this.getCurrentUser();
|
|
4339
|
-
if (!user) {
|
|
4340
|
-
const loginPath = redirectTo || process.env.NEXT_PUBLIC_AUTH_REDIRECT_TO_LOGIN || "/auth/login";
|
|
4341
|
-
navigation.redirect(loginPath);
|
|
4342
|
-
}
|
|
4343
|
-
return user;
|
|
4344
|
-
}
|
|
4345
|
-
async redirectIfAuthenticated(redirectTo) {
|
|
4346
|
-
const isAuth = await this.isAuthenticated();
|
|
4347
|
-
if (isAuth) {
|
|
4348
|
-
const dashboardPath = redirectTo || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGIN || "/dashboard";
|
|
4349
|
-
navigation.redirect(dashboardPath);
|
|
4350
|
-
}
|
|
4351
|
-
}
|
|
4352
|
-
async getProfile() {
|
|
4353
|
-
const token = await this.getToken();
|
|
4354
|
-
if (!token)
|
|
4355
|
-
return null;
|
|
4356
|
-
try {
|
|
4357
|
-
const response = await fetch(`${this.config.authApiUrl}/api/v1/user/me`, {
|
|
4358
|
-
headers: {
|
|
4359
|
-
"Authorization": `Bearer ${token}`
|
|
4360
|
-
}
|
|
4361
|
-
});
|
|
4362
|
-
if (!response.ok) {
|
|
4363
|
-
return null;
|
|
4364
|
-
}
|
|
4365
|
-
const data = await response.json();
|
|
4366
|
-
return data.user;
|
|
4367
|
-
} catch (error) {
|
|
4368
|
-
console.error("Failed to fetch profile:", error);
|
|
4369
|
-
return null;
|
|
4370
|
-
}
|
|
4371
|
-
}
|
|
4372
|
-
};
|
|
4373
|
-
var authServerInstance = null;
|
|
4374
|
-
function getAuthServer(config) {
|
|
4375
|
-
if (!authServerInstance) {
|
|
4376
|
-
authServerInstance = new AuthServer(config);
|
|
4377
|
-
}
|
|
4378
|
-
return authServerInstance;
|
|
4379
|
-
}
|
|
4380
|
-
async function currentUser() {
|
|
4381
|
-
const auth2 = getAuthServer();
|
|
4382
|
-
return auth2.getCurrentUser();
|
|
4383
|
-
}
|
|
4384
|
-
async function auth() {
|
|
4385
|
-
const authServer = getAuthServer();
|
|
4386
|
-
const user = await authServer.getCurrentUser();
|
|
4387
|
-
const token = await authServer.getToken();
|
|
4388
|
-
return {
|
|
4389
|
-
user,
|
|
4390
|
-
userId: user?._id || null,
|
|
4391
|
-
isAuthenticated: !!user,
|
|
4392
|
-
token
|
|
4393
|
-
};
|
|
4394
|
-
}
|
|
4395
|
-
async function requireAuth(redirectTo) {
|
|
4396
|
-
const authServer = getAuthServer();
|
|
4397
|
-
return authServer.requireAuth(redirectTo);
|
|
4398
|
-
}
|
|
4399
|
-
async function redirectIfAuthenticated(redirectTo) {
|
|
4400
|
-
const authServer = getAuthServer();
|
|
4401
|
-
return authServer.redirectIfAuthenticated(redirectTo);
|
|
4402
|
-
}
|
|
4403
|
-
function authMiddleware(config) {
|
|
4404
|
-
const {
|
|
4405
|
-
publicRoutes = ["/auth/login", "/auth/register", "/auth/verify-email", "/auth/forgot-password", "/auth/reset-password"],
|
|
4406
|
-
protectedRoutes = ["/dashboard"],
|
|
4407
|
-
loginUrl = "/auth/login",
|
|
4408
|
-
afterLoginUrl = "/dashboard",
|
|
4409
|
-
tokenCookieName = "auth_token"
|
|
4410
|
-
} = config || {};
|
|
4411
|
-
return function middleware(request) {
|
|
4412
|
-
const { pathname } = request.nextUrl;
|
|
4413
|
-
const token = request.cookies.get(tokenCookieName)?.value;
|
|
4414
|
-
const isAuthenticated = !!token;
|
|
4415
|
-
const isPublicRoute = publicRoutes.some((route) => {
|
|
4416
|
-
if (route.endsWith("*")) {
|
|
4417
|
-
return pathname.startsWith(route.slice(0, -1));
|
|
4418
|
-
}
|
|
4419
|
-
return pathname === route || pathname.startsWith(route + "/");
|
|
4420
|
-
});
|
|
4421
|
-
const isProtectedRoute = protectedRoutes.some((route) => {
|
|
4422
|
-
if (route.endsWith("*")) {
|
|
4423
|
-
return pathname.startsWith(route.slice(0, -1));
|
|
4424
|
-
}
|
|
4425
|
-
return pathname === route || pathname.startsWith(route + "/");
|
|
4426
|
-
});
|
|
4427
|
-
if (isAuthenticated && isPublicRoute) {
|
|
4428
|
-
return server.NextResponse.redirect(new URL(afterLoginUrl, request.url));
|
|
4429
|
-
}
|
|
4430
|
-
if (!isAuthenticated && isProtectedRoute) {
|
|
4431
|
-
const loginUrlWithRedirect = new URL(loginUrl, request.url);
|
|
4432
|
-
loginUrlWithRedirect.searchParams.set("redirect", pathname);
|
|
4433
|
-
return server.NextResponse.redirect(loginUrlWithRedirect);
|
|
4434
|
-
}
|
|
4435
|
-
return server.NextResponse.next();
|
|
4436
|
-
};
|
|
4437
|
-
}
|
|
4438
|
-
function createAuthMiddleware(config) {
|
|
4439
|
-
return authMiddleware(config);
|
|
4440
|
-
}
|
|
4441
|
-
|
|
4442
4170
|
exports.AuthFlow = AuthFlow;
|
|
4443
4171
|
exports.AuthProvider = AuthProvider;
|
|
4444
|
-
exports.AuthServer = AuthServer;
|
|
4445
4172
|
exports.AuthService = AuthService;
|
|
4446
4173
|
exports.ChangePassword = ChangePassword;
|
|
4447
4174
|
exports.EmailVerificationPage = EmailVerificationPage;
|
|
4448
4175
|
exports.ForgotPassword = ForgotPassword;
|
|
4449
4176
|
exports.HttpClient = HttpClient;
|
|
4450
4177
|
exports.LoginForm = LoginForm;
|
|
4451
|
-
exports.NextServerAuth = NextServerAuth;
|
|
4452
4178
|
exports.OtpForm = OtpForm;
|
|
4453
4179
|
exports.ProtectedRoute = ProtectedRoute;
|
|
4454
4180
|
exports.PublicRoute = PublicRoute;
|
|
@@ -4460,13 +4186,6 @@ exports.SignUp = SignUp;
|
|
|
4460
4186
|
exports.UserButton = UserButton;
|
|
4461
4187
|
exports.UserProfile = UserProfile;
|
|
4462
4188
|
exports.VerifyEmail = VerifyEmail;
|
|
4463
|
-
exports.auth = auth;
|
|
4464
|
-
exports.authMiddleware = authMiddleware;
|
|
4465
|
-
exports.createAuthMiddleware = createAuthMiddleware;
|
|
4466
|
-
exports.currentUser = currentUser;
|
|
4467
|
-
exports.getAuthServer = getAuthServer;
|
|
4468
|
-
exports.redirectIfAuthenticated = redirectIfAuthenticated;
|
|
4469
|
-
exports.requireAuth = requireAuth;
|
|
4470
4189
|
exports.useAuth = useAuth2;
|
|
4471
4190
|
exports.useAuthLegacy = useAuth;
|
|
4472
4191
|
exports.useNextAuth = useNextAuth;
|