@javagt/express-easy-auth 1.0.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/.env.example ADDED
@@ -0,0 +1,13 @@
1
+ # Environment Variables
2
+
3
+ # The domain name your app is running on (for WebAuthn/Passkeys)
4
+ DOMAIN=auth-test.javagrant.ac.nz
5
+
6
+ # The port the server will listen on
7
+ PORT=3000
8
+
9
+ # Secret used to sign the session cookie
10
+ SESSION_SECRET=your-secure-session-secret-here
11
+
12
+ # Environment (development/production)
13
+ NODE_ENV=development
@@ -0,0 +1,64 @@
1
+ import express from 'express';
2
+ import { userDb } from '../src/db/init.js';
3
+ import { requireAuth } from '../src/middleware/auth.js';
4
+
5
+ const router = express.Router();
6
+
7
+ // ─── PROFILE (Demo Only) ───────────────────────────────────────────────────
8
+
9
+ router.get('/me', requireAuth, (req, res) => {
10
+ const appDataDb = req.app.get('appDataDb');
11
+
12
+ // 1. Get Identity Data (from Auth Server / Session)
13
+ // In a real app, you might query your auth DB here if you need more fields,
14
+ // but we have username/email in the session or can query authDb if needed.
15
+ // For the demo, we'll just return what's in the profile table for this user.
16
+
17
+ const profile = appDataDb.prepare(
18
+ 'SELECT display_name, bio, avatar_url, location, website, preferences, updated_at FROM profiles WHERE user_id = ?'
19
+ ).get(req.userId) || {};
20
+
21
+ res.json({
22
+ userId: req.userId,
23
+ username: req.session.username, // From session
24
+ profile: {
25
+ ...profile,
26
+ preferences: profile.preferences ? JSON.parse(profile.preferences) : {}
27
+ }
28
+ });
29
+ });
30
+
31
+ router.patch('/me', requireAuth, (req, res) => {
32
+ const appDataDb = req.app.get('appDataDb');
33
+ const { display_name, bio, location, website, preferences } = req.body;
34
+
35
+ if (bio && bio.length > 500) {
36
+ return res.status(400).json({ error: 'Bio must be under 500 characters' });
37
+ }
38
+
39
+ const now = Date.now();
40
+ appDataDb.prepare(`
41
+ INSERT INTO profiles (user_id, display_name, bio, location, website, preferences, created_at, updated_at)
42
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
43
+ ON CONFLICT(user_id) DO UPDATE SET
44
+ display_name = COALESCE(excluded.display_name, display_name),
45
+ bio = COALESCE(excluded.bio, bio),
46
+ location = COALESCE(excluded.location, location),
47
+ website = COALESCE(excluded.website, website),
48
+ preferences = COALESCE(excluded.preferences, preferences),
49
+ updated_at = excluded.updated_at
50
+ `).run(
51
+ req.userId,
52
+ display_name || null,
53
+ bio || null,
54
+ location || null,
55
+ website || null,
56
+ preferences ? JSON.stringify(preferences) : null,
57
+ now,
58
+ now
59
+ );
60
+
61
+ res.json({ success: true });
62
+ });
63
+
64
+ export default router;