create-zerra-app 1.1.0 ā 1.2.0
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/index.js +66 -13
- package/package.json +1 -1
- package/templates/js-auth/api/auth/login.js +15 -0
- package/templates/js-auth/api/auth/register.js +24 -0
- package/templates/js-auth/api/protected/_middleware.js +17 -0
- package/templates/js-auth/api/protected/secret.js +6 -0
- package/templates/js-auth/package.json +6 -0
- package/templates/js-auth/services/auth.js +11 -0
- package/templates/js-base/package.json +1 -1
package/index.js
CHANGED
|
@@ -28,35 +28,67 @@ program
|
|
|
28
28
|
},
|
|
29
29
|
{
|
|
30
30
|
type: "confirm",
|
|
31
|
-
name: "
|
|
32
|
-
message: "
|
|
33
|
-
default: true,
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
type: "confirm",
|
|
37
|
-
name: "initGit",
|
|
38
|
-
message: "Would you like to initialize a new git repository?",
|
|
31
|
+
name: "includeAuth",
|
|
32
|
+
message: "Include Authentication Starter (JWT + Bcrypt)?",
|
|
39
33
|
default: true,
|
|
40
34
|
},
|
|
41
35
|
{
|
|
42
36
|
type: "checkbox",
|
|
43
37
|
name: "features",
|
|
44
|
-
message: "
|
|
38
|
+
message: "Select advanced features to enable:",
|
|
45
39
|
choices: [
|
|
46
40
|
{ name: "Beautiful Request Logging", value: "logging", checked: true },
|
|
47
41
|
{ name: "Dynamic Routing ([id].js)", value: "dynamicRouting", checked: true },
|
|
48
42
|
{ name: "File-Based Middleware (_middleware.js)", value: "middleware", checked: true },
|
|
49
43
|
{ name: "Auto-load Environment Variables (.env)", value: "dotenv", checked: true },
|
|
50
44
|
{ name: "Automatic Input Validation (Schema)", value: "validation", checked: true },
|
|
51
|
-
{ name: "Multipart File Uploads (req.files)", value: "multipart", checked: true }
|
|
45
|
+
{ name: "Multipart File Uploads (req.files)", value: "multipart", checked: true },
|
|
46
|
+
{ name: "Smart Error Handling (_error.js)", value: "errors", checked: true },
|
|
47
|
+
{ name: "Dev Dashboard (/__zerra)", value: "dashboard", checked: true }
|
|
52
48
|
]
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: "confirm",
|
|
52
|
+
name: "installDeps",
|
|
53
|
+
message: "Install dependencies automatically?",
|
|
54
|
+
default: true,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: "confirm",
|
|
58
|
+
name: "initGit",
|
|
59
|
+
message: "Initialize git repository?",
|
|
60
|
+
default: true,
|
|
53
61
|
}
|
|
54
62
|
]);
|
|
55
63
|
|
|
64
|
+
// 2. Project Preview Summary
|
|
65
|
+
console.log(`\nš Project Configuration:`);
|
|
66
|
+
console.log(` - Project Name: \x1b[36m${projectName}\x1b[0m`);
|
|
67
|
+
console.log(` - Database: \x1b[33m${answers.database.replace('js-', '')}\x1b[0m`);
|
|
68
|
+
console.log(` - Auth Starter: \x1b[32m${answers.includeAuth ? 'Enabled' : 'Disabled'}\x1b[0m`);
|
|
69
|
+
console.log(` - Features: \x1b[35m${answers.features.join(', ') || 'None'}\x1b[0m`);
|
|
70
|
+
console.log(` - Auto-Install: \x1b[32m${answers.installDeps ? 'Yes' : 'No'}\x1b[0m`);
|
|
71
|
+
console.log(` - Git Init: \x1b[32m${answers.initGit ? 'Yes' : 'No'}\x1b[0m`);
|
|
72
|
+
|
|
73
|
+
const { confirmProceed } = await inquirer.prompt([
|
|
74
|
+
{
|
|
75
|
+
type: "confirm",
|
|
76
|
+
name: "confirmProceed",
|
|
77
|
+
message: "Does this look correct?",
|
|
78
|
+
default: true,
|
|
79
|
+
}
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
if (!confirmProceed) {
|
|
83
|
+
console.log("\nā Project creation cancelled.\n");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
56
87
|
const baseTemplatePath = path.join(__dirname, "templates", "js-base");
|
|
57
88
|
const dbTemplatePath = path.join(__dirname, "templates", answers.database);
|
|
89
|
+
const authTemplatePath = path.join(__dirname, "templates", "js-auth");
|
|
58
90
|
|
|
59
|
-
console.log(`\nšļø
|
|
91
|
+
console.log(`\nšļø Building your Zerra application...`);
|
|
60
92
|
|
|
61
93
|
try {
|
|
62
94
|
// 1. Copy the Base Template (Foundation)
|
|
@@ -87,6 +119,25 @@ program
|
|
|
87
119
|
}
|
|
88
120
|
}
|
|
89
121
|
|
|
122
|
+
// 2.5 Overlay Auth Starter (if selected)
|
|
123
|
+
if (answers.includeAuth && fs.existsSync(authTemplatePath)) {
|
|
124
|
+
console.log(` š Adding Auth Starter...`);
|
|
125
|
+
await fs.copy(authTemplatePath, targetPath, {
|
|
126
|
+
overwrite: true,
|
|
127
|
+
filter: (src) => !src.endsWith("package.json"),
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const authPkgPath = path.join(authTemplatePath, "package.json");
|
|
131
|
+
const targetPkgPath = path.join(targetPath, "package.json");
|
|
132
|
+
|
|
133
|
+
if (fs.existsSync(authPkgPath) && fs.existsSync(targetPkgPath)) {
|
|
134
|
+
const basePkg = await fs.readJson(targetPkgPath);
|
|
135
|
+
const authPkg = await fs.readJson(authPkgPath);
|
|
136
|
+
basePkg.dependencies = { ...(basePkg.dependencies || {}), ...(authPkg.dependencies || {}) };
|
|
137
|
+
await fs.writeJson(targetPkgPath, basePkg, { spaces: 2 });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
90
141
|
// 3. Customize project name in package.json
|
|
91
142
|
const pkgPath = path.join(targetPath, "package.json");
|
|
92
143
|
if (fs.existsSync(pkgPath)) {
|
|
@@ -102,11 +153,13 @@ program
|
|
|
102
153
|
middleware: answers.features.includes('middleware'),
|
|
103
154
|
dotenv: answers.features.includes('dotenv'),
|
|
104
155
|
validation: answers.features.includes('validation'),
|
|
105
|
-
multipart: answers.features.includes('multipart')
|
|
156
|
+
multipart: answers.features.includes('multipart'),
|
|
157
|
+
errors: answers.features.includes('errors'),
|
|
158
|
+
dashboard: answers.features.includes('dashboard')
|
|
106
159
|
};
|
|
107
160
|
|
|
108
161
|
const configJsonPath = path.join(targetPath, 'zerra.config.json');
|
|
109
|
-
await fs.writeJson(configJsonPath, { features: featureConfig }, { spaces: 2 });
|
|
162
|
+
await fs.writeJson(configJsonPath, { features: featureConfig, plugins: [] }, { spaces: 2 });
|
|
110
163
|
|
|
111
164
|
const { execSync } = require("child_process");
|
|
112
165
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const auth = require('../../services/auth');
|
|
2
|
+
|
|
3
|
+
module.exports = async (req, res) => {
|
|
4
|
+
const { email, password } = req.body;
|
|
5
|
+
|
|
6
|
+
// Mock user for the starter
|
|
7
|
+
const mockUser = { id: 1, email: 'user@example.com', password: auth.hashPassword('password123') };
|
|
8
|
+
|
|
9
|
+
if (email === mockUser.email && auth.comparePassword(password, mockUser.password)) {
|
|
10
|
+
const token = auth.generateToken(mockUser);
|
|
11
|
+
return res.json({ token, message: "Login successful" });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
res.status(401).json({ error: "Invalid credentials" });
|
|
15
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const auth = require('../../services/auth');
|
|
2
|
+
|
|
3
|
+
// Note: This is a starter. In a real app, you would save the user to your database.
|
|
4
|
+
module.exports = async (req, res) => {
|
|
5
|
+
const { email, password } = req.body;
|
|
6
|
+
|
|
7
|
+
if (!email || !password) {
|
|
8
|
+
return res.status(400).json({ error: "Email and password are required" });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const hashedPassword = auth.hashPassword(password);
|
|
12
|
+
|
|
13
|
+
// Here you would: await db.users.create({ email, password: hashedPassword })
|
|
14
|
+
|
|
15
|
+
res.status(201).json({
|
|
16
|
+
message: "User registered successfully (Starter Mock)",
|
|
17
|
+
user: { email }
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports.schema = {
|
|
22
|
+
email: 'string',
|
|
23
|
+
password: 'string'
|
|
24
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const auth = require('../../services/auth');
|
|
2
|
+
|
|
3
|
+
module.exports = async (req, res, next) => {
|
|
4
|
+
const token = req.headers.authorization?.split(' ')[1];
|
|
5
|
+
|
|
6
|
+
if (!token) {
|
|
7
|
+
return res.status(401).json({ error: "No token provided" });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const decoded = auth.verifyToken(token);
|
|
12
|
+
req.user = decoded;
|
|
13
|
+
await next();
|
|
14
|
+
} catch (e) {
|
|
15
|
+
res.status(401).json({ error: "Invalid or expired token" });
|
|
16
|
+
}
|
|
17
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const jwt = require('jsonwebtoken');
|
|
2
|
+
const bcrypt = require('bcryptjs');
|
|
3
|
+
|
|
4
|
+
const JWT_SECRET = process.env.JWT_SECRET || 'zerra-secret-key';
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
hashPassword: (password) => bcrypt.hashSync(password, 10),
|
|
8
|
+
comparePassword: (password, hash) => bcrypt.compareSync(password, hash),
|
|
9
|
+
generateToken: (user) => jwt.sign({ id: user.id, email: user.email }, JWT_SECRET, { expiresIn: '1d' }),
|
|
10
|
+
verifyToken: (token) => jwt.verify(token, JWT_SECRET)
|
|
11
|
+
};
|