create-sprint 0.0.52 → 0.0.54

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.
@@ -200,8 +200,8 @@ export const jwtValidateController: Handler = (req: SprintRequest, res: SprintRe
200
200
  }
201
201
 
202
202
  try {
203
- const { publicKey } = getJwtFromEnv();
204
- const decoded = verifyEncrypted(token, publicKey);
203
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
204
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);
205
205
 
206
206
  if (!decoded) {
207
207
  return res.status(401).json({ error: "Invalid token" });
@@ -232,8 +232,8 @@ export const jwtValidateController = (req: SprintRequest, res: SprintResponse) =
232
232
  }
233
233
 
234
234
  try {
235
- const { publicKey } = getJwtFromEnv();
236
- const decoded = verifyEncrypted(token, publicKey);
235
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
236
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);
237
237
 
238
238
  if (!decoded) {
239
239
  return res.status(401).json({ error: "Invalid token" });
@@ -273,7 +273,7 @@ export const jwtGenerateController: Handler = (req: SprintRequest, res: SprintRe
273
273
  try {
274
274
  const { privateKey } = getJwtFromEnv();
275
275
  const payload = { userId, role: role || "user" };
276
- const token = signEncrypted(payload, privateKey, { expiresIn: "1h" });
276
+ const token = signEncrypted(payload, privateKey, encryptionSecret, { expiresIn: "1h" });
277
277
  res.json({ token });
278
278
  } catch (error) {
279
279
  return res.status(500).json({ error: "JWT not configured" });
@@ -304,9 +304,9 @@ export const jwtGenerateController = (req: SprintRequest, res: SprintResponse) =
304
304
  const { userId, role } = req.body || {};
305
305
 
306
306
  try {
307
- const { privateKey } = getJwtFromEnv();
307
+ const { privateKey, encryptionSecret } = getJwtFromEnv();
308
308
  const payload = { userId, role: role || "user" };
309
- const token = signEncrypted(payload, privateKey, { expiresIn: "1h" });
309
+ const token = signEncrypted(payload, privateKey, encryptionSecret, { expiresIn: "1h" });
310
310
  res.json({ token });
311
311
  } catch (error) {
312
312
  return res.status(500).json({ error: "JWT not configured" });
@@ -368,26 +368,21 @@ export const jwtGenerateSchema = defineRouteSchema({
368
368
  });
369
369
  `;
370
370
  }
371
- export function getAuthMiddleware(language) {
371
+ export function getInternalAuthMiddleware(language) {
372
372
  if (language === "typescript") {
373
373
  return `import { defineMiddleware } from "sprint-es";
374
374
 
375
375
  export default defineMiddleware({
376
- name: "auth",
376
+ name: "adminAuth",
377
377
  priority: 10,
378
378
  include: "/admin/**",
379
379
  handler: (req, res, next) => {
380
380
  const auth = req.sprint.getAuthorization();
381
-
382
- if (!auth) {
383
- return res.status(401).json({ error: "No authorization header" });
384
- }
381
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
385
382
 
386
383
  const token = auth.replace("Bearer ", "");
387
384
 
388
- if (token !== "admin-token") {
389
- return res.status(403).json({ error: "Invalid token" });
390
- }
385
+ if (token !== "admin-token") return res.status(403).json({ error: "Invalid token" });
391
386
 
392
387
  next();
393
388
  }
@@ -397,21 +392,72 @@ export default defineMiddleware({
397
392
  return `import { defineMiddleware } from "sprint-es";
398
393
 
399
394
  export default defineMiddleware({
400
- name: "auth",
395
+ name: "adminAuth",
401
396
  priority: 10,
402
397
  include: "/admin/**",
403
398
  handler: (req, res, next) => {
404
399
  const auth = req.sprint.getAuthorization();
400
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
401
+
402
+ const token = auth.replace("Bearer ", "");
405
403
 
406
- if (!auth) {
407
- return res.status(401).json({ error: "No authorization header" });
408
- }
404
+ if (token !== "admin-token") return res.status(403).json({ error: "Invalid token" });
405
+
406
+ next();
407
+ }
408
+ });
409
+ `;
410
+ }
411
+ export function getUserAuthMiddleware(language) {
412
+ if (language === "typescript") {
413
+ return `import { defineMiddleware } from "sprint-es";
414
+ import { verifyEncrypted, getJwtFromEnv } from "sprint-es/jwt";
415
+
416
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
417
+
418
+ export default defineMiddleware({
419
+ name: "userAuth",
420
+ priority: 10,
421
+ include: "/**",
422
+ exclude: "/admin/**",
423
+ handler: (req, res, next) => {
424
+ const auth = req.sprint.getAuthorization();
425
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
409
426
 
410
427
  const token = auth.replace("Bearer ", "");
411
428
 
412
- if (token !== "admin-token") {
413
- return res.status(403).json({ error: "Invalid token" });
414
- }
429
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);cd .
430
+
431
+ if (!decoded) return res.status(403).json({ error: "Invalid token" });
432
+
433
+ req.custom.user = decoded;
434
+
435
+ next();
436
+ }
437
+ });
438
+ `;
439
+ }
440
+ return `import { defineMiddleware } from "sprint-es";
441
+ import { verifyEncrypted, getJwtFromEnv } from "sprint-es/jwt";
442
+
443
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
444
+
445
+ export default defineMiddleware({
446
+ name: "userAuth",
447
+ priority: 10,
448
+ include: "/**",
449
+ exclude: "/admin/**",
450
+ handler: (req, res, next) => {
451
+ const auth = req.sprint.getAuthorization();
452
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
453
+
454
+ const token = auth.replace("Bearer ", "");
455
+
456
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);
457
+
458
+ if (!decoded) return res.status(403).json({ error: "Invalid token" });
459
+
460
+ req.custom.user = decoded;
415
461
 
416
462
  next();
417
463
  }
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import { join } from "path";
5
5
  import color from "picocolors";
6
6
  import * as p from "@clack/prompts";
7
7
  import { validateProjectName } from "./validators.js";
8
- import { getTypeScriptPackageJson, getJavaScriptPackageJson, getTsConfig, getViteConfig, getMainFile, getHomeRoute, getAdminRoute, getHomeController, getAdminController, getAuthMiddleware, getHomeSchema, getAdminSchema, getDockerfile, getDockerCompose, getGitignore, getDockerIgnore, getSprintConfigFile, getEnvDevelopment, getEnvProduction, getExampleCronJob } from "./generators.js";
8
+ import { getTypeScriptPackageJson, getJavaScriptPackageJson, getTsConfig, getViteConfig, getMainFile, getHomeRoute, getAdminRoute, getHomeController, getAdminController, getInternalAuthMiddleware, getUserAuthMiddleware, getHomeSchema, getAdminSchema, getDockerfile, getDockerCompose, getGitignore, getDockerIgnore, getSprintConfigFile, getEnvDevelopment, getEnvProduction, getExampleCronJob } from "./generators.js";
9
9
  export async function writeFile(path, content, options) {
10
10
  if (typeof content === "string")
11
11
  content = content.trimEnd();
@@ -158,7 +158,8 @@ async function createProject(projectName, language, telemetry, useDocker) {
158
158
  await writeFile(join(srcDir, "routes", "admin." + (language === "typescript" ? "ts" : "js")), getAdminRoute(language));
159
159
  await writeFile(join(srcDir, "controllers", "home." + (language === "typescript" ? "ts" : "js")), getHomeController(language));
160
160
  await writeFile(join(srcDir, "controllers", "admin." + (language === "typescript" ? "ts" : "js")), getAdminController(language));
161
- await writeFile(join(srcDir, "middlewares", "auth." + (language === "typescript" ? "ts" : "js")), getAuthMiddleware(language));
161
+ await writeFile(join(srcDir, "middlewares", "auth.internal." + (language === "typescript" ? "ts" : "js")), getInternalAuthMiddleware(language));
162
+ await writeFile(join(srcDir, "middlewares", "auth.user." + (language === "typescript" ? "ts" : "js")), getUserAuthMiddleware(language));
162
163
  await writeFile(join(srcDir, "schemas", "home." + (language === "typescript" ? "ts" : "js")), getHomeSchema(language));
163
164
  await writeFile(join(srcDir, "schemas", "admin." + (language === "typescript" ? "ts" : "js")), getAdminSchema(language));
164
165
  await writeFile(join(srcDir, "cronjobs", "example." + (language === "typescript" ? "ts" : "js")), getExampleCronJob(language));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sprint",
3
- "version": "0.0.52",
3
+ "version": "0.0.54",
4
4
  "description": "Create a new Sprint API project",
5
5
  "type": "module",
6
6
  "bin": {
package/src/generators.ts CHANGED
@@ -218,8 +218,8 @@ export const jwtValidateController: Handler = (req: SprintRequest, res: SprintRe
218
218
  }
219
219
 
220
220
  try {
221
- const { publicKey } = getJwtFromEnv();
222
- const decoded = verifyEncrypted(token, publicKey);
221
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
222
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);
223
223
 
224
224
  if (!decoded) {
225
225
  return res.status(401).json({ error: "Invalid token" });
@@ -250,8 +250,8 @@ export const jwtValidateController = (req: SprintRequest, res: SprintResponse) =
250
250
  }
251
251
 
252
252
  try {
253
- const { publicKey } = getJwtFromEnv();
254
- const decoded = verifyEncrypted(token, publicKey);
253
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
254
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);
255
255
 
256
256
  if (!decoded) {
257
257
  return res.status(401).json({ error: "Invalid token" });
@@ -292,7 +292,7 @@ export const jwtGenerateController: Handler = (req: SprintRequest, res: SprintRe
292
292
  try {
293
293
  const { privateKey } = getJwtFromEnv();
294
294
  const payload = { userId, role: role || "user" };
295
- const token = signEncrypted(payload, privateKey, { expiresIn: "1h" });
295
+ const token = signEncrypted(payload, privateKey, encryptionSecret, { expiresIn: "1h" });
296
296
  res.json({ token });
297
297
  } catch (error) {
298
298
  return res.status(500).json({ error: "JWT not configured" });
@@ -323,9 +323,9 @@ export const jwtGenerateController = (req: SprintRequest, res: SprintResponse) =
323
323
  const { userId, role } = req.body || {};
324
324
 
325
325
  try {
326
- const { privateKey } = getJwtFromEnv();
326
+ const { privateKey, encryptionSecret } = getJwtFromEnv();
327
327
  const payload = { userId, role: role || "user" };
328
- const token = signEncrypted(payload, privateKey, { expiresIn: "1h" });
328
+ const token = signEncrypted(payload, privateKey, encryptionSecret, { expiresIn: "1h" });
329
329
  res.json({ token });
330
330
  } catch (error) {
331
331
  return res.status(500).json({ error: "JWT not configured" });
@@ -391,26 +391,21 @@ export const jwtGenerateSchema = defineRouteSchema({
391
391
  `;
392
392
  }
393
393
 
394
- export function getAuthMiddleware(language: string) {
394
+ export function getInternalAuthMiddleware(language: string) {
395
395
  if (language === "typescript") {
396
396
  return `import { defineMiddleware } from "sprint-es";
397
397
 
398
398
  export default defineMiddleware({
399
- name: "auth",
399
+ name: "adminAuth",
400
400
  priority: 10,
401
401
  include: "/admin/**",
402
402
  handler: (req, res, next) => {
403
403
  const auth = req.sprint.getAuthorization();
404
-
405
- if (!auth) {
406
- return res.status(401).json({ error: "No authorization header" });
407
- }
404
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
408
405
 
409
406
  const token = auth.replace("Bearer ", "");
410
407
 
411
- if (token !== "admin-token") {
412
- return res.status(403).json({ error: "Invalid token" });
413
- }
408
+ if (token !== "admin-token") return res.status(403).json({ error: "Invalid token" });
414
409
 
415
410
  next();
416
411
  }
@@ -420,21 +415,73 @@ export default defineMiddleware({
420
415
  return `import { defineMiddleware } from "sprint-es";
421
416
 
422
417
  export default defineMiddleware({
423
- name: "auth",
418
+ name: "adminAuth",
424
419
  priority: 10,
425
420
  include: "/admin/**",
426
421
  handler: (req, res, next) => {
427
422
  const auth = req.sprint.getAuthorization();
423
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
424
+
425
+ const token = auth.replace("Bearer ", "");
428
426
 
429
- if (!auth) {
430
- return res.status(401).json({ error: "No authorization header" });
431
- }
427
+ if (token !== "admin-token") return res.status(403).json({ error: "Invalid token" });
428
+
429
+ next();
430
+ }
431
+ });
432
+ `;
433
+ }
434
+
435
+ export function getUserAuthMiddleware(language: string) {
436
+ if (language === "typescript") {
437
+ return `import { defineMiddleware } from "sprint-es";
438
+ import { verifyEncrypted, getJwtFromEnv } from "sprint-es/jwt";
439
+
440
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
441
+
442
+ export default defineMiddleware({
443
+ name: "userAuth",
444
+ priority: 10,
445
+ include: "/**",
446
+ exclude: "/admin/**",
447
+ handler: (req, res, next) => {
448
+ const auth = req.sprint.getAuthorization();
449
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
432
450
 
433
451
  const token = auth.replace("Bearer ", "");
434
452
 
435
- if (token !== "admin-token") {
436
- return res.status(403).json({ error: "Invalid token" });
437
- }
453
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);cd .
454
+
455
+ if (!decoded) return res.status(403).json({ error: "Invalid token" });
456
+
457
+ req.custom.user = decoded;
458
+
459
+ next();
460
+ }
461
+ });
462
+ `;
463
+ }
464
+ return `import { defineMiddleware } from "sprint-es";
465
+ import { verifyEncrypted, getJwtFromEnv } from "sprint-es/jwt";
466
+
467
+ const { publicKey, encryptionSecret } = getJwtFromEnv();
468
+
469
+ export default defineMiddleware({
470
+ name: "userAuth",
471
+ priority: 10,
472
+ include: "/**",
473
+ exclude: "/admin/**",
474
+ handler: (req, res, next) => {
475
+ const auth = req.sprint.getAuthorization();
476
+ if (!auth) return res.status(401).json({ error: "No authorization header" });
477
+
478
+ const token = auth.replace("Bearer ", "");
479
+
480
+ const decoded = verifyEncrypted(token, publicKey, encryptionSecret);
481
+
482
+ if (!decoded) return res.status(403).json({ error: "Invalid token" });
483
+
484
+ req.custom.user = decoded;
438
485
 
439
486
  next();
440
487
  }
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@ import { join } from "path";
5
5
  import color from "picocolors";
6
6
  import * as p from "@clack/prompts";
7
7
  import { validateProjectName } from "./validators.js";
8
- import { getTypeScriptPackageJson, getJavaScriptPackageJson, getTsConfig, getViteConfig, getMainFile, getHomeRoute, getAdminRoute, getHomeController, getAdminController, getAuthMiddleware, getHomeSchema, getAdminSchema, getDockerfile, getDockerCompose, getGitignore, getDockerIgnore, getSprintConfigFile, getEnvDevelopment, getEnvProduction, getExampleCronJob } from "./generators.js";
8
+ import { getTypeScriptPackageJson, getJavaScriptPackageJson, getTsConfig, getViteConfig, getMainFile, getHomeRoute, getAdminRoute, getHomeController, getAdminController, getInternalAuthMiddleware, getUserAuthMiddleware, getHomeSchema, getAdminSchema, getDockerfile, getDockerCompose, getGitignore, getDockerIgnore, getSprintConfigFile, getEnvDevelopment, getEnvProduction, getExampleCronJob } from "./generators.js";
9
9
 
10
10
  export interface CLIOptions {
11
11
  projectName?: string;
@@ -202,7 +202,8 @@ async function createProject(
202
202
  await writeFile(join(srcDir, "controllers", "home." + (language === "typescript" ? "ts" : "js")), getHomeController(language));
203
203
  await writeFile(join(srcDir, "controllers", "admin." + (language === "typescript" ? "ts" : "js")), getAdminController(language));
204
204
 
205
- await writeFile(join(srcDir, "middlewares", "auth." + (language === "typescript" ? "ts" : "js")), getAuthMiddleware(language));
205
+ await writeFile(join(srcDir, "middlewares", "auth.internal." + (language === "typescript" ? "ts" : "js")), getInternalAuthMiddleware(language));
206
+ await writeFile(join(srcDir, "middlewares", "auth.user." + (language === "typescript" ? "ts" : "js")), getUserAuthMiddleware(language));
206
207
 
207
208
  await writeFile(join(srcDir, "schemas", "home." + (language === "typescript" ? "ts" : "js")), getHomeSchema(language));
208
209
  await writeFile(join(srcDir, "schemas", "admin." + (language === "typescript" ? "ts" : "js")), getAdminSchema(language));