@hintergrund/worker 0.1.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/README.md +45 -0
- package/dist/api/builder/handlers/generate.handler.d.ts +12 -0
- package/dist/api/builder/handlers/generate.handler.d.ts.map +1 -0
- package/dist/api/builder/handlers/generate.handler.js +203 -0
- package/dist/api/builder/handlers/template.handler.d.ts +12 -0
- package/dist/api/builder/handlers/template.handler.d.ts.map +1 -0
- package/dist/api/builder/handlers/template.handler.js +208 -0
- package/dist/api/builder/routes.d.ts +7 -0
- package/dist/api/builder/routes.d.ts.map +1 -0
- package/dist/api/builder/routes.js +15 -0
- package/dist/api/cms/handlers/assets.handler.d.ts +8 -0
- package/dist/api/cms/handlers/assets.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/assets.handler.js +299 -0
- package/dist/api/cms/handlers/auth.handler.d.ts +11 -0
- package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/auth.handler.js +214 -0
- package/dist/api/cms/handlers/config.handler.d.ts +3 -0
- package/dist/api/cms/handlers/config.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/config.handler.js +88 -0
- package/dist/api/cms/handlers/content.handler.d.ts +10 -0
- package/dist/api/cms/handlers/content.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/content.handler.js +293 -0
- package/dist/api/cms/handlers/dashboard.handler.d.ts +6 -0
- package/dist/api/cms/handlers/dashboard.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/dashboard.handler.js +15 -0
- package/dist/api/cms/routes/assets.routes.d.ts +4 -0
- package/dist/api/cms/routes/assets.routes.d.ts.map +1 -0
- package/dist/api/cms/routes/assets.routes.js +13 -0
- package/dist/api/cms/routes/auth.routes.d.ts +4 -0
- package/dist/api/cms/routes/auth.routes.d.ts.map +1 -0
- package/dist/api/cms/routes/auth.routes.js +7 -0
- package/dist/api/cms/routes/content.routes.d.ts +4 -0
- package/dist/api/cms/routes/content.routes.d.ts.map +1 -0
- package/dist/api/cms/routes/content.routes.js +11 -0
- package/dist/api/cms/routes/index.d.ts +4 -0
- package/dist/api/cms/routes/index.d.ts.map +1 -0
- package/dist/api/cms/routes/index.js +16 -0
- package/dist/api/newsletter/handlers/email.handler.d.ts +7 -0
- package/dist/api/newsletter/handlers/email.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/email.handler.js +102 -0
- package/dist/api/newsletter/handlers/emails.handler.d.ts +21 -0
- package/dist/api/newsletter/handlers/emails.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/emails.handler.js +134 -0
- package/dist/api/newsletter/handlers/public.handler.d.ts +22 -0
- package/dist/api/newsletter/handlers/public.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/public.handler.js +242 -0
- package/dist/api/newsletter/handlers/send.handler.d.ts +12 -0
- package/dist/api/newsletter/handlers/send.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/send.handler.js +153 -0
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts +22 -0
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/subscribers.handler.js +191 -0
- package/dist/api/newsletter/routes/admin.routes.d.ts +4 -0
- package/dist/api/newsletter/routes/admin.routes.d.ts.map +1 -0
- package/dist/api/newsletter/routes/admin.routes.js +21 -0
- package/dist/api/newsletter/routes/index.d.ts +4 -0
- package/dist/api/newsletter/routes/index.d.ts.map +1 -0
- package/dist/api/newsletter/routes/index.js +9 -0
- package/dist/api/newsletter/routes/public.routes.d.ts +4 -0
- package/dist/api/newsletter/routes/public.routes.d.ts.map +1 -0
- package/dist/api/newsletter/routes/public.routes.js +9 -0
- package/dist/api/shop/handlers/customers.handler.d.ts +17 -0
- package/dist/api/shop/handlers/customers.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/customers.handler.js +89 -0
- package/dist/api/shop/handlers/orders.handler.d.ts +17 -0
- package/dist/api/shop/handlers/orders.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/orders.handler.js +84 -0
- package/dist/api/shop/handlers/prices.handler.d.ts +17 -0
- package/dist/api/shop/handlers/prices.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/prices.handler.js +87 -0
- package/dist/api/shop/handlers/products.handler.d.ts +45 -0
- package/dist/api/shop/handlers/products.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/products.handler.js +243 -0
- package/dist/api/shop/handlers/stats.handler.d.ts +10 -0
- package/dist/api/shop/handlers/stats.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/stats.handler.js +71 -0
- package/dist/api/shop/routes.d.ts +7 -0
- package/dist/api/shop/routes.d.ts.map +1 -0
- package/dist/api/shop/routes.js +31 -0
- package/dist/cms-assets.d.ts +4 -0
- package/dist/cms-assets.d.ts.map +1 -0
- package/dist/cms-assets.js +5 -0
- package/dist/cms-bundle.d.ts +3 -0
- package/dist/cms-bundle.d.ts.map +1 -0
- package/dist/cms-bundle.js +4 -0
- package/dist/config-loader.d.ts +26 -0
- package/dist/config-loader.d.ts.map +1 -0
- package/dist/config-loader.js +98 -0
- package/dist/hintergrund-assets.d.ts +4 -0
- package/dist/hintergrund-assets.d.ts.map +1 -0
- package/dist/hintergrund-assets.js +9 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/jwt.d.ts +26 -0
- package/dist/jwt.d.ts.map +1 -0
- package/dist/jwt.js +102 -0
- package/dist/lib/auth/crypto.d.ts +23 -0
- package/dist/lib/auth/crypto.d.ts.map +1 -0
- package/dist/lib/auth/crypto.js +110 -0
- package/dist/lib/auth/jwt.d.ts +38 -0
- package/dist/lib/auth/jwt.d.ts.map +1 -0
- package/dist/lib/auth/jwt.js +120 -0
- package/dist/lib/config.d.ts +31 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +174 -0
- package/dist/lib/email.d.ts +16 -0
- package/dist/lib/email.d.ts.map +1 -0
- package/dist/lib/email.js +83 -0
- package/dist/lib/template.d.ts +2 -0
- package/dist/lib/template.d.ts.map +1 -0
- package/dist/lib/template.js +226 -0
- package/dist/middleware/auth.d.ts +9 -0
- package/dist/middleware/auth.d.ts.map +1 -0
- package/dist/middleware/auth.js +93 -0
- package/dist/template.d.ts +2 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +61 -0
- package/dist/utils.d.ts +23 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +110 -0
- package/package.json +32 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load application configuration from environment variables
|
|
3
|
+
* Supports both new modular env vars and legacy HG_CONFIG format
|
|
4
|
+
*/
|
|
5
|
+
export function loadConfig(env) {
|
|
6
|
+
// Try new env vars first - need either legacy password or new auth config
|
|
7
|
+
if (env.HG_SECRET && (env.HG_PASSWORD || env.HG_AUTH_METHOD)) {
|
|
8
|
+
return loadFromEnvVars(env);
|
|
9
|
+
}
|
|
10
|
+
throw new Error('No configuration found. Set HG_SECRET and either HG_PASSWORD or HG_AUTH_METHOD.');
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Check which required variables are missing for a module
|
|
14
|
+
*/
|
|
15
|
+
function getMissingVars(env, requiredVars) {
|
|
16
|
+
return requiredVars.filter(varName => !env[varName]);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Check if any variable from a list is set (module is attempted)
|
|
20
|
+
*/
|
|
21
|
+
function hasAnyVar(env, vars) {
|
|
22
|
+
return vars.some(varName => !!env[varName]);
|
|
23
|
+
}
|
|
24
|
+
// Module required variables definitions
|
|
25
|
+
const CMS_REQUIRED_VARS = ['HG_CMS_GIT_TOKEN', 'HG_CMS_GIT_OWNER', 'HG_CMS_GIT_REPO'];
|
|
26
|
+
const NEWSLETTER_REQUIRED_VARS = ['HG_NEWSLETTER_EMAIL_API_KEY', 'HG_NEWSLETTER_FROM_EMAIL'];
|
|
27
|
+
const SHOP_REQUIRED_VARS = ['HG_SHOP_STRIPE_SECRET_KEY'];
|
|
28
|
+
const BUILDER_REQUIRED_VARS = ['HG_BUILDER_GEMINI_API_KEY', 'HG_BUILDER_GIT_TOKEN'];
|
|
29
|
+
/**
|
|
30
|
+
* Parse user groups from environment variable
|
|
31
|
+
* Format: JSON array of UserGroup objects
|
|
32
|
+
* Example: [{"name":"admin","password":"encrypted"},{"name":"editors","emails":["a@b.com"]}]
|
|
33
|
+
*/
|
|
34
|
+
function parseUserGroups(env) {
|
|
35
|
+
if (!env.HG_AUTH_USER_GROUPS)
|
|
36
|
+
return undefined;
|
|
37
|
+
try {
|
|
38
|
+
const groups = JSON.parse(env.HG_AUTH_USER_GROUPS);
|
|
39
|
+
if (Array.isArray(groups)) {
|
|
40
|
+
return groups;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
console.error('Failed to parse HG_AUTH_USER_GROUPS:', e);
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Load config from new modular environment variables
|
|
50
|
+
* Auto-detects module availability based on required variables
|
|
51
|
+
*/
|
|
52
|
+
function loadFromEnvVars(env) {
|
|
53
|
+
// Build auth configuration
|
|
54
|
+
const loginMethod = env.HG_AUTH_METHOD || 'password';
|
|
55
|
+
const userGroups = parseUserGroups(env);
|
|
56
|
+
const authConfig = env.HG_AUTH_METHOD ? {
|
|
57
|
+
loginMethod,
|
|
58
|
+
resendApiKey: env.HG_AUTH_RESEND_API_KEY,
|
|
59
|
+
fromEmail: env.HG_AUTH_FROM_EMAIL,
|
|
60
|
+
userGroups,
|
|
61
|
+
} : undefined;
|
|
62
|
+
const config = {
|
|
63
|
+
global: {
|
|
64
|
+
hgSecret: env.HG_SECRET || '',
|
|
65
|
+
hgPassword: env.HG_PASSWORD,
|
|
66
|
+
auth: authConfig,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
// Load CMS module config (auto-detect based on required vars)
|
|
70
|
+
const cmsMissing = getMissingVars(env, CMS_REQUIRED_VARS);
|
|
71
|
+
if (cmsMissing.length === 0) {
|
|
72
|
+
config.cms = {
|
|
73
|
+
enabled: true,
|
|
74
|
+
gitToken: env.HG_CMS_GIT_TOKEN || '',
|
|
75
|
+
gitOwner: env.HG_CMS_GIT_OWNER || '',
|
|
76
|
+
gitRepo: env.HG_CMS_GIT_REPO || '',
|
|
77
|
+
gitBranch: env.HG_CMS_GIT_BRANCH || 'main',
|
|
78
|
+
contentDir: env.HG_CMS_CONTENT_DIR || 'content',
|
|
79
|
+
assetDir: env.HG_CMS_ASSET_DIR || 'assets',
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// Load Newsletter module config (auto-detect based on required vars)
|
|
83
|
+
const newsletterMissing = getMissingVars(env, NEWSLETTER_REQUIRED_VARS);
|
|
84
|
+
if (newsletterMissing.length === 0) {
|
|
85
|
+
config.newsletter = {
|
|
86
|
+
enabled: true,
|
|
87
|
+
emailProvider: env.HG_NEWSLETTER_EMAIL_PROVIDER || 'resend',
|
|
88
|
+
emailApiKey: env.HG_NEWSLETTER_EMAIL_API_KEY || '',
|
|
89
|
+
fromEmail: env.HG_NEWSLETTER_FROM_EMAIL || '',
|
|
90
|
+
fromName: env.HG_NEWSLETTER_FROM_NAME || '',
|
|
91
|
+
replyTo: env.HG_NEWSLETTER_REPLY_TO,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
// Load Shop module config (auto-detect based on required vars)
|
|
95
|
+
const shopMissing = getMissingVars(env, SHOP_REQUIRED_VARS);
|
|
96
|
+
if (shopMissing.length === 0) {
|
|
97
|
+
config.shop = {
|
|
98
|
+
enabled: true,
|
|
99
|
+
paymentProvider: 'stripe',
|
|
100
|
+
stripeSecretKey: env.HG_SHOP_STRIPE_SECRET_KEY || '',
|
|
101
|
+
stripeWebhookSecret: env.HG_SHOP_STRIPE_WEBHOOK_SECRET,
|
|
102
|
+
stripePublishableKey: env.HG_SHOP_STRIPE_PUBLISHABLE_KEY,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// Load Builder module config (auto-detect based on required vars)
|
|
106
|
+
const builderMissing = getMissingVars(env, BUILDER_REQUIRED_VARS);
|
|
107
|
+
if (builderMissing.length === 0) {
|
|
108
|
+
config.builder = {
|
|
109
|
+
enabled: true,
|
|
110
|
+
geminiApiKey: env.HG_BUILDER_GEMINI_API_KEY || '',
|
|
111
|
+
gitToken: env.HG_BUILDER_GIT_TOKEN || '',
|
|
112
|
+
templateRepo: env.HG_BUILDER_TEMPLATE_REPO || 'hintergrund-cloud/demo-shop',
|
|
113
|
+
gitBranch: env.HG_BUILDER_GIT_BRANCH || 'main',
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return config;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get admin UI configuration (which modules are enabled)
|
|
120
|
+
*/
|
|
121
|
+
export function getAdminConfig(appConfig) {
|
|
122
|
+
return {
|
|
123
|
+
cms: appConfig.cms?.enabled ?? false,
|
|
124
|
+
newsletter: appConfig.newsletter?.enabled ?? false,
|
|
125
|
+
shop: appConfig.shop?.enabled ?? false,
|
|
126
|
+
builder: appConfig.builder?.enabled ?? false,
|
|
127
|
+
loginMethod: appConfig.global.auth?.loginMethod ?? 'password',
|
|
128
|
+
userGroups: appConfig.global.auth?.userGroups?.map(g => g.name),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get CMS module configuration
|
|
133
|
+
* @throws Error if CMS module is not enabled
|
|
134
|
+
*/
|
|
135
|
+
export function getCmsConfig(env) {
|
|
136
|
+
const config = loadConfig(env);
|
|
137
|
+
if (!config.cms) {
|
|
138
|
+
throw new Error('CMS module is not enabled');
|
|
139
|
+
}
|
|
140
|
+
return config.cms;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get Newsletter module configuration
|
|
144
|
+
* @throws Error if Newsletter module is not enabled
|
|
145
|
+
*/
|
|
146
|
+
export function getNewsletterConfig(env) {
|
|
147
|
+
const config = loadConfig(env);
|
|
148
|
+
if (!config.newsletter) {
|
|
149
|
+
throw new Error('Newsletter module is not enabled');
|
|
150
|
+
}
|
|
151
|
+
return config.newsletter;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get Shop module configuration
|
|
155
|
+
* @throws Error if Shop module is not enabled
|
|
156
|
+
*/
|
|
157
|
+
export function getShopConfig(env) {
|
|
158
|
+
const config = loadConfig(env);
|
|
159
|
+
if (!config.shop) {
|
|
160
|
+
throw new Error('Shop module is not enabled');
|
|
161
|
+
}
|
|
162
|
+
return config.shop;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get Builder module configuration
|
|
166
|
+
* @throws Error if Builder module is not enabled
|
|
167
|
+
*/
|
|
168
|
+
export function getBuilderConfig(env) {
|
|
169
|
+
const config = loadConfig(env);
|
|
170
|
+
if (!config.builder) {
|
|
171
|
+
throw new Error('Builder module is not enabled');
|
|
172
|
+
}
|
|
173
|
+
return config.builder;
|
|
174
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Email sending utilities using Resend API
|
|
3
|
+
*/
|
|
4
|
+
interface SendMagicLinkParams {
|
|
5
|
+
to: string;
|
|
6
|
+
fromEmail: string;
|
|
7
|
+
resendApiKey: string;
|
|
8
|
+
magicLinkUrl: string;
|
|
9
|
+
userGroup?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Send a magic link email for passwordless authentication
|
|
13
|
+
*/
|
|
14
|
+
export declare function sendMagicLinkEmail(params: SendMagicLinkParams): Promise<void>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=email.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email.d.ts","sourceRoot":"","sources":["../../src/lib/email.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,UAAU,mBAAmB;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CAiFnF"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Email sending utilities using Resend API
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Send a magic link email for passwordless authentication
|
|
6
|
+
*/
|
|
7
|
+
export async function sendMagicLinkEmail(params) {
|
|
8
|
+
const { to, fromEmail, resendApiKey, magicLinkUrl, userGroup } = params;
|
|
9
|
+
const subject = 'Sign in to hintergrund';
|
|
10
|
+
const groupInfo = userGroup ? ` as ${userGroup}` : '';
|
|
11
|
+
const html = `
|
|
12
|
+
<!DOCTYPE html>
|
|
13
|
+
<html>
|
|
14
|
+
<head>
|
|
15
|
+
<meta charset="utf-8">
|
|
16
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
17
|
+
<title>Sign in to hintergrund</title>
|
|
18
|
+
</head>
|
|
19
|
+
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; color: #24292f; max-width: 560px; margin: 0 auto; padding: 20px;">
|
|
20
|
+
<div style="text-align: center; margin-bottom: 32px;">
|
|
21
|
+
<svg width="48" height="48" viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg">
|
|
22
|
+
<path d="M2 2h60v60H2z" fill="#000000"></path>
|
|
23
|
+
</svg>
|
|
24
|
+
</div>
|
|
25
|
+
|
|
26
|
+
<h1 style="font-size: 24px; font-weight: 600; text-align: center; margin-bottom: 24px;">
|
|
27
|
+
Sign in to hintergrund${groupInfo}
|
|
28
|
+
</h1>
|
|
29
|
+
|
|
30
|
+
<p style="margin-bottom: 24px; text-align: center; color: #57606a;">
|
|
31
|
+
Click the button below to sign in. This link will expire in 15 minutes.
|
|
32
|
+
</p>
|
|
33
|
+
|
|
34
|
+
<div style="text-align: center; margin-bottom: 32px;">
|
|
35
|
+
<a href="${magicLinkUrl}" style="display: inline-block; background-color: #1f883d; color: white; text-decoration: none; padding: 12px 24px; border-radius: 6px; font-weight: 500; font-size: 16px;">
|
|
36
|
+
Sign in to hintergrund
|
|
37
|
+
</a>
|
|
38
|
+
</div>
|
|
39
|
+
|
|
40
|
+
<p style="font-size: 14px; color: #57606a; text-align: center; margin-bottom: 16px;">
|
|
41
|
+
If you didn't request this email, you can safely ignore it.
|
|
42
|
+
</p>
|
|
43
|
+
|
|
44
|
+
<hr style="border: none; border-top: 1px solid #d0d7de; margin: 32px 0;">
|
|
45
|
+
|
|
46
|
+
<p style="font-size: 12px; color: #8c959f; text-align: center;">
|
|
47
|
+
If the button doesn't work, copy and paste this link into your browser:
|
|
48
|
+
</p>
|
|
49
|
+
<p style="font-size: 12px; color: #0969da; text-align: center; word-break: break-all;">
|
|
50
|
+
${magicLinkUrl}
|
|
51
|
+
</p>
|
|
52
|
+
</body>
|
|
53
|
+
</html>
|
|
54
|
+
`;
|
|
55
|
+
const text = `
|
|
56
|
+
Sign in to hintergrund${groupInfo}
|
|
57
|
+
|
|
58
|
+
Click the link below to sign in. This link will expire in 15 minutes.
|
|
59
|
+
|
|
60
|
+
${magicLinkUrl}
|
|
61
|
+
|
|
62
|
+
If you didn't request this email, you can safely ignore it.
|
|
63
|
+
`;
|
|
64
|
+
const response = await fetch('https://api.resend.com/emails', {
|
|
65
|
+
method: 'POST',
|
|
66
|
+
headers: {
|
|
67
|
+
'Authorization': `Bearer ${resendApiKey}`,
|
|
68
|
+
'Content-Type': 'application/json',
|
|
69
|
+
},
|
|
70
|
+
body: JSON.stringify({
|
|
71
|
+
from: fromEmail,
|
|
72
|
+
to: [to],
|
|
73
|
+
subject,
|
|
74
|
+
html,
|
|
75
|
+
text,
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
const error = await response.text();
|
|
80
|
+
console.error('Resend API error:', error);
|
|
81
|
+
throw new Error(`Failed to send email: ${response.status}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../src/lib/template.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAE,MAAW,GAAG,MAAM,CAqO3F"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
export function getTemplate(element, uiBaseUrl, params = '') {
|
|
2
|
+
const faviconSvg = `data:image/svg+xml,${encodeURIComponent('<svg width="48" height="48" viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg"><path d="M2 2h60v60H2z" fill="#000000"/></svg>')}`;
|
|
3
|
+
if (element === 'login') {
|
|
4
|
+
return `
|
|
5
|
+
<!doctype html>
|
|
6
|
+
<html>
|
|
7
|
+
<head>
|
|
8
|
+
<meta charset="utf-8">
|
|
9
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
10
|
+
<title>Login</title>
|
|
11
|
+
<link rel="icon" type="image/svg+xml" href="${faviconSvg}">
|
|
12
|
+
<style>
|
|
13
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
14
|
+
body { margin: 0; height: 100vh; }
|
|
15
|
+
</style>
|
|
16
|
+
<link rel="stylesheet" href="${uiBaseUrl}/ui.css">
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<hintergrund-login></hintergrund-login>
|
|
20
|
+
<script type="module" src="${uiBaseUrl}/login.js"></script>
|
|
21
|
+
</body>
|
|
22
|
+
</html>
|
|
23
|
+
`;
|
|
24
|
+
}
|
|
25
|
+
if (element === 'admin') {
|
|
26
|
+
return `
|
|
27
|
+
<!doctype html>
|
|
28
|
+
<html>
|
|
29
|
+
<head>
|
|
30
|
+
<meta charset="utf-8">
|
|
31
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
32
|
+
<title>Hintergrund Admin</title>
|
|
33
|
+
<meta name="description" content="Admin Panel for Hintergrund">
|
|
34
|
+
<link rel="icon" type="image/svg+xml" href="${faviconSvg}">
|
|
35
|
+
<style>
|
|
36
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
37
|
+
body { margin: 0; height: 100vh; }
|
|
38
|
+
</style>
|
|
39
|
+
<link rel="stylesheet" href="${uiBaseUrl}/ui.css">
|
|
40
|
+
</head>
|
|
41
|
+
<body>
|
|
42
|
+
<hintergrund-admin ${params}></hintergrund-admin>
|
|
43
|
+
<script type="module" src="${uiBaseUrl}/admin.js"></script>
|
|
44
|
+
</body>
|
|
45
|
+
</html>
|
|
46
|
+
`;
|
|
47
|
+
}
|
|
48
|
+
if (element === 'unsubscribe') {
|
|
49
|
+
return `
|
|
50
|
+
<!doctype html>
|
|
51
|
+
<html>
|
|
52
|
+
<head>
|
|
53
|
+
<meta charset="utf-8">
|
|
54
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
55
|
+
<title>Unsubscribe</title>
|
|
56
|
+
<link rel="icon" type="image/svg+xml" href="${faviconSvg}">
|
|
57
|
+
<style>
|
|
58
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
59
|
+
body {
|
|
60
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
|
61
|
+
background: #f6f8fa;
|
|
62
|
+
display: flex;
|
|
63
|
+
align-items: center;
|
|
64
|
+
justify-content: center;
|
|
65
|
+
min-height: 100vh;
|
|
66
|
+
padding: 1rem;
|
|
67
|
+
}
|
|
68
|
+
.container {
|
|
69
|
+
background: white;
|
|
70
|
+
border-radius: 8px;
|
|
71
|
+
border: 1px solid #d0d7de;
|
|
72
|
+
padding: 2rem;
|
|
73
|
+
max-width: 480px;
|
|
74
|
+
width: 100%;
|
|
75
|
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
|
|
76
|
+
}
|
|
77
|
+
h1 {
|
|
78
|
+
font-size: 1.5rem;
|
|
79
|
+
font-weight: 600;
|
|
80
|
+
margin-bottom: 1rem;
|
|
81
|
+
color: #24292f;
|
|
82
|
+
}
|
|
83
|
+
p {
|
|
84
|
+
color: #57606a;
|
|
85
|
+
line-height: 1.5;
|
|
86
|
+
margin-bottom: 1.5rem;
|
|
87
|
+
}
|
|
88
|
+
.buttons {
|
|
89
|
+
display: flex;
|
|
90
|
+
gap: 0.75rem;
|
|
91
|
+
}
|
|
92
|
+
button {
|
|
93
|
+
flex: 1;
|
|
94
|
+
padding: 0.75rem 1rem;
|
|
95
|
+
font-size: 0.875rem;
|
|
96
|
+
font-weight: 500;
|
|
97
|
+
border-radius: 6px;
|
|
98
|
+
border: 1px solid;
|
|
99
|
+
cursor: pointer;
|
|
100
|
+
transition: all 0.15s ease;
|
|
101
|
+
}
|
|
102
|
+
.btn-primary {
|
|
103
|
+
background: #cf222e;
|
|
104
|
+
color: white;
|
|
105
|
+
border-color: #cf222e;
|
|
106
|
+
}
|
|
107
|
+
.btn-primary:hover {
|
|
108
|
+
background: #a40e26;
|
|
109
|
+
border-color: #a40e26;
|
|
110
|
+
}
|
|
111
|
+
.btn-secondary {
|
|
112
|
+
background: #f6f8fa;
|
|
113
|
+
color: #24292f;
|
|
114
|
+
border-color: #d0d7de;
|
|
115
|
+
}
|
|
116
|
+
.btn-secondary:hover {
|
|
117
|
+
background: #f3f4f6;
|
|
118
|
+
border-color: #b1b9c2;
|
|
119
|
+
}
|
|
120
|
+
.message {
|
|
121
|
+
padding: 1rem;
|
|
122
|
+
border-radius: 6px;
|
|
123
|
+
margin-bottom: 1rem;
|
|
124
|
+
display: none;
|
|
125
|
+
}
|
|
126
|
+
.message.success {
|
|
127
|
+
background: #dafbe1;
|
|
128
|
+
color: #1a7f37;
|
|
129
|
+
border: 1px solid #1a7f37;
|
|
130
|
+
}
|
|
131
|
+
.message.error {
|
|
132
|
+
background: #ffebe9;
|
|
133
|
+
color: #cf222e;
|
|
134
|
+
border: 1px solid #cf222e;
|
|
135
|
+
}
|
|
136
|
+
.spinner {
|
|
137
|
+
display: none;
|
|
138
|
+
border: 2px solid #f3f4f6;
|
|
139
|
+
border-top: 2px solid #cf222e;
|
|
140
|
+
border-radius: 50%;
|
|
141
|
+
width: 16px;
|
|
142
|
+
height: 16px;
|
|
143
|
+
animation: spin 0.6s linear infinite;
|
|
144
|
+
margin: 0 auto;
|
|
145
|
+
}
|
|
146
|
+
@keyframes spin {
|
|
147
|
+
0% { transform: rotate(0deg); }
|
|
148
|
+
100% { transform: rotate(360deg); }
|
|
149
|
+
}
|
|
150
|
+
</style>
|
|
151
|
+
</head>
|
|
152
|
+
<body>
|
|
153
|
+
<div class="container">
|
|
154
|
+
<h1>Unsubscribe from Newsletter</h1>
|
|
155
|
+
<div id="message" class="message"></div>
|
|
156
|
+
<div id="content">
|
|
157
|
+
<p>Are you sure you want to unsubscribe from our newsletter?</p>
|
|
158
|
+
<div class="buttons">
|
|
159
|
+
<button class="btn-secondary" onclick="window.close()">Cancel</button>
|
|
160
|
+
<button class="btn-primary" onclick="handleUnsubscribe()">Unsubscribe</button>
|
|
161
|
+
</div>
|
|
162
|
+
</div>
|
|
163
|
+
<div id="spinner" class="spinner"></div>
|
|
164
|
+
</div>
|
|
165
|
+
<script>
|
|
166
|
+
${params}
|
|
167
|
+
|
|
168
|
+
async function handleUnsubscribe() {
|
|
169
|
+
const content = document.getElementById('content');
|
|
170
|
+
const spinner = document.getElementById('spinner');
|
|
171
|
+
const message = document.getElementById('message');
|
|
172
|
+
|
|
173
|
+
content.style.display = 'none';
|
|
174
|
+
spinner.style.display = 'block';
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
const response = await fetch('/api/newsletter/unsubscribe', {
|
|
178
|
+
method: 'POST',
|
|
179
|
+
headers: { 'Content-Type': 'application/json' },
|
|
180
|
+
body: JSON.stringify({ token })
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const data = await response.json();
|
|
184
|
+
spinner.style.display = 'none';
|
|
185
|
+
|
|
186
|
+
if (data.success) {
|
|
187
|
+
message.className = 'message success';
|
|
188
|
+
message.textContent = data.message || 'You have been unsubscribed successfully.';
|
|
189
|
+
message.style.display = 'block';
|
|
190
|
+
} else {
|
|
191
|
+
message.className = 'message error';
|
|
192
|
+
message.textContent = data.message || 'Failed to unsubscribe. Please try again.';
|
|
193
|
+
message.style.display = 'block';
|
|
194
|
+
content.style.display = 'block';
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
spinner.style.display = 'none';
|
|
198
|
+
message.className = 'message error';
|
|
199
|
+
message.textContent = 'Network error. Please try again.';
|
|
200
|
+
message.style.display = 'block';
|
|
201
|
+
content.style.display = 'block';
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
</script>
|
|
205
|
+
</body>
|
|
206
|
+
</html>
|
|
207
|
+
`;
|
|
208
|
+
}
|
|
209
|
+
return `
|
|
210
|
+
<!doctype html>
|
|
211
|
+
<html>
|
|
212
|
+
<head>
|
|
213
|
+
<meta charset="utf-8">
|
|
214
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
215
|
+
<title>Hintergrund CMS</title>
|
|
216
|
+
<meta name="description" content="Admin Panel for Hintergrund CMS">
|
|
217
|
+
<link rel="icon" type="image/svg+xml" href="${faviconSvg}">
|
|
218
|
+
<style>body{margin:0}</style>
|
|
219
|
+
</head>
|
|
220
|
+
<body>
|
|
221
|
+
<${element} ${params}></${element}>
|
|
222
|
+
<script type="module" src="/index.js"></script>
|
|
223
|
+
</body>
|
|
224
|
+
</html>
|
|
225
|
+
`;
|
|
226
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Context, Next } from 'hono';
|
|
2
|
+
export declare function authMiddleware(c: Context, next: Next): Promise<void | (Response & import("hono").TypedResponse<{
|
|
3
|
+
error: string;
|
|
4
|
+
}, 500, "json">) | (Response & import("hono").TypedResponse<"Server configuration error: No configuration found", 500, "text">) | (Response & import("hono").TypedResponse<{
|
|
5
|
+
error: string;
|
|
6
|
+
}, 403, "json">) | (Response & import("hono").TypedResponse<"Access denied to this module", 403, "text">) | (Response & import("hono").TypedResponse<{
|
|
7
|
+
error: string;
|
|
8
|
+
}, 401, "json">) | (Response & import("hono").TypedResponse<undefined, 302, "redirect">)>;
|
|
9
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAyCrC,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI;;;;;;0FAuE1D"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { parse } from 'cookie';
|
|
2
|
+
import { verify } from '../lib/auth/jwt';
|
|
3
|
+
import { loadConfig } from '../lib/config';
|
|
4
|
+
/**
|
|
5
|
+
* Determines which module is being accessed based on the pathname
|
|
6
|
+
*/
|
|
7
|
+
function getModuleFromPath(pathname) {
|
|
8
|
+
if (pathname.includes('/api/admin/cms') || pathname.includes('/api/admin/collections') || pathname.includes('/api/admin/assets')) {
|
|
9
|
+
return 'cms';
|
|
10
|
+
}
|
|
11
|
+
if (pathname.includes('/api/admin/newsletter')) {
|
|
12
|
+
return 'newsletter';
|
|
13
|
+
}
|
|
14
|
+
if (pathname.includes('/api/admin/shop')) {
|
|
15
|
+
return 'shop';
|
|
16
|
+
}
|
|
17
|
+
if (pathname.includes('/api/admin/builder')) {
|
|
18
|
+
return 'builder';
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Check if user has access to a specific module based on their group
|
|
24
|
+
*/
|
|
25
|
+
function hasModuleAccess(userModules, targetModule) {
|
|
26
|
+
// If no target module, allow access (general admin routes)
|
|
27
|
+
if (!targetModule)
|
|
28
|
+
return true;
|
|
29
|
+
// If user has no module restrictions, allow all
|
|
30
|
+
if (!userModules || userModules.length === 0)
|
|
31
|
+
return true;
|
|
32
|
+
// Check if the target module is in the allowed list
|
|
33
|
+
return userModules.includes(targetModule);
|
|
34
|
+
}
|
|
35
|
+
export async function authMiddleware(c, next) {
|
|
36
|
+
const { pathname } = new URL(c.req.url);
|
|
37
|
+
console.log('[AUTH MIDDLEWARE] Checking:', pathname);
|
|
38
|
+
// Skip authentication for login, logout, and magic link verification routes
|
|
39
|
+
if (pathname === '/login' ||
|
|
40
|
+
pathname === '/api/admin/login' ||
|
|
41
|
+
pathname === '/api/admin/logout' ||
|
|
42
|
+
pathname === '/api/admin/verify-magic-link') {
|
|
43
|
+
console.log('[AUTH MIDDLEWARE] Skipping auth for:', pathname);
|
|
44
|
+
return await next();
|
|
45
|
+
}
|
|
46
|
+
// Check if configuration exists
|
|
47
|
+
if (!c.env.HG_CONFIG && !c.env.HG_SECRET) {
|
|
48
|
+
console.error('No configuration found. Set HG_SECRET/HG_PASSWORD or HG_CONFIG');
|
|
49
|
+
if (pathname.startsWith('/api/')) {
|
|
50
|
+
return c.json({ error: 'Server configuration error' }, 500);
|
|
51
|
+
}
|
|
52
|
+
return c.text('Server configuration error: No configuration found', 500);
|
|
53
|
+
}
|
|
54
|
+
const cookie = parse(c.req.header('cookie') || '');
|
|
55
|
+
const token = cookie.token;
|
|
56
|
+
if (token != null) {
|
|
57
|
+
try {
|
|
58
|
+
const appConfig = loadConfig(c.env);
|
|
59
|
+
const payload = await verify(token, appConfig.global.hgSecret);
|
|
60
|
+
if (payload) {
|
|
61
|
+
// Token is valid - check module access
|
|
62
|
+
const targetModule = getModuleFromPath(pathname);
|
|
63
|
+
const userModules = payload.modules;
|
|
64
|
+
if (!hasModuleAccess(userModules, targetModule)) {
|
|
65
|
+
console.log('[AUTH MIDDLEWARE] User does not have access to module:', targetModule);
|
|
66
|
+
if (pathname.startsWith('/api/')) {
|
|
67
|
+
return c.json({ error: 'Access denied to this module' }, 403);
|
|
68
|
+
}
|
|
69
|
+
return c.text('Access denied to this module', 403);
|
|
70
|
+
}
|
|
71
|
+
// Store user info in context for downstream handlers
|
|
72
|
+
c.set('user', {
|
|
73
|
+
email: payload.email,
|
|
74
|
+
userGroup: payload.userGroup,
|
|
75
|
+
modules: userModules,
|
|
76
|
+
});
|
|
77
|
+
return await next();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
console.error('Token verification error:', error);
|
|
82
|
+
// Continue to unauthorized handling
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// No cookie or incorrect hash in cookie
|
|
86
|
+
console.log('[AUTH MIDDLEWARE] No valid token, redirecting to login');
|
|
87
|
+
// Check if this is an API request
|
|
88
|
+
if (pathname.startsWith('/api/')) {
|
|
89
|
+
return c.json({ error: 'Unauthorized' }, 401);
|
|
90
|
+
}
|
|
91
|
+
// For non-API requests, redirect to login
|
|
92
|
+
return c.redirect('/login');
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../src/template.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAE,MAAW,GAAG,MAAM,CA8D3F"}
|