(null);
const [newUser, setNewUser] = useState({ email: "", name: "", familyId: "", role: "caregiver" });
useEffect(() => {
@@ -98,6 +99,26 @@ export default function AdminUsers() {
}
};
+ const handleSetPassword = async (userId: string, password: string) => {
+ if (!password) return;
+ try {
+ const res = await fetch("/api/admin/users", {
+ method: "PATCH",
+ headers: {
+ Authorization: `Bearer ${localStorage.getItem("admin_token")}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ userId, password }),
+ });
+ if (res.ok) {
+ fetchUsers();
+ setShowPassword(null);
+ }
+ } catch (err) {
+ console.error("Failed to set password:", err);
+ }
+ };
+
const filteredUsers = users.filter((u) =>
u.email.toLowerCase().includes(search.toLowerCase()) ||
(u.name || "").toLowerCase().includes(search.toLowerCase())
@@ -190,9 +211,19 @@ export default function AdminUsers() {
| {user.familyName || "-"} |
{user.hasPassword ? (
- ✓ Set
+
) : (
- Not set
+
)}
|
@@ -214,6 +245,38 @@ export default function AdminUsers() {
No users found
)}
+
+ {/* Password Modal */}
+ {showPassword && (
+
+
+ Set Password
+
+
+
+
+
+
+
+ )}
);
}
\ No newline at end of file
diff --git a/src/app/api/admin/users/route.ts b/src/app/api/admin/users/route.ts
index e88f3d0..80b3307 100644
--- a/src/app/api/admin/users/route.ts
+++ b/src/app/api/admin/users/route.ts
@@ -87,6 +87,47 @@ export async function POST(request: Request) {
}
}
+// Update user password or other fields
+export async function PATCH(request: Request) {
+ try {
+ const authHeader = request.headers.get("authorization");
+ if (!authHeader || !authHeader.startsWith("Bearer ")) {
+ return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
+ }
+
+ const body = await request.json();
+ const { userId, password } = body;
+
+ if (!userId) {
+ return NextResponse.json({ error: "userId required" }, { status: 400 });
+ }
+
+ // Simple hash function
+ function hashPassword(pwd: string): string {
+ let hash = 0;
+ for (let i = 0; i < pwd.length; i++) {
+ const char = pwd.charCodeAt(i);
+ hash = ((hash << 5) - hash) + char;
+ hash = hash & hash;
+ }
+ return "hash_" + hash.toString(16);
+ }
+
+ if (password) {
+ const passwordHash = hashPassword(password);
+ await sql`
+ UPDATE users SET password_hash = ${passwordHash}, password_updated_at = NOW(), updated_at = NOW()
+ WHERE id = ${userId}
+ `;
+ }
+
+ return NextResponse.json({ success: true });
+ } catch (error) {
+ console.error("Admin password update error:", error);
+ return NextResponse.json({ error: String(error) }, { status: 500 });
+ }
+}
+
// Remove user from family
export async function DELETE(request: Request) {
try {
|