@jhits/plugin-users 0.0.1 → 0.0.4
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/package.json +12 -20
- package/src/api/auth.ts +70 -38
- package/src/api/router.ts +12 -0
- package/src/index.server.ts +2 -0
package/package.json
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jhits/plugin-users",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "User management and authentication plugin for the JHITS ecosystem",
|
|
5
|
-
"publishConfig": {
|
|
6
|
-
"access": "public"
|
|
7
|
-
},
|
|
8
5
|
"main": "./src/index.tsx",
|
|
9
6
|
"types": "./src/index.tsx",
|
|
7
|
+
"browser": {
|
|
8
|
+
"mongodb": false,
|
|
9
|
+
"bcrypt": false,
|
|
10
|
+
"bcryptjs": false,
|
|
11
|
+
"server-only": false,
|
|
12
|
+
"./src/api/users.ts": false,
|
|
13
|
+
"./src/index.server.ts": false
|
|
14
|
+
},
|
|
10
15
|
"exports": {
|
|
11
16
|
".": {
|
|
12
17
|
"types": "./src/index.tsx",
|
|
18
|
+
"import": "./src/index.tsx",
|
|
13
19
|
"default": "./src/index.tsx"
|
|
14
20
|
},
|
|
15
21
|
"./server": {
|
|
16
22
|
"types": "./src/index.server.ts",
|
|
23
|
+
"import": "./src/index.server.ts",
|
|
17
24
|
"default": "./src/index.server.ts"
|
|
18
25
|
}
|
|
19
26
|
},
|
|
@@ -31,23 +38,8 @@
|
|
|
31
38
|
"react": ">=18.0.0",
|
|
32
39
|
"react-dom": ">=18.0.0"
|
|
33
40
|
},
|
|
34
|
-
"devDependencies": {
|
|
35
|
-
"@tailwindcss/postcss": "^4",
|
|
36
|
-
"@types/bcrypt": "^6.0.0",
|
|
37
|
-
"@types/bcryptjs": "^2.4.6",
|
|
38
|
-
"@types/node": "^20.19.27",
|
|
39
|
-
"@types/react": "^19",
|
|
40
|
-
"@types/react-dom": "^19",
|
|
41
|
-
"eslint": "^9",
|
|
42
|
-
"eslint-config-next": "16.1.1",
|
|
43
|
-
"next": "16.1.1",
|
|
44
|
-
"react": "19.2.3",
|
|
45
|
-
"react-dom": "19.2.3",
|
|
46
|
-
"tailwindcss": "^4",
|
|
47
|
-
"typescript": "^5"
|
|
48
|
-
},
|
|
49
41
|
"files": [
|
|
50
42
|
"src",
|
|
51
43
|
"package.json"
|
|
52
44
|
]
|
|
53
|
-
}
|
|
45
|
+
}
|
package/src/api/auth.ts
CHANGED
|
@@ -36,48 +36,80 @@ function getHandler() {
|
|
|
36
36
|
// Export handlers - NextAuth handler works for both GET and POST
|
|
37
37
|
// NextAuth expects the route to be /api/auth/[...nextauth] with path segments in context.params.nextauth
|
|
38
38
|
export async function GET(req: any, context?: { params?: Promise<{ nextauth?: string[] }> }) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
try {
|
|
40
|
+
const handler = getHandler();
|
|
41
|
+
|
|
42
|
+
// Use context if provided, otherwise extract from URL
|
|
43
|
+
let nextauthPath: string[] = [];
|
|
44
|
+
if (context?.params) {
|
|
45
|
+
const resolvedParams = await context.params;
|
|
46
|
+
nextauthPath = resolvedParams.nextauth || [];
|
|
47
|
+
} else {
|
|
48
|
+
// Fallback: extract from URL
|
|
49
|
+
const url = new URL(req.url);
|
|
50
|
+
const pathSegments = url.pathname.split('/').filter(Boolean);
|
|
51
|
+
// Path: /api/auth/session -> segments: ['api', 'auth', 'session'] -> nextauth: ['session']
|
|
52
|
+
nextauthPath = pathSegments.length > 2 ? pathSegments.slice(2) : [];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Create context with nextauth parameter (NextAuth expects this)
|
|
56
|
+
const nextauthContext = {
|
|
57
|
+
params: Promise.resolve({ nextauth: nextauthPath })
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return await handler(req, nextauthContext);
|
|
61
|
+
} catch (error: any) {
|
|
62
|
+
console.error('[NextAuth GET] Error:', error);
|
|
63
|
+
// Return a proper error response instead of throwing
|
|
64
|
+
// This prevents CLIENT_FETCH_ERROR in NextAuth
|
|
65
|
+
return new Response(
|
|
66
|
+
JSON.stringify({
|
|
67
|
+
error: error.message || 'Authentication error',
|
|
68
|
+
code: 'AUTH_ERROR'
|
|
69
|
+
}),
|
|
70
|
+
{
|
|
71
|
+
status: 500,
|
|
72
|
+
headers: { 'Content-Type': 'application/json' }
|
|
73
|
+
}
|
|
74
|
+
);
|
|
52
75
|
}
|
|
53
|
-
|
|
54
|
-
// Create context with nextauth parameter (NextAuth expects this)
|
|
55
|
-
const nextauthContext = {
|
|
56
|
-
params: Promise.resolve({ nextauth: nextauthPath })
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
return handler(req, nextauthContext);
|
|
60
76
|
}
|
|
61
77
|
|
|
62
78
|
export async function POST(req: any, context?: { params?: Promise<{ nextauth?: string[] }> }) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
try {
|
|
80
|
+
const handler = getHandler();
|
|
81
|
+
|
|
82
|
+
// Use context if provided, otherwise extract from URL
|
|
83
|
+
let nextauthPath: string[] = [];
|
|
84
|
+
if (context?.params) {
|
|
85
|
+
const resolvedParams = await context.params;
|
|
86
|
+
nextauthPath = resolvedParams.nextauth || [];
|
|
87
|
+
} else {
|
|
88
|
+
// Fallback: extract from URL
|
|
89
|
+
const url = new URL(req.url);
|
|
90
|
+
const pathSegments = url.pathname.split('/').filter(Boolean);
|
|
91
|
+
nextauthPath = pathSegments.length > 2 ? pathSegments.slice(2) : [];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const nextauthContext = {
|
|
95
|
+
params: Promise.resolve({ nextauth: nextauthPath })
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return await handler(req, nextauthContext);
|
|
99
|
+
} catch (error: any) {
|
|
100
|
+
console.error('[NextAuth POST] Error:', error);
|
|
101
|
+
// Return a proper error response instead of throwing
|
|
102
|
+
// This prevents CLIENT_FETCH_ERROR in NextAuth
|
|
103
|
+
return new Response(
|
|
104
|
+
JSON.stringify({
|
|
105
|
+
error: error.message || 'Authentication error',
|
|
106
|
+
code: 'AUTH_ERROR'
|
|
107
|
+
}),
|
|
108
|
+
{
|
|
109
|
+
status: 500,
|
|
110
|
+
headers: { 'Content-Type': 'application/json' }
|
|
111
|
+
}
|
|
112
|
+
);
|
|
75
113
|
}
|
|
76
|
-
|
|
77
|
-
const nextauthContext = {
|
|
78
|
-
params: Promise.resolve({ nextauth: nextauthPath })
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
return handler(req, nextauthContext);
|
|
82
114
|
}
|
|
83
115
|
|
package/src/api/router.ts
CHANGED
|
@@ -44,6 +44,18 @@ export async function handleUsersApi(
|
|
|
44
44
|
['session', 'signin', 'signout', 'callback', 'csrf', 'providers', 'error'].includes(route);
|
|
45
45
|
|
|
46
46
|
if (isNextAuthRoute) {
|
|
47
|
+
// Ensure authOptions are provided before handling NextAuth routes
|
|
48
|
+
// Check both config.authOptions and ensure it's not undefined/null
|
|
49
|
+
if (!config.authOptions) {
|
|
50
|
+
console.error('[UsersApiRouter] authOptions not provided for NextAuth route');
|
|
51
|
+
console.error('[UsersApiRouter] Config keys:', Object.keys(config));
|
|
52
|
+
console.error('[UsersApiRouter] Config:', JSON.stringify(config, null, 2));
|
|
53
|
+
return NextResponse.json(
|
|
54
|
+
{ error: 'Authentication not configured', debug: 'authOptions missing from config' },
|
|
55
|
+
{ status: 500 }
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
47
59
|
// This is a NextAuth route - use the path as nextauth segments
|
|
48
60
|
// For /api/auth, path is [] -> nextauthPath is []
|
|
49
61
|
// For /api/auth/session, path is ['session'] -> nextauthPath is ['session']
|