@erwininteractive/mvc 0.6.1 → 0.6.3
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/cli.js +19 -0
- package/dist/generators/makeAuth.d.ts +9 -0
- package/dist/generators/makeAuth.js +299 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -8,6 +8,7 @@ const generateController_1 = require("./generators/generateController");
|
|
|
8
8
|
const generateResource_1 = require("./generators/generateResource");
|
|
9
9
|
const generateWebAuthn_1 = require("./generators/generateWebAuthn");
|
|
10
10
|
const listRoutes_1 = require("./generators/listRoutes");
|
|
11
|
+
const makeAuth_1 = require("./generators/makeAuth");
|
|
11
12
|
const program = new commander_1.Command();
|
|
12
13
|
program
|
|
13
14
|
.name("erwinmvc")
|
|
@@ -18,6 +19,7 @@ Examples:
|
|
|
18
19
|
$ erwinmvc init myapp Create a new app
|
|
19
20
|
$ erwinmvc generate resource Post Generate CRUD for Post
|
|
20
21
|
$ erwinmvc webauthn Setup passkey authentication
|
|
22
|
+
$ erwinmvc make:auth Generate authentication system
|
|
21
23
|
$ erwinmvc list:routes Show all routes
|
|
22
24
|
`);
|
|
23
25
|
// Init command - scaffold a new application
|
|
@@ -116,4 +118,21 @@ program
|
|
|
116
118
|
process.exit(1);
|
|
117
119
|
}
|
|
118
120
|
});
|
|
121
|
+
// Make auth command - generate complete authentication system
|
|
122
|
+
program
|
|
123
|
+
.command("make:auth")
|
|
124
|
+
.alias("ma")
|
|
125
|
+
.description("Generate a complete authentication system")
|
|
126
|
+
.option("--without-model", "Skip generating User model")
|
|
127
|
+
.option("--session-only", "Only session-based auth (no JWT tokens)")
|
|
128
|
+
.option("--jwt-only", "Only JWT tokens (no sessions)")
|
|
129
|
+
.action(async (options) => {
|
|
130
|
+
try {
|
|
131
|
+
await (0, makeAuth_1.makeAuth)(options);
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
console.error("Error:", err instanceof Error ? err.message : err);
|
|
135
|
+
process.exit(1);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
119
138
|
program.parse();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface MakeAuthOptions {
|
|
2
|
+
withoutModel?: boolean;
|
|
3
|
+
sessionOnly?: boolean;
|
|
4
|
+
jwtOnly?: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Generate a complete authentication system (User model, AuthController, views)
|
|
8
|
+
*/
|
|
9
|
+
export declare function makeAuth(options?: MakeAuthOptions): Promise<void>;
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.makeAuth = makeAuth;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const paths_1 = require("./paths");
|
|
10
|
+
/**
|
|
11
|
+
* Generate a complete authentication system (User model, AuthController, views)
|
|
12
|
+
*/
|
|
13
|
+
async function makeAuth(options = {}) {
|
|
14
|
+
const targetDir = process.cwd();
|
|
15
|
+
const templateDir = path_1.default.join((0, paths_1.getTemplatesDir)(), "auth");
|
|
16
|
+
console.log("Generating authentication system...");
|
|
17
|
+
// Create auth template files if they don't exist
|
|
18
|
+
createAuthTemplates(templateDir);
|
|
19
|
+
// Copy auth templates
|
|
20
|
+
if (fs_1.default.existsSync(templateDir)) {
|
|
21
|
+
copyDirRecursive(templateDir, targetDir);
|
|
22
|
+
}
|
|
23
|
+
// Update .env with auth configuration
|
|
24
|
+
const envPath = path_1.default.join(targetDir, ".env");
|
|
25
|
+
if (fs_1.default.existsSync(envPath)) {
|
|
26
|
+
const envContent = fs_1.default.readFileSync(envPath, "utf-8");
|
|
27
|
+
if (!envContent.includes("SESSION_SECRET")) {
|
|
28
|
+
fs_1.default.appendFileSync(envPath, "\nSESSION_SECRET=\"change-me-session\"\n");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// Update .env.example
|
|
32
|
+
const envExamplePath = path_1.default.join(targetDir, ".env.example");
|
|
33
|
+
if (fs_1.default.existsSync(envExamplePath)) {
|
|
34
|
+
const envContent = fs_1.default.readFileSync(envExamplePath, "utf-8");
|
|
35
|
+
if (!envContent.includes("SESSION_SECRET")) {
|
|
36
|
+
fs_1.default.appendFileSync(envExamplePath, "\nSESSION_SECRET=\"change-me-session\"\n");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
console.log("\nAuthentication system generated successfully!");
|
|
40
|
+
console.log(`
|
|
41
|
+
Next steps:
|
|
42
|
+
1. Run migrations: npm run db:migrate
|
|
43
|
+
2. Add auth routes to src/server.ts:
|
|
44
|
+
- import * as auth from "./controllers/AuthController";
|
|
45
|
+
- app.get("/login", auth.showLogin);
|
|
46
|
+
- app.post("/login", auth.login);
|
|
47
|
+
- app.get("/register", auth.showRegister);
|
|
48
|
+
- app.post("/register", auth.register);
|
|
49
|
+
- app.post("/logout", auth.logout);
|
|
50
|
+
`);
|
|
51
|
+
// Generate AuthController.ejs files
|
|
52
|
+
const authControllerTemplate = path_1.default.join((0, paths_1.getTemplatesDir)(), "authController.ts.ejs");
|
|
53
|
+
if (fs_1.default.existsSync(authControllerTemplate)) {
|
|
54
|
+
const destPath = path_1.default.join(targetDir, "src", "controllers", "AuthController.ts");
|
|
55
|
+
const content = fs_1.default.readFileSync(authControllerTemplate, "utf-8");
|
|
56
|
+
fs_1.default.writeFileSync(destPath, content);
|
|
57
|
+
console.log("✓ Created src/controllers/AuthController.ts");
|
|
58
|
+
}
|
|
59
|
+
// Generate EJS views
|
|
60
|
+
const viewsDir = path_1.default.join(targetDir, "src", "views", "auth");
|
|
61
|
+
fs_1.default.mkdirSync(viewsDir, { recursive: true });
|
|
62
|
+
const loginView = path_1.default.join((0, paths_1.getTemplatesDir)(), "views/auth/login.ejs");
|
|
63
|
+
if (fs_1.default.existsSync(loginView)) {
|
|
64
|
+
fs_1.default.copyFileSync(loginView, path_1.default.join(viewsDir, "login.ejs"));
|
|
65
|
+
console.log("✓ Created src/views/auth/login.ejs");
|
|
66
|
+
}
|
|
67
|
+
const registerView = path_1.default.join((0, paths_1.getTemplatesDir)(), "views/auth/register.ejs");
|
|
68
|
+
if (fs_1.default.existsSync(registerView)) {
|
|
69
|
+
fs_1.default.copyFileSync(registerView, path_1.default.join(viewsDir, "register.ejs"));
|
|
70
|
+
console.log("✓ Created src/views/auth/register.ejs");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create auth template files if they don't exist
|
|
75
|
+
*/
|
|
76
|
+
function createAuthTemplates(templateDir) {
|
|
77
|
+
fs_1.default.mkdirSync(templateDir, { recursive: true });
|
|
78
|
+
// Create AuthController template
|
|
79
|
+
const controllerTemplate = `import { Request, Response } from "express";
|
|
80
|
+
import { hashPassword, verifyPassword, signToken, verifyToken } from "@erwininteractive/mvc";
|
|
81
|
+
import { getPrismaClient } from "@erwininteractive/mvc";
|
|
82
|
+
|
|
83
|
+
const prisma = getPrismaClient();
|
|
84
|
+
|
|
85
|
+
export async function showLogin(req: Request, res: Response) {
|
|
86
|
+
res.render("auth/login", { title: "Login" });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function showRegister(req: Request, res: Response) {
|
|
90
|
+
res.render("auth/register", { title: "Register" });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function login(req: Request, res: Response) {
|
|
94
|
+
const { email, password } = req.body;
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const user = await prisma.user.findUnique({
|
|
98
|
+
where: { email }
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (!user) {
|
|
102
|
+
return res.render("auth/login", {
|
|
103
|
+
title: "Login",
|
|
104
|
+
error: "Invalid email or password"
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const isValid = await verifyPassword(password, user.hashedPassword);
|
|
109
|
+
if (!isValid) {
|
|
110
|
+
return res.render("auth/login", {
|
|
111
|
+
title: "Login",
|
|
112
|
+
error: "Invalid email or password"
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const token = signToken({ userId: user.id, email: user.email });
|
|
117
|
+
res.cookie("token", token, {
|
|
118
|
+
httpOnly: true,
|
|
119
|
+
secure: process.env.NODE_ENV === "production",
|
|
120
|
+
maxAge: 1000 * 60 * 60 * 24 // 24 hours
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
res.redirect("/");
|
|
124
|
+
} catch (err) {
|
|
125
|
+
console.error("Login error:", err);
|
|
126
|
+
res.render("auth/login", {
|
|
127
|
+
title: "Login",
|
|
128
|
+
error: "An error occurred during login"
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function register(req: Request, res: Response) {
|
|
134
|
+
const { email, password, confirmPassword } = req.body;
|
|
135
|
+
|
|
136
|
+
if (password !== confirmPassword) {
|
|
137
|
+
return res.render("auth/register", {
|
|
138
|
+
title: "Register",
|
|
139
|
+
error: "Passwords do not match"
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
const existingUser = await prisma.user.findUnique({
|
|
145
|
+
where: { email }
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (existingUser) {
|
|
149
|
+
return res.render("auth/register", {
|
|
150
|
+
title: "Register",
|
|
151
|
+
error: "Email already in use"
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const hashedPassword = await hashPassword(password);
|
|
156
|
+
|
|
157
|
+
await prisma.user.create({
|
|
158
|
+
data: {
|
|
159
|
+
email,
|
|
160
|
+
hashedPassword,
|
|
161
|
+
role: "user"
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
res.redirect("/login");
|
|
166
|
+
} catch (err) {
|
|
167
|
+
console.error("Registration error:", err);
|
|
168
|
+
res.render("auth/register", {
|
|
169
|
+
title: "Register",
|
|
170
|
+
error: "An error occurred during registration"
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function logout(req: Request, res: Response) {
|
|
176
|
+
res.clearCookie("token");
|
|
177
|
+
res.redirect("/login");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export async function requireAuth(req: Request, res: Response, next: any) {
|
|
181
|
+
const token = req.cookies?.token || req.headers.authorization?.split(" ")[1];
|
|
182
|
+
|
|
183
|
+
if (!token) {
|
|
184
|
+
return res.redirect("/login");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
try {
|
|
188
|
+
const decoded = verifyToken(token);
|
|
189
|
+
req.user = decoded;
|
|
190
|
+
next();
|
|
191
|
+
} catch {
|
|
192
|
+
res.clearCookie("token");
|
|
193
|
+
res.redirect("/login");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
`;
|
|
197
|
+
fs_1.default.writeFileSync(path_1.default.join(templateDir, "authController.ts.ejs"), controllerTemplate);
|
|
198
|
+
// Create login view
|
|
199
|
+
const loginView = `<!DOCTYPE html>
|
|
200
|
+
<html>
|
|
201
|
+
<head>
|
|
202
|
+
<title><%= title %></title>
|
|
203
|
+
<style>
|
|
204
|
+
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; }
|
|
205
|
+
input { width: 100%; padding: 8px; margin: 8px 0; }
|
|
206
|
+
button { background: #007bff; color: white; padding: 10px; border: none; cursor: pointer; }
|
|
207
|
+
.error { color: red; }
|
|
208
|
+
.link { color: #007bff; }
|
|
209
|
+
</style>
|
|
210
|
+
</head>
|
|
211
|
+
<body>
|
|
212
|
+
<h1><%= title %></h1>
|
|
213
|
+
|
|
214
|
+
<% if (error) { %>
|
|
215
|
+
<p class="error"><%= error %></p>
|
|
216
|
+
<% } %>
|
|
217
|
+
|
|
218
|
+
<form method="POST" action="/login">
|
|
219
|
+
<label>Email</label>
|
|
220
|
+
<input type="email" name="email" required>
|
|
221
|
+
|
|
222
|
+
<label>Password</label>
|
|
223
|
+
<input type="password" name="password" required>
|
|
224
|
+
|
|
225
|
+
<button type="submit">Login</button>
|
|
226
|
+
</form>
|
|
227
|
+
|
|
228
|
+
<p>Don't have an account? <a href="/register" class="link">Register</a></p>
|
|
229
|
+
</body>
|
|
230
|
+
</html>
|
|
231
|
+
`;
|
|
232
|
+
fs_1.default.writeFileSync(path_1.default.join(templateDir, "views/auth/login.ejs.ejs"), loginView);
|
|
233
|
+
// Create register view
|
|
234
|
+
const registerView = `<!DOCTYPE html>
|
|
235
|
+
<html>
|
|
236
|
+
<head>
|
|
237
|
+
<title><%= title %></title>
|
|
238
|
+
<style>
|
|
239
|
+
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; }
|
|
240
|
+
input { width: 100%; padding: 8px; margin: 8px 0; }
|
|
241
|
+
button { background: #28a745; color: white; padding: 10px; border: none; cursor: pointer; }
|
|
242
|
+
.error { color: red; }
|
|
243
|
+
.link { color: #007bff; }
|
|
244
|
+
</style>
|
|
245
|
+
</head>
|
|
246
|
+
<body>
|
|
247
|
+
<h1><%= title %></h1>
|
|
248
|
+
|
|
249
|
+
<% if (error) { %>
|
|
250
|
+
<p class="error"><%= error %></p>
|
|
251
|
+
<% } %>
|
|
252
|
+
|
|
253
|
+
<form method="POST" action="/register">
|
|
254
|
+
<label>Email</label>
|
|
255
|
+
<input type="email" name="email" required>
|
|
256
|
+
|
|
257
|
+
<label>Password</label>
|
|
258
|
+
<input type="password" name="password" required>
|
|
259
|
+
|
|
260
|
+
<label>Confirm Password</label>
|
|
261
|
+
<input type="password" name="confirmPassword" required>
|
|
262
|
+
|
|
263
|
+
<button type="submit">Register</button>
|
|
264
|
+
</form>
|
|
265
|
+
|
|
266
|
+
<p>Already have an account? <a href="/login" class="link">Login</a></p>
|
|
267
|
+
</body>
|
|
268
|
+
</html>
|
|
269
|
+
`;
|
|
270
|
+
fs_1.default.writeFileSync(path_1.default.join(templateDir, "views/auth/register.ejs.ejs"), registerView);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Recursively copy a directory
|
|
274
|
+
*/
|
|
275
|
+
function copyDirRecursive(src, dest) {
|
|
276
|
+
if (!fs_1.default.existsSync(src)) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (!fs_1.default.existsSync(dest)) {
|
|
280
|
+
fs_1.default.mkdirSync(dest, { recursive: true });
|
|
281
|
+
}
|
|
282
|
+
const entries = fs_1.default.readdirSync(src, { withFileTypes: true });
|
|
283
|
+
for (const entry of entries) {
|
|
284
|
+
const srcPath = path_1.default.join(src, entry.name);
|
|
285
|
+
const destPath = path_1.default.join(dest, entry.name);
|
|
286
|
+
if (entry.isDirectory()) {
|
|
287
|
+
copyDirRecursive(srcPath, destPath);
|
|
288
|
+
}
|
|
289
|
+
else if (entry.name.endsWith(".ejs.ejs")) {
|
|
290
|
+
// Remove .ejs.ejs suffix, write render version
|
|
291
|
+
const content = fs_1.default.readFileSync(srcPath, "utf-8");
|
|
292
|
+
const targetPath = destPath.replace(/\.ejs\.ejs$/, ".ejs");
|
|
293
|
+
fs_1.default.writeFileSync(targetPath, content);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
fs_1.default.copyFileSync(srcPath, destPath);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
package/package.json
CHANGED