@jhits/plugin-users 0.0.6 → 0.0.8
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/api/auth.d.ts +24 -0
- package/dist/api/auth.d.ts.map +1 -0
- package/dist/api/auth.js +69 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +7 -0
- package/dist/api/router.d.ts +18 -0
- package/dist/api/router.d.ts.map +1 -0
- package/dist/api/router.js +80 -0
- package/dist/api/users.d.ts +32 -0
- package/dist/api/users.d.ts.map +1 -0
- package/dist/api/users.js +138 -0
- package/dist/api-server.d.ts +9 -0
- package/dist/api-server.d.ts.map +1 -0
- package/dist/api-server.js +9 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.server.d.ts +12 -0
- package/dist/index.server.d.ts.map +1 -0
- package/dist/index.server.js +10 -0
- package/dist/views/UserManagement.d.ts +4 -0
- package/dist/views/UserManagement.d.ts.map +1 -0
- package/dist/views/UserManagement.js +94 -0
- package/package.json +3 -3
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Users - Auth API Handler
|
|
3
|
+
* NextAuth API route handler for App Router
|
|
4
|
+
*
|
|
5
|
+
* This is a core plugin that provides authentication functionality.
|
|
6
|
+
* authOptions should be passed from the client app (typically from @jhits/dashboard/lib/auth)
|
|
7
|
+
*/
|
|
8
|
+
import type { NextAuthOptions } from 'next-auth';
|
|
9
|
+
/**
|
|
10
|
+
* Initialize the auth handler with authOptions
|
|
11
|
+
* This should be called by the client app before handling auth requests
|
|
12
|
+
*/
|
|
13
|
+
export declare function initAuthHandler(authOptions: NextAuthOptions): void;
|
|
14
|
+
export declare function GET(req: any, context?: {
|
|
15
|
+
params?: Promise<{
|
|
16
|
+
nextauth?: string[];
|
|
17
|
+
}>;
|
|
18
|
+
}): Promise<any>;
|
|
19
|
+
export declare function POST(req: any, context?: {
|
|
20
|
+
params?: Promise<{
|
|
21
|
+
nextauth?: string[];
|
|
22
|
+
}>;
|
|
23
|
+
}): Promise<any>;
|
|
24
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/api/auth.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAKjD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,eAAe,QAE3D;AAiBD,wBAAsB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAC;QAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,gBAsB1F;AAED,wBAAsB,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAC;QAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,gBAoB3F"}
|
package/dist/api/auth.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Users - Auth API Handler
|
|
3
|
+
* NextAuth API route handler for App Router
|
|
4
|
+
*
|
|
5
|
+
* This is a core plugin that provides authentication functionality.
|
|
6
|
+
* authOptions should be passed from the client app (typically from @jhits/dashboard/lib/auth)
|
|
7
|
+
*/
|
|
8
|
+
import NextAuth from 'next-auth';
|
|
9
|
+
// Store authOptions - will be initialized by the client app
|
|
10
|
+
let authOptionsInstance = null;
|
|
11
|
+
/**
|
|
12
|
+
* Initialize the auth handler with authOptions
|
|
13
|
+
* This should be called by the client app before handling auth requests
|
|
14
|
+
*/
|
|
15
|
+
export function initAuthHandler(authOptions) {
|
|
16
|
+
authOptionsInstance = authOptions;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Get the NextAuth handler
|
|
20
|
+
* Throws an error if authOptions haven't been initialized
|
|
21
|
+
*/
|
|
22
|
+
function getHandler() {
|
|
23
|
+
if (!authOptionsInstance) {
|
|
24
|
+
throw new Error('Auth handler not initialized. Call initAuthHandler() with authOptions from @jhits/dashboard/lib/auth');
|
|
25
|
+
}
|
|
26
|
+
return NextAuth(authOptionsInstance);
|
|
27
|
+
}
|
|
28
|
+
// Export handlers - NextAuth handler works for both GET and POST
|
|
29
|
+
// NextAuth expects the route to be /api/auth/[...nextauth] with path segments in context.params.nextauth
|
|
30
|
+
export async function GET(req, context) {
|
|
31
|
+
const handler = getHandler();
|
|
32
|
+
// Use context if provided, otherwise extract from URL
|
|
33
|
+
let nextauthPath = [];
|
|
34
|
+
if (context?.params) {
|
|
35
|
+
const resolvedParams = await context.params;
|
|
36
|
+
nextauthPath = resolvedParams.nextauth || [];
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
// Fallback: extract from URL
|
|
40
|
+
const url = new URL(req.url);
|
|
41
|
+
const pathSegments = url.pathname.split('/').filter(Boolean);
|
|
42
|
+
// Path: /api/auth/session -> segments: ['api', 'auth', 'session'] -> nextauth: ['session']
|
|
43
|
+
nextauthPath = pathSegments.length > 2 ? pathSegments.slice(2) : [];
|
|
44
|
+
}
|
|
45
|
+
// Create context with nextauth parameter (NextAuth expects this)
|
|
46
|
+
const nextauthContext = {
|
|
47
|
+
params: Promise.resolve({ nextauth: nextauthPath })
|
|
48
|
+
};
|
|
49
|
+
return handler(req, nextauthContext);
|
|
50
|
+
}
|
|
51
|
+
export async function POST(req, context) {
|
|
52
|
+
const handler = getHandler();
|
|
53
|
+
// Use context if provided, otherwise extract from URL
|
|
54
|
+
let nextauthPath = [];
|
|
55
|
+
if (context?.params) {
|
|
56
|
+
const resolvedParams = await context.params;
|
|
57
|
+
nextauthPath = resolvedParams.nextauth || [];
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Fallback: extract from URL
|
|
61
|
+
const url = new URL(req.url);
|
|
62
|
+
const pathSegments = url.pathname.split('/').filter(Boolean);
|
|
63
|
+
nextauthPath = pathSegments.length > 2 ? pathSegments.slice(2) : [];
|
|
64
|
+
}
|
|
65
|
+
const nextauthContext = {
|
|
66
|
+
params: Promise.resolve({ nextauth: nextauthPath })
|
|
67
|
+
};
|
|
68
|
+
return handler(req, nextauthContext);
|
|
69
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAGpD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Users API Router
|
|
3
|
+
* Centralized API handler for all users plugin routes
|
|
4
|
+
*
|
|
5
|
+
* This router handles requests to:
|
|
6
|
+
* - /api/auth/[...nextauth] (NextAuth routes)
|
|
7
|
+
* - /api/users (user management routes)
|
|
8
|
+
* - /api/plugin-users/* (plugin-specific routes)
|
|
9
|
+
*/
|
|
10
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
11
|
+
import { UsersApiConfig } from './users';
|
|
12
|
+
/**
|
|
13
|
+
* Handle users API requests
|
|
14
|
+
* Routes requests to appropriate handlers based on path
|
|
15
|
+
* Handles both NextAuth routes (/api/auth) and user management routes (/api/users)
|
|
16
|
+
*/
|
|
17
|
+
export declare function handleUsersApi(req: NextRequest, path: string[], config: UsersApiConfig): Promise<NextResponse>;
|
|
18
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/api/router.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AAEH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAIzC;;;;GAIG;AACH,wBAAsB,cAAc,CAChC,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,EAAE,cAAc,GACvB,OAAO,CAAC,YAAY,CAAC,CAwEvB"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use server';
|
|
2
|
+
/**
|
|
3
|
+
* Plugin Users API Router
|
|
4
|
+
* Centralized API handler for all users plugin routes
|
|
5
|
+
*
|
|
6
|
+
* This router handles requests to:
|
|
7
|
+
* - /api/auth/[...nextauth] (NextAuth routes)
|
|
8
|
+
* - /api/users (user management routes)
|
|
9
|
+
* - /api/plugin-users/* (plugin-specific routes)
|
|
10
|
+
*/
|
|
11
|
+
import { NextResponse } from 'next/server';
|
|
12
|
+
import { GET_USERS, POST_USERS, PATCH_USER, DELETE_USER } from './users';
|
|
13
|
+
import { GET as AuthGET, POST as AuthPOST, initAuthHandler } from './auth';
|
|
14
|
+
/**
|
|
15
|
+
* Handle users API requests
|
|
16
|
+
* Routes requests to appropriate handlers based on path
|
|
17
|
+
* Handles both NextAuth routes (/api/auth) and user management routes (/api/users)
|
|
18
|
+
*/
|
|
19
|
+
export async function handleUsersApi(req, path, config) {
|
|
20
|
+
const method = req.method;
|
|
21
|
+
const route = path && path.length > 0 ? path[0] : '';
|
|
22
|
+
try {
|
|
23
|
+
// Initialize auth handler if authOptions are provided
|
|
24
|
+
if (config.authOptions) {
|
|
25
|
+
initAuthHandler(config.authOptions);
|
|
26
|
+
}
|
|
27
|
+
// Route: /api/auth/[...nextauth] - NextAuth routes
|
|
28
|
+
// When routed from /api/auth, the path contains the nextauth segments
|
|
29
|
+
// Path structure: /api/auth/session -> path: ['session']
|
|
30
|
+
// Path structure: /api/auth/signin -> path: ['signin']
|
|
31
|
+
// Path structure: /api/auth -> path: [] (empty)
|
|
32
|
+
// We handle this by checking if route is empty (root /api/auth) or if it's a known NextAuth route
|
|
33
|
+
const isNextAuthRoute = route === '' ||
|
|
34
|
+
['session', 'signin', 'signout', 'callback', 'csrf', 'providers', 'error'].includes(route);
|
|
35
|
+
if (isNextAuthRoute) {
|
|
36
|
+
// This is a NextAuth route - use the path as nextauth segments
|
|
37
|
+
// For /api/auth, path is [] -> nextauthPath is []
|
|
38
|
+
// For /api/auth/session, path is ['session'] -> nextauthPath is ['session']
|
|
39
|
+
const nextauthPath = path;
|
|
40
|
+
// Create NextAuth context
|
|
41
|
+
const nextauthContext = {
|
|
42
|
+
params: Promise.resolve({ nextauth: nextauthPath })
|
|
43
|
+
};
|
|
44
|
+
if (method === 'GET') {
|
|
45
|
+
return await AuthGET(req, nextauthContext);
|
|
46
|
+
}
|
|
47
|
+
if (method === 'POST') {
|
|
48
|
+
return await AuthPOST(req, nextauthContext);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Route: /api/users or /api/plugin-users/users (user management)
|
|
52
|
+
if (route === 'users') {
|
|
53
|
+
if (path.length === 1) {
|
|
54
|
+
// /api/users or /api/plugin-users/users
|
|
55
|
+
if (method === 'GET') {
|
|
56
|
+
return await GET_USERS(req, config);
|
|
57
|
+
}
|
|
58
|
+
if (method === 'POST') {
|
|
59
|
+
return await POST_USERS(req, config);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (path.length === 2) {
|
|
63
|
+
// /api/users/[id] or /api/plugin-users/users/[id]
|
|
64
|
+
const userId = path[1];
|
|
65
|
+
if (method === 'PATCH') {
|
|
66
|
+
return await PATCH_USER(req, userId, config);
|
|
67
|
+
}
|
|
68
|
+
if (method === 'DELETE') {
|
|
69
|
+
return await DELETE_USER(req, userId, config);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Method not allowed
|
|
74
|
+
return NextResponse.json({ error: `Method ${method} not allowed for route: ${route || '/'}` }, { status: 405 });
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.error('[UsersApiRouter] Error:', error);
|
|
78
|
+
return NextResponse.json({ error: error.message || 'Internal server error' }, { status: 500 });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Users - Users API Handler
|
|
3
|
+
* Handles user management API routes
|
|
4
|
+
*/
|
|
5
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
6
|
+
export interface UsersApiConfig {
|
|
7
|
+
/** MongoDB client promise */
|
|
8
|
+
getDb: () => Promise<{
|
|
9
|
+
db: () => any;
|
|
10
|
+
}>;
|
|
11
|
+
/** Collection name (default: 'users') */
|
|
12
|
+
collectionName?: string;
|
|
13
|
+
/** NextAuth options for authentication routes */
|
|
14
|
+
authOptions?: any;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* GET /api/plugin-users/users - List all users
|
|
18
|
+
*/
|
|
19
|
+
export declare function GET_USERS(req: NextRequest, config: UsersApiConfig): Promise<NextResponse>;
|
|
20
|
+
/**
|
|
21
|
+
* POST /api/plugin-users/users - Create new user
|
|
22
|
+
*/
|
|
23
|
+
export declare function POST_USERS(req: NextRequest, config: UsersApiConfig): Promise<NextResponse>;
|
|
24
|
+
/**
|
|
25
|
+
* PATCH /api/plugin-users/users/[id] - Update user
|
|
26
|
+
*/
|
|
27
|
+
export declare function PATCH_USER(req: NextRequest, userId: string, config: UsersApiConfig): Promise<NextResponse>;
|
|
28
|
+
/**
|
|
29
|
+
* DELETE /api/plugin-users/users/[id] - Delete user
|
|
30
|
+
*/
|
|
31
|
+
export declare function DELETE_USER(req: NextRequest, userId: string, config: UsersApiConfig): Promise<NextResponse>;
|
|
32
|
+
//# sourceMappingURL=users.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"users.d.ts","sourceRoot":"","sources":["../../src/api/users.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIxD,MAAM,WAAW,cAAc;IAC3B,6BAA6B;IAC7B,KAAK,EAAE,MAAM,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,GAAG,CAAA;KAAE,CAAC,CAAC;IACxC,yCAAyC;IACzC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,iDAAiD;IACjD,WAAW,CAAC,EAAE,GAAG,CAAC;CACrB;AAED;;GAEG;AACH,wBAAsB,SAAS,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,YAAY,CAAC,CAmB/F;AAED;;GAEG;AACH,wBAAsB,UAAU,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,YAAY,CAAC,CAiDhG;AAED;;GAEG;AACH,wBAAsB,UAAU,CAC5B,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,cAAc,GACvB,OAAO,CAAC,YAAY,CAAC,CAiEvB;AAED;;GAEG;AACH,wBAAsB,WAAW,CAC7B,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,cAAc,GACvB,OAAO,CAAC,YAAY,CAAC,CA6BvB"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Users - Users API Handler
|
|
3
|
+
* Handles user management API routes
|
|
4
|
+
*/
|
|
5
|
+
import { NextResponse } from 'next/server';
|
|
6
|
+
import { ObjectId } from 'mongodb';
|
|
7
|
+
import bcrypt from 'bcryptjs';
|
|
8
|
+
/**
|
|
9
|
+
* GET /api/plugin-users/users - List all users
|
|
10
|
+
*/
|
|
11
|
+
export async function GET_USERS(req, config) {
|
|
12
|
+
try {
|
|
13
|
+
const dbConnection = await config.getDb();
|
|
14
|
+
const db = dbConnection.db();
|
|
15
|
+
const users = db.collection(config.collectionName || 'users');
|
|
16
|
+
// Exclude passwords for security
|
|
17
|
+
const userList = await users
|
|
18
|
+
.find({}, { projection: { password: 0 } })
|
|
19
|
+
.toArray();
|
|
20
|
+
return NextResponse.json(userList);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
console.error('[UsersAPI] GET error:', err);
|
|
24
|
+
return NextResponse.json({ error: 'Failed to fetch users', detail: err.message }, { status: 500 });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* POST /api/plugin-users/users - Create new user
|
|
29
|
+
*/
|
|
30
|
+
export async function POST_USERS(req, config) {
|
|
31
|
+
try {
|
|
32
|
+
const { email, name, role, password } = await req.json();
|
|
33
|
+
const dbConnection = await config.getDb();
|
|
34
|
+
const db = dbConnection.db();
|
|
35
|
+
const users = db.collection(config.collectionName || 'users');
|
|
36
|
+
// Validation
|
|
37
|
+
if (!password || password.length < 6) {
|
|
38
|
+
return NextResponse.json({ error: 'Password too short (minimum 6 characters)' }, { status: 400 });
|
|
39
|
+
}
|
|
40
|
+
const exists = await users.findOne({ email });
|
|
41
|
+
if (exists) {
|
|
42
|
+
return NextResponse.json({ error: 'User already exists' }, { status: 400 });
|
|
43
|
+
}
|
|
44
|
+
// Hash the password
|
|
45
|
+
const hashedPassword = await bcrypt.hash(password, 12);
|
|
46
|
+
// Save to DB
|
|
47
|
+
const result = await users.insertOne({
|
|
48
|
+
email,
|
|
49
|
+
name,
|
|
50
|
+
role: role || 'editor',
|
|
51
|
+
password: hashedPassword,
|
|
52
|
+
createdAt: new Date(),
|
|
53
|
+
});
|
|
54
|
+
return NextResponse.json({
|
|
55
|
+
_id: result.insertedId,
|
|
56
|
+
email,
|
|
57
|
+
name,
|
|
58
|
+
role: role || 'editor',
|
|
59
|
+
createdAt: new Date()
|
|
60
|
+
}, { status: 201 });
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
console.error('[UsersAPI] POST error:', err);
|
|
64
|
+
return NextResponse.json({ error: 'Failed to create user', detail: err.message }, { status: 500 });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* PATCH /api/plugin-users/users/[id] - Update user
|
|
69
|
+
*/
|
|
70
|
+
export async function PATCH_USER(req, userId, config) {
|
|
71
|
+
try {
|
|
72
|
+
const { role, name, password, currentPassword } = await req.json();
|
|
73
|
+
const dbConnection = await config.getDb();
|
|
74
|
+
const db = dbConnection.db();
|
|
75
|
+
const users = db.collection(config.collectionName || 'users');
|
|
76
|
+
// Prepare the update object
|
|
77
|
+
const updateData = { updatedAt: new Date() };
|
|
78
|
+
if (role)
|
|
79
|
+
updateData.role = role;
|
|
80
|
+
if (name)
|
|
81
|
+
updateData.name = name;
|
|
82
|
+
// Handle Password Update Logic
|
|
83
|
+
if (password) {
|
|
84
|
+
// Find the user first to verify identity
|
|
85
|
+
const user = await users.findOne({ _id: new ObjectId(userId) });
|
|
86
|
+
if (!user) {
|
|
87
|
+
return NextResponse.json({ error: 'User not found' }, { status: 404 });
|
|
88
|
+
}
|
|
89
|
+
// Safety Check: Verify current password before allowing change
|
|
90
|
+
if (!currentPassword) {
|
|
91
|
+
return NextResponse.json({ error: 'Current password is required' }, { status: 400 });
|
|
92
|
+
}
|
|
93
|
+
const isCorrect = await bcrypt.compare(currentPassword, user.password);
|
|
94
|
+
if (!isCorrect) {
|
|
95
|
+
return NextResponse.json({ error: 'Current password is incorrect' }, { status: 401 });
|
|
96
|
+
}
|
|
97
|
+
// Hash the NEW password
|
|
98
|
+
if (password.length < 6) {
|
|
99
|
+
return NextResponse.json({ error: 'New password too short (minimum 6 characters)' }, { status: 400 });
|
|
100
|
+
}
|
|
101
|
+
updateData.password = await bcrypt.hash(password, 12);
|
|
102
|
+
}
|
|
103
|
+
// Execute the Update
|
|
104
|
+
const result = await users.updateOne({ _id: new ObjectId(userId) }, { $set: updateData });
|
|
105
|
+
if (result.matchedCount === 0) {
|
|
106
|
+
return NextResponse.json({ error: 'User not found' }, { status: 404 });
|
|
107
|
+
}
|
|
108
|
+
return NextResponse.json({ message: 'Update successful' });
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
console.error('[UsersAPI] PATCH error:', err);
|
|
112
|
+
return NextResponse.json({ error: 'Update failed', detail: err.message }, { status: 500 });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* DELETE /api/plugin-users/users/[id] - Delete user
|
|
117
|
+
*/
|
|
118
|
+
export async function DELETE_USER(req, userId, config) {
|
|
119
|
+
try {
|
|
120
|
+
const dbConnection = await config.getDb();
|
|
121
|
+
const db = dbConnection.db();
|
|
122
|
+
const users = db.collection(config.collectionName || 'users');
|
|
123
|
+
// Safety check: Prevent deleting developer account
|
|
124
|
+
const user = await users.findOne({ _id: new ObjectId(userId) });
|
|
125
|
+
if (user?.role === 'dev') {
|
|
126
|
+
return NextResponse.json({ error: 'Cannot delete developer account' }, { status: 403 });
|
|
127
|
+
}
|
|
128
|
+
const result = await users.deleteOne({ _id: new ObjectId(userId) });
|
|
129
|
+
if (result.deletedCount === 0) {
|
|
130
|
+
return NextResponse.json({ error: 'User not found' }, { status: 404 });
|
|
131
|
+
}
|
|
132
|
+
return NextResponse.json({ message: 'User deleted' });
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
console.error('[UsersAPI] DELETE error:', err);
|
|
136
|
+
return NextResponse.json({ error: 'Delete failed', detail: err.message }, { status: 500 });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Users - Server-Only API Exports
|
|
3
|
+
* This file is only imported in server-side code (API routes)
|
|
4
|
+
*
|
|
5
|
+
* IMPORTANT: This file uses Node.js modules and should NEVER
|
|
6
|
+
* be imported in client-side code. Only use in server-side API routes.
|
|
7
|
+
*/
|
|
8
|
+
export * from './api';
|
|
9
|
+
//# sourceMappingURL=api-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-server.d.ts","sourceRoot":"","sources":["../src/api-server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,cAAc,OAAO,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin Users - Server-Only API Exports
|
|
3
|
+
* This file is only imported in server-side code (API routes)
|
|
4
|
+
*
|
|
5
|
+
* IMPORTANT: This file uses Node.js modules and should NEVER
|
|
6
|
+
* be imported in client-side code. Only use in server-side API routes.
|
|
7
|
+
*/
|
|
8
|
+
// Re-export everything from the API index
|
|
9
|
+
export * from './api';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,EAAE,CAAC,GAAG,CAQ/B,CAAC;AAEF,eAAe,KAAK,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import UserManagement from './views/UserManagement';
|
|
4
|
+
// User Management Plugin - Always enabled by default
|
|
5
|
+
export const Index = ({ subPath = [], siteId, locale: dashboardLocale = 'en' }) => {
|
|
6
|
+
return (_jsx("div", { className: "w-full h-full flex flex-col overflow-hidden", children: _jsx("div", { className: "flex-1 overflow-y-auto", children: _jsx(UserManagement, { locale: dashboardLocale }) }) }));
|
|
7
|
+
};
|
|
8
|
+
export default Index;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import 'server-only';
|
|
2
|
+
/**
|
|
3
|
+
* Plugin Users - Server-Only Entry Point
|
|
4
|
+
* This file exports only server-side API handlers
|
|
5
|
+
* Used by the dynamic plugin router via @jhits/plugin-users/server
|
|
6
|
+
*
|
|
7
|
+
* Note: This file is server-only (no 'use server' needed - that's only for Server Actions)
|
|
8
|
+
*/
|
|
9
|
+
export { handleUsersApi as handleApi } from './api/router';
|
|
10
|
+
export { handleUsersApi } from './api/router';
|
|
11
|
+
export type { UsersApiConfig } from './api/users';
|
|
12
|
+
//# sourceMappingURL=index.server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.server.d.ts","sourceRoot":"","sources":["../src/index.server.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAC;AAErB;;;;;;GAMG;AAEH,OAAO,EAAE,cAAc,IAAI,SAAS,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import 'server-only';
|
|
2
|
+
/**
|
|
3
|
+
* Plugin Users - Server-Only Entry Point
|
|
4
|
+
* This file exports only server-side API handlers
|
|
5
|
+
* Used by the dynamic plugin router via @jhits/plugin-users/server
|
|
6
|
+
*
|
|
7
|
+
* Note: This file is server-only (no 'use server' needed - that's only for Server Actions)
|
|
8
|
+
*/
|
|
9
|
+
export { handleUsersApi as handleApi } from './api/router';
|
|
10
|
+
export { handleUsersApi } from './api/router'; // Keep original export for backward compatibility
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserManagement.d.ts","sourceRoot":"","sources":["../../src/views/UserManagement.tsx"],"names":[],"mappings":"AAgBA,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,EAAE,MAAa,EAAE,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,2CA0T5E"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState, useEffect } from "react";
|
|
4
|
+
import { UserPlus, Shield, Trash2, Search, Loader2, X, Eye, EyeOff, Copy, Check, Calendar } from "lucide-react";
|
|
5
|
+
export default function UserManagement({ locale = 'en' }) {
|
|
6
|
+
const [users, setUsers] = useState([]);
|
|
7
|
+
const [loading, setLoading] = useState(true);
|
|
8
|
+
const [searchTerm, setSearchTerm] = useState("");
|
|
9
|
+
// Modal State
|
|
10
|
+
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
11
|
+
const [isCreating, setIsCreating] = useState(false);
|
|
12
|
+
const [showPassword, setShowPassword] = useState(false);
|
|
13
|
+
const [copied, setCopied] = useState(false);
|
|
14
|
+
// Form State
|
|
15
|
+
const [formData, setFormData] = useState({
|
|
16
|
+
name: "",
|
|
17
|
+
email: "",
|
|
18
|
+
password: "",
|
|
19
|
+
role: "editor"
|
|
20
|
+
});
|
|
21
|
+
const fetchUsers = async () => {
|
|
22
|
+
try {
|
|
23
|
+
setLoading(true);
|
|
24
|
+
const res = await fetch('/api/users');
|
|
25
|
+
const data = await res.json();
|
|
26
|
+
if (Array.isArray(data))
|
|
27
|
+
setUsers(data);
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
console.error("Failed to load users", err);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
setLoading(false);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
useEffect(() => { fetchUsers(); }, []);
|
|
37
|
+
const generateTempPassword = () => {
|
|
38
|
+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%";
|
|
39
|
+
let retVal = "";
|
|
40
|
+
for (let i = 0, n = charset.length; i < 10; ++i) {
|
|
41
|
+
retVal += charset.charAt(Math.floor(Math.random() * n));
|
|
42
|
+
}
|
|
43
|
+
setFormData({ ...formData, password: retVal });
|
|
44
|
+
};
|
|
45
|
+
const handleCreateUser = async (e) => {
|
|
46
|
+
e.preventDefault();
|
|
47
|
+
setIsCreating(true);
|
|
48
|
+
try {
|
|
49
|
+
const res = await fetch('/api/users', {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
headers: { 'Content-Type': 'application/json' },
|
|
52
|
+
body: JSON.stringify(formData)
|
|
53
|
+
});
|
|
54
|
+
if (res.ok) {
|
|
55
|
+
const newUser = await res.json();
|
|
56
|
+
setUsers([...users, newUser]);
|
|
57
|
+
setIsModalOpen(false);
|
|
58
|
+
setFormData({ name: "", email: "", password: "", role: "editor" });
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const err = await res.json();
|
|
62
|
+
alert(err.error || "Failed to create user");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
alert("Network error while creating user");
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
setIsCreating(false);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const copyToClipboard = () => {
|
|
73
|
+
navigator.clipboard.writeText(formData.password);
|
|
74
|
+
setCopied(true);
|
|
75
|
+
setTimeout(() => setCopied(false), 2000);
|
|
76
|
+
};
|
|
77
|
+
const handleDelete = async (userId, userRole) => {
|
|
78
|
+
if (userRole === 'dev')
|
|
79
|
+
return alert("Cannot delete developer account.");
|
|
80
|
+
if (!confirm("Are you sure you want to delete this user?"))
|
|
81
|
+
return;
|
|
82
|
+
try {
|
|
83
|
+
const res = await fetch(`/api/users/${userId}`, { method: 'DELETE' });
|
|
84
|
+
if (res.ok)
|
|
85
|
+
setUsers(users.filter(u => u._id !== userId));
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
alert("Error deleting user.");
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const filteredUsers = users.filter(user => user.name?.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
|
92
|
+
user.email?.toLowerCase().includes(searchTerm.toLowerCase()));
|
|
93
|
+
return (_jsxs("div", { className: "h-full w-full rounded-[2.5rem] bg-dashboard-card dark:bg-neutral-900 p-8 overflow-y-auto", children: [_jsxs("div", { className: "flex flex-col md:flex-row md:items-center justify-between gap-6 mb-8", children: [_jsxs("div", { children: [_jsx("h1", { className: "text-3xl font-black text-dashboard-text uppercase tracking-tighter mb-2", children: "User Management" }), _jsx("p", { className: "text-sm text-dashboard-text-secondary", children: "Manage who has access to the dashboard." })] }), _jsxs("button", { onClick: () => { setIsModalOpen(true); generateTempPassword(); }, className: "inline-flex items-center gap-2 px-6 py-3 bg-primary text-white rounded-full text-[10px] font-black uppercase tracking-widest hover:bg-primary/90 transition-all shadow-lg shadow-primary/20", children: [_jsx(UserPlus, { size: 16 }), "New User"] })] }), _jsxs("div", { className: "bg-dashboard-bg dark:bg-neutral-800/50 rounded-2xl sm:rounded-[2rem] border border-dashboard-border overflow-hidden", children: [_jsxs("div", { className: "p-4 sm:p-6 border-b border-dashboard-border flex items-center justify-between", children: [_jsxs("div", { className: "relative w-full sm:w-72", children: [_jsx(Search, { className: "absolute left-3 top-1/2 -translate-y-1/2 text-dashboard-text-secondary", size: 16 }), _jsx("input", { type: "text", placeholder: "Search users...", value: searchTerm, onChange: (e) => setSearchTerm(e.target.value), className: "w-full pl-10 pr-4 py-2.5 bg-dashboard-card border border-dashboard-border rounded-lg text-sm outline-none focus:ring-2 focus:ring-primary/20 transition-all text-dashboard-text placeholder:text-dashboard-text-secondary" })] }), loading && _jsx(Loader2, { className: "animate-spin text-primary ml-4", size: 20 })] }), _jsx("div", { className: "hidden md:block overflow-x-auto", children: _jsxs("table", { className: "w-full text-left border-collapse", children: [_jsx("thead", { children: _jsxs("tr", { className: "text-[11px] font-bold text-dashboard-text-secondary uppercase tracking-widest border-b border-dashboard-border", children: [_jsx("th", { className: "px-8 py-4", children: "User" }), _jsx("th", { className: "px-8 py-4", children: "Role" }), _jsx("th", { className: "px-8 py-4", children: "Since" }), _jsx("th", { className: "px-8 py-4 text-right", children: "Actions" })] }) }), _jsx("tbody", { className: "divide-y divide-dashboard-border", children: filteredUsers.map((user) => (_jsxs("tr", { className: "group hover:bg-dashboard-card transition-colors", children: [_jsx("td", { className: "px-8 py-5", children: _jsxs("div", { className: "flex items-center gap-3", children: [_jsx("div", { className: "w-10 h-10 rounded-full bg-primary/10 text-primary flex items-center justify-center font-bold border border-primary/20", children: user.name?.[0] || 'U' }), _jsxs("div", { children: [_jsx("p", { className: "font-bold text-dashboard-text text-sm", children: user.name }), _jsx("p", { className: "text-xs text-dashboard-text-secondary", children: user.email })] })] }) }), _jsx("td", { className: "px-8 py-5", children: _jsxs("span", { className: `inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider ${user.role === 'dev' ? 'bg-primary text-white' : 'bg-dashboard-bg text-dashboard-text'}`, children: [_jsx(Shield, { size: 10 }), " ", user.role] }) }), _jsx("td", { className: "px-8 py-5 text-sm text-dashboard-text-secondary", children: user.createdAt ? new Date(user.createdAt).toLocaleDateString() : '-' }), _jsx("td", { className: "px-8 py-5 text-right", children: _jsx("button", { onClick: () => handleDelete(user._id, user.role), className: "p-2 text-dashboard-text-secondary hover:text-red-500 transition-colors disabled:opacity-0", disabled: user.role === 'dev', children: _jsx(Trash2, { size: 16 }) }) })] }, user._id))) })] }) }), _jsx("div", { className: "md:hidden divide-y divide-dashboard-border", children: filteredUsers.map((user) => (_jsxs("div", { className: "p-5 space-y-4", children: [_jsxs("div", { className: "flex justify-between items-start", children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx("div", { className: "w-10 h-10 rounded-full bg-primary/10 text-primary flex items-center justify-center font-bold border border-primary/20", children: user.name?.[0] || 'U' }), _jsxs("div", { className: "min-w-0", children: [_jsx("p", { className: "font-bold text-dashboard-text text-sm truncate", children: user.name }), _jsx("p", { className: "text-[11px] text-dashboard-text-secondary truncate", children: user.email })] })] }), _jsx("button", { onClick: () => handleDelete(user._id, user.role), className: `p-2 rounded-lg bg-red-50 dark:bg-red-950/20 text-red-400 ${user.role === 'dev' ? 'hidden' : 'block'}`, children: _jsx(Trash2, { size: 16 }) })] }), _jsxs("div", { className: "flex items-center justify-between pt-2 border-t border-dashboard-border", children: [_jsxs("span", { className: `inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-[9px] font-black uppercase tracking-wider ${user.role === 'dev' ? 'bg-primary text-white' : 'bg-dashboard-bg text-dashboard-text'}`, children: [_jsx(Shield, { size: 10 }), " ", user.role] }), _jsxs("div", { className: "flex items-center gap-1.5 text-[10px] text-dashboard-text-secondary font-medium", children: [_jsx(Calendar, { size: 12 }), user.createdAt ? new Date(user.createdAt).toLocaleDateString() : '-'] })] })] }, user._id))) }), filteredUsers.length === 0 && !loading && (_jsx("div", { className: "p-12 text-center text-dashboard-text-secondary text-sm", children: "No users found matching your search." }))] }), isModalOpen && (_jsx("div", { className: "fixed inset-0 z-50 flex items-end sm:items-center justify-center p-0 sm:p-4 bg-black/40 backdrop-blur-sm animate-in fade-in duration-200", children: _jsxs("div", { className: "bg-dashboard-card w-full max-w-md rounded-t-[2.5rem] sm:rounded-[2.5rem] shadow-2xl overflow-hidden animate-in slide-in-from-bottom sm:zoom-in-95 duration-300 max-h-[90vh] overflow-y-auto border border-dashboard-border", children: [_jsxs("div", { className: "p-6 sm:p-8 bg-primary text-white flex justify-between items-center sticky top-0 z-10", children: [_jsx("h2", { className: "text-xl sm:text-2xl font-black uppercase tracking-tighter", children: "New User" }), _jsx("button", { onClick: () => setIsModalOpen(false), className: "hover:rotate-90 transition-transform p-2", children: _jsx(X, { size: 24 }) })] }), _jsxs("form", { onSubmit: handleCreateUser, className: "p-6 sm:p-8 space-y-5", children: [_jsxs("div", { className: "space-y-2", children: [_jsx("label", { className: "text-xs font-bold text-dashboard-text-secondary uppercase tracking-widest ml-1", children: "Name" }), _jsx("input", { required: true, className: "w-full px-5 py-3 bg-dashboard-bg border border-dashboard-border rounded-2xl focus:ring-2 focus:ring-primary outline-none text-sm transition-all text-dashboard-text", value: formData.name, onChange: e => setFormData({ ...formData, name: e.target.value }), placeholder: "e.g. John Doe" })] }), _jsxs("div", { className: "space-y-2", children: [_jsx("label", { className: "text-xs font-bold text-dashboard-text-secondary uppercase tracking-widest ml-1", children: "Email Address" }), _jsx("input", { required: true, type: "email", className: "w-full px-5 py-3 bg-dashboard-bg border border-dashboard-border rounded-2xl focus:ring-2 focus:ring-primary outline-none text-sm transition-all text-dashboard-text", value: formData.email, onChange: e => setFormData({ ...formData, email: e.target.value }), placeholder: "email@example.com" })] }), _jsxs("div", { className: "space-y-2", children: [_jsx("label", { className: "text-xs font-bold text-dashboard-text-secondary uppercase tracking-widest ml-1", children: "Temporary Password" }), _jsxs("div", { className: "relative", children: [_jsx("input", { required: true, type: showPassword ? "text" : "password", className: "w-full px-5 py-3 bg-dashboard-bg border border-dashboard-border rounded-2xl focus:ring-2 focus:ring-primary outline-none text-sm transition-all font-mono text-dashboard-text", value: formData.password, onChange: e => setFormData({ ...formData, password: e.target.value }) }), _jsxs("div", { className: "absolute right-3 top-1/2 -translate-y-1/2 flex items-center gap-1", children: [_jsx("button", { type: "button", onClick: () => setShowPassword(!showPassword), className: "p-1.5 text-dashboard-text-secondary hover:text-primary", children: showPassword ? _jsx(EyeOff, { size: 16 }) : _jsx(Eye, { size: 16 }) }), _jsx("button", { type: "button", onClick: copyToClipboard, className: "p-1.5 text-dashboard-text-secondary hover:text-primary", children: copied ? _jsx(Check, { size: 16, className: "text-emerald-500" }) : _jsx(Copy, { size: 16 }) })] })] }), _jsx("button", { type: "button", onClick: generateTempPassword, className: "text-[10px] text-primary font-bold uppercase tracking-wider hover:underline ml-1", children: "Generate new password" })] }), _jsxs("div", { className: "space-y-2", children: [_jsx("label", { className: "text-xs font-bold text-dashboard-text-secondary uppercase tracking-widest ml-1", children: "Role" }), _jsxs("div", { className: "relative", children: [_jsxs("select", { className: "w-full px-5 py-3 bg-dashboard-bg border border-dashboard-border rounded-2xl focus:ring-2 focus:ring-primary outline-none text-sm appearance-none cursor-pointer text-dashboard-text", value: formData.role, onChange: e => setFormData({ ...formData, role: e.target.value }), children: [_jsx("option", { value: "editor", children: "Editor (Content only)" }), _jsx("option", { value: "dev", children: "Developer" }), _jsx("option", { value: "admin", children: "Admin (All settings)" })] }), _jsx("div", { className: "absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-dashboard-text-secondary", children: _jsx(Shield, { size: 16 }) })] })] }), _jsx("button", { type: "submit", disabled: isCreating, className: "w-full py-4 bg-primary text-white rounded-full text-[10px] font-black uppercase tracking-widest shadow-lg shadow-primary/20 hover:bg-primary/90 transition-all mt-4 flex items-center justify-center gap-2", children: isCreating ? _jsx(Loader2, { className: "animate-spin", size: 20 }) : "Add User" })] })] }) }))] }));
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jhits/plugin-users",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "User management and authentication plugin for the JHITS ecosystem",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@jhits/plugin-core": "^0.0.2",
|
|
22
21
|
"bcrypt": "^6.0.0",
|
|
23
22
|
"bcryptjs": "^3.0.3",
|
|
24
23
|
"framer-motion": "^12.34.0",
|
|
@@ -29,7 +28,8 @@
|
|
|
29
28
|
"@types/bcryptjs": "^3.0.0",
|
|
30
29
|
"@types/node": "^25.2.3",
|
|
31
30
|
"@types/react": "^19.2.14",
|
|
32
|
-
"@types/react-dom": "^19.2.3"
|
|
31
|
+
"@types/react-dom": "^19.2.3",
|
|
32
|
+
"@jhits/plugin-core": "0.0.2"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"next": ">=15.0.0",
|