sarransh commited on
Commit
29fac62
·
verified ·
1 Parent(s): f596987

Add Setup Page for User Creation

Browse files
Files changed (2) hide show
  1. src/app/actions/setup.ts +48 -0
  2. src/app/setup/page.tsx +60 -0
src/app/actions/setup.ts ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "use server";
2
+
3
+ import bcrypt from "bcryptjs";
4
+ import { prisma } from "@/lib/prisma";
5
+
6
+ export async function createAdminUser(email: string, password: string) {
7
+ if (!email || !password) {
8
+ return { success: false, error: "Missing email or password" };
9
+ }
10
+
11
+ try {
12
+ console.log(`[SETUP] Creating user: ${email}`);
13
+
14
+ // 1. Check if user exists
15
+ const existing = await prisma.user.findFirst({
16
+ where: { email }
17
+ });
18
+
19
+ if (existing) {
20
+ console.log(`[SETUP] User existing: ${existing.id}`);
21
+ return { success: false, error: `User with email ${email} already exists` };
22
+ }
23
+
24
+ // 2. Hash password
25
+ const passwordHash = await bcrypt.hash(password, 10);
26
+ console.log(`[SETUP] Hashed password: ${passwordHash.substring(0, 10)}...`);
27
+
28
+ // 3. Create user
29
+ const newUser = await prisma.user.create({
30
+ data: {
31
+ name: "Admin User",
32
+ email,
33
+ passwordHash,
34
+ role: "ADMIN"
35
+ }
36
+ });
37
+
38
+ console.log(`[SETUP] Success! User ID: ${newUser.id}`);
39
+ return { success: true };
40
+
41
+ } catch (error: any) {
42
+ console.error("[SETUP] Error:", error);
43
+ return {
44
+ success: false,
45
+ error: error.message || String(error)
46
+ };
47
+ }
48
+ }
src/app/setup/page.tsx ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+ import { createAdminUser } from "@/app/actions/setup";
5
+
6
+ export default function SetupPage() {
7
+ const [status, setStatus] = useState<string>("");
8
+ const [email, setEmail] = useState("");
9
+ const [password, setPassword] = useState("");
10
+
11
+ const handleCreate = async () => {
12
+ setStatus("Creating admin user...");
13
+ try {
14
+ const res = await createAdminUser(email, password);
15
+ if (res.success) {
16
+ setStatus(`✅ Success! User ${email} created. You can now login.`);
17
+ } else {
18
+ setStatus(`❌ Error: ${res.error}`);
19
+ }
20
+ } catch (e: any) {
21
+ setStatus(`❌ Exception: ${e.message}`);
22
+ }
23
+ };
24
+
25
+ return (
26
+ <div className="p-8 max-w-md mx-auto space-y-4">
27
+ <h1 className="text-2xl font-bold">System Setup</h1>
28
+
29
+ <div className="space-y-2">
30
+ <input
31
+ className="border p-2 w-full rounded"
32
+ placeholder="Admin Email"
33
+ value={email}
34
+ onChange={e => setEmail(e.target.value)}
35
+ />
36
+ <input
37
+ className="border p-2 w-full rounded"
38
+ placeholder="Password"
39
+ type="password"
40
+ value={password}
41
+ onChange={e => setPassword(e.target.value)}
42
+ />
43
+ <button
44
+ onClick={handleCreate}
45
+ className="bg-blue-600 text-white px-4 py-2 rounded w-full hover:bg-blue-700"
46
+ >
47
+ Create Admin User
48
+ </button>
49
+ </div>
50
+
51
+ <div className="p-4 bg-gray-100 rounded min-h-[100px] font-mono text-sm whitespace-pre-wrap">
52
+ {status || "Waiting for input..."}
53
+ </div>
54
+
55
+ <div className="text-sm text-gray-500">
56
+ <p>After creating user, go to <a href="/auth/signin" className="text-blue-500 underline">Sign In</a></p>
57
+ </div>
58
+ </div>
59
+ );
60
+ }