update user login baru dan hakakses
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// server/api/users/[id].patch.ts
|
||||
// Update user data
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
import { join } from 'path';
|
||||
import { existsSync } from 'fs';
|
||||
|
||||
const getDbPath = () => {
|
||||
const dbDir = join(process.cwd(), 'data');
|
||||
return join(dbDir, 'users.db');
|
||||
};
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const userId = getRouterParam(event, 'id');
|
||||
const body = await readBody(event);
|
||||
|
||||
console.log(`🔄 Update user endpoint called for ID: ${userId}`);
|
||||
|
||||
if (!userId) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "User ID is required",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const dbPath = getDbPath();
|
||||
|
||||
if (!existsSync(dbPath)) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: "Database not found",
|
||||
});
|
||||
}
|
||||
|
||||
const db = new Database(dbPath);
|
||||
|
||||
// Check if user exists
|
||||
const existingUser = db.prepare('SELECT * FROM users WHERE id = ?').get(userId) as any;
|
||||
|
||||
if (!existingUser) {
|
||||
db.close();
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: "User not found",
|
||||
});
|
||||
}
|
||||
|
||||
// Prepare update fields
|
||||
const updateFields: string[] = [];
|
||||
const updateValues: any[] = [];
|
||||
|
||||
if (body.namaLengkap !== undefined) {
|
||||
updateFields.push('namaLengkap = ?');
|
||||
updateValues.push(body.namaLengkap);
|
||||
}
|
||||
if (body.tipeUser !== undefined) {
|
||||
updateFields.push('tipeUser = ?');
|
||||
updateValues.push(body.tipeUser);
|
||||
}
|
||||
if (body.lastLogin !== undefined) {
|
||||
updateFields.push('lastLogin = ?');
|
||||
updateValues.push(body.lastLogin);
|
||||
}
|
||||
if (body.roles !== undefined) {
|
||||
updateFields.push('roles = ?');
|
||||
updateValues.push(JSON.stringify(Array.isArray(body.roles) ? body.roles : []));
|
||||
}
|
||||
if (body.realmRoles !== undefined) {
|
||||
updateFields.push('realmRoles = ?');
|
||||
updateValues.push(JSON.stringify(Array.isArray(body.realmRoles) ? body.realmRoles : []));
|
||||
}
|
||||
if (body.accountRoles !== undefined) {
|
||||
updateFields.push('accountRoles = ?');
|
||||
updateValues.push(JSON.stringify(Array.isArray(body.accountRoles) ? body.accountRoles : []));
|
||||
}
|
||||
if (body.resourceRoles !== undefined) {
|
||||
updateFields.push('resourceRoles = ?');
|
||||
updateValues.push(JSON.stringify(Array.isArray(body.resourceRoles) ? body.resourceRoles : []));
|
||||
}
|
||||
if (body.groups !== undefined) {
|
||||
updateFields.push('groups = ?');
|
||||
updateValues.push(JSON.stringify(Array.isArray(body.groups) ? body.groups : []));
|
||||
}
|
||||
|
||||
if (updateFields.length === 0) {
|
||||
db.close();
|
||||
return { success: true, message: 'No fields to update' };
|
||||
}
|
||||
|
||||
// Add updatedAt
|
||||
updateFields.push('updatedAt = strftime(\'%s\', \'now\')');
|
||||
updateValues.push(userId);
|
||||
|
||||
// Execute update
|
||||
const sql = `UPDATE users SET ${updateFields.join(', ')} WHERE id = ?`;
|
||||
db.prepare(sql).run(...updateValues);
|
||||
|
||||
db.close();
|
||||
|
||||
console.log(`✅ User updated: ${userId}`);
|
||||
return { success: true, message: 'User updated successfully' };
|
||||
} catch (error: any) {
|
||||
console.error("❌ Error updating user:", error);
|
||||
throw createError({
|
||||
statusCode: error.statusCode || 500,
|
||||
statusMessage: error.message || "Failed to update user",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user