@erwininteractive/mvc 0.4.1 → 0.5.1
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/README.md
CHANGED
|
@@ -279,6 +279,24 @@ app.get("/protected", authenticate, (req, res) => {
|
|
|
279
279
|
});
|
|
280
280
|
```
|
|
281
281
|
|
|
282
|
+
### Auto-Inject User to Views
|
|
283
|
+
|
|
284
|
+
When a user is authenticated, their information is automatically available in all EJS templates via `res.locals.user`:
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
// In any EJS template
|
|
288
|
+
<% if (user) { %>
|
|
289
|
+
<p>Welcome, <%= user.email %></p>
|
|
290
|
+
<% } else { %>
|
|
291
|
+
<a href="/auth/login">Login</a>
|
|
292
|
+
<% } %>
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
The middleware automatically:
|
|
296
|
+
- Extracts JWT from `req.cookies.token` or `Authorization` header
|
|
297
|
+
- Verifies the token using `JWT_SECRET`
|
|
298
|
+
- Sets `req.user` and `res.locals.user` for use in views
|
|
299
|
+
|
|
282
300
|
---
|
|
283
301
|
|
|
284
302
|
### WebAuthn (Passkeys)
|
package/dist/framework/App.js
CHANGED
|
@@ -12,6 +12,7 @@ const redis_1 = require("redis");
|
|
|
12
12
|
const connect_redis_1 = __importDefault(require("connect-redis"));
|
|
13
13
|
const helmet_1 = __importDefault(require("helmet"));
|
|
14
14
|
const cors_1 = __importDefault(require("cors"));
|
|
15
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
15
16
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
16
17
|
const path_1 = __importDefault(require("path"));
|
|
17
18
|
// Load environment variables
|
|
@@ -74,6 +75,21 @@ async function createMvcApp(options = {}) {
|
|
|
74
75
|
// View engine
|
|
75
76
|
app.set("view engine", "ejs");
|
|
76
77
|
app.set("views", path_1.default.resolve(viewsPath));
|
|
78
|
+
// Auto-inject authenticated user into views via res.locals
|
|
79
|
+
app.use((req, res, next) => {
|
|
80
|
+
try {
|
|
81
|
+
const token = req.cookies?.token || req.headers.authorization?.split(" ")[1];
|
|
82
|
+
if (token) {
|
|
83
|
+
const decoded = jsonwebtoken_1.default.verify(token, process.env.JWT_SECRET);
|
|
84
|
+
req.user = decoded;
|
|
85
|
+
res.locals.user = decoded;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// Token invalid or expired
|
|
90
|
+
}
|
|
91
|
+
next();
|
|
92
|
+
});
|
|
77
93
|
// Add respond helper to response
|
|
78
94
|
app.use((req, res, next) => {
|
|
79
95
|
res.respond = function (viewName, data) {
|
package/package.json
CHANGED
|
@@ -76,6 +76,8 @@ Create `.ejs` files in `src/views/`. EJS lets you use JavaScript in your HTML:
|
|
|
76
76
|
<%- include('partials/header') %>
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
+
**Note:** When using authentication, the `user` object is automatically available in all views when a user is logged in. No need to pass it manually!
|
|
80
|
+
|
|
79
81
|
### Adding Routes
|
|
80
82
|
|
|
81
83
|
Edit `src/server.ts` to add routes:
|
|
@@ -12,9 +12,11 @@
|
|
|
12
12
|
"db:push": "prisma db push"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@erwininteractive/mvc": "^
|
|
15
|
+
"@erwininteractive/mvc": "^0.4.0",
|
|
16
|
+
"cookie-parser": "^1.4.6"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
19
|
+
"@types/cookie-parser": "^1.4.7",
|
|
18
20
|
"@types/express": "^5.0.0",
|
|
19
21
|
"@types/node": "^22.7.5",
|
|
20
22
|
"tsx": "^4.19.1",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createMvcApp, startServer } from "@erwininteractive/mvc";
|
|
2
|
+
import cookieParser from "cookie-parser";
|
|
2
3
|
|
|
3
4
|
async function main() {
|
|
4
5
|
const { app } = await createMvcApp({
|
|
@@ -6,8 +7,11 @@ async function main() {
|
|
|
6
7
|
publicPath: "public",
|
|
7
8
|
});
|
|
8
9
|
|
|
10
|
+
// Parse cookies (needed for JWT authentication)
|
|
11
|
+
app.use(cookieParser());
|
|
12
|
+
|
|
9
13
|
// Root route - displays welcome page
|
|
10
|
-
app.get("/", (req, res) => {
|
|
14
|
+
app.get("/", (req: any, res: any) => {
|
|
11
15
|
res.render("index", { title: "Welcome" });
|
|
12
16
|
});
|
|
13
17
|
|