@volcanicminds/backend 0.2.28 → 0.2.30

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.
Files changed (55) hide show
  1. package/README.md +0 -7
  2. package/dist/index.js +39 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/lib/api/auth/controller/auth.js +136 -17
  5. package/dist/lib/api/auth/controller/auth.js.map +1 -1
  6. package/dist/lib/api/auth/routes.js +107 -13
  7. package/dist/lib/api/auth/routes.js.map +1 -1
  8. package/dist/lib/api/health/routes.js +2 -2
  9. package/dist/lib/api/health/routes.js.map +1 -1
  10. package/dist/lib/api/users/routes.js +12 -14
  11. package/dist/lib/api/users/routes.js.map +1 -1
  12. package/dist/lib/hooks/onRequest.js +21 -17
  13. package/dist/lib/hooks/onRequest.js.map +1 -1
  14. package/dist/lib/loader/plugins.js +0 -1
  15. package/dist/lib/loader/plugins.js.map +1 -1
  16. package/dist/lib/loader/router.js +66 -35
  17. package/dist/lib/loader/router.js.map +1 -1
  18. package/dist/lib/loader/schemas.js +8 -3
  19. package/dist/lib/loader/schemas.js.map +1 -1
  20. package/dist/lib/middleware/isAdmin.js +5 -4
  21. package/dist/lib/middleware/isAdmin.js.map +1 -1
  22. package/dist/lib/middleware/isAuthenticated.js +7 -6
  23. package/dist/lib/middleware/isAuthenticated.js.map +1 -1
  24. package/dist/lib/middleware/postAuth.js +19 -0
  25. package/dist/lib/middleware/postAuth.js.map +1 -0
  26. package/dist/lib/middleware/preAuth.js +17 -0
  27. package/dist/lib/middleware/preAuth.js.map +1 -0
  28. package/dist/lib/util/generate.js +10 -0
  29. package/dist/lib/util/generate.js.map +1 -0
  30. package/dist/lib/util/regexp.js +13 -13
  31. package/dist/lib/util/regexp.js.map +1 -1
  32. package/index.d.ts +2 -1
  33. package/index.ts +50 -2
  34. package/lib/api/auth/controller/auth.ts +118 -23
  35. package/lib/api/auth/routes.ts +107 -14
  36. package/lib/api/health/routes.ts +2 -2
  37. package/lib/api/users/routes.ts +12 -14
  38. package/lib/hooks/onRequest.ts +21 -27
  39. package/lib/loader/plugins.ts +0 -1
  40. package/lib/loader/router.ts +71 -34
  41. package/lib/loader/schemas.ts +7 -3
  42. package/lib/middleware/isAdmin.ts +3 -3
  43. package/lib/middleware/isAuthenticated.ts +5 -5
  44. package/lib/middleware/postAuth.ts +5 -0
  45. package/lib/middleware/preAuth.ts +3 -0
  46. package/lib/util/generate.ts +6 -0
  47. package/lib/util/regexp.ts +34 -32
  48. package/package.json +1 -1
  49. package/types/global.d.ts +15 -1
  50. package/dist/lib/api/auth/controller/password.js +0 -23
  51. package/dist/lib/api/auth/controller/password.js.map +0 -1
  52. package/dist/lib/middleware/example.js +0 -13
  53. package/dist/lib/middleware/example.js.map +0 -1
  54. package/lib/api/auth/controller/password.ts +0 -21
  55. package/lib/middleware/example.ts +0 -12
@@ -1,14 +1,14 @@
1
1
  import { FastifyReply, FastifyRequest } from 'fastify'
2
2
 
3
- const log = global.log
4
- module.exports = (req: FastifyRequest, res: FastifyReply, next: any) => {
3
+ export function preHandler(req: FastifyRequest, res: FastifyReply, done: any) {
5
4
  try {
6
5
  if (!!req.user?.id) {
7
- return next()
6
+ return done()
8
7
  }
9
- throw new Error('User not authenticated')
8
+
9
+ throw new Error('Unauthorized')
10
10
  } catch (err) {
11
11
  log.e && log.error(`Upps, something just happened ${err}`)
12
- res.code(403).send(err)
12
+ return res.code(401).send(err) // must be authorized first
13
13
  }
14
14
  }
@@ -0,0 +1,5 @@
1
+ import { FastifyReply, FastifyRequest } from 'fastify'
2
+
3
+ export async function preSerialization(req: FastifyRequest, res: FastifyReply, payload) {
4
+ return payload
5
+ }
@@ -0,0 +1,3 @@
1
+ import { FastifyReply, FastifyRequest } from 'fastify'
2
+
3
+ export async function preHandler(req: FastifyRequest, res: FastifyReply) {}
@@ -0,0 +1,6 @@
1
+ const { customAlphabet } = require('nanoid')
2
+ const nanoid = customAlphabet('AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789')
3
+
4
+ export function newAuthCode() {
5
+ return nanoid(Number(process.env.AUTH_CODE_SIZE) || 10)
6
+ }
@@ -1,32 +1,34 @@
1
- module.exports = {
2
- /*
3
- * min 3 max 33, one special character (. _ -) only in the middle
4
- * username can have uppercase or lowercase chars
5
- */
6
- username: /(?=^.{3,33}$)^[a-z][a-z0-9]*[._-]?[a-z0-9]+$/gi,
7
- emailAlt:
8
- /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
9
- /*
10
- * email can have multiple words
11
- * email can use . - or + for smart labeling
12
- */
13
- email: /^\w+([\.+-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
14
- /*
15
- * password must contain 1 number (0-9)
16
- * password must contain 1 uppercase chars
17
- * password must contain 1 lowercase chars
18
- * password must contain 1 non-alpha number
19
- * password is 8-64 characters with no space
20
- */
21
- password: /^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[^\w\d\s:])([^\s]){8,64}$/gi,
22
- zipCode: /(^\d{5}$)|(^\d{5}-\d{4}$)/,
23
- taxCodePersona: /^[a-zA-Z]{6}[0-9]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9]{2}([a-zA-Z]{1}[0-9]{3})[a-zA-Z]{1}$/,
24
- /*
25
- * taxCode can have 2 letter (IT,DE,..) and 11 digits
26
- */
27
- taxCodeCompany: /^([A-Z]{2}|)[0-9]{11}$/,
28
- iban: /^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$/,
29
- mobilePhone: /^((00|\+)39)?3[0-9]{8,9}$/,
30
- landLinePhone: /^(((00|\+)39))?[\s]?(0{1}[1-9]{1,3})[\s]?(\d{4,6})$/,
31
- tollFreePhone: /^((00|\+)39)?(800|803|167)\d{3,6}$/
32
- }
1
+ /*
2
+ * min 3 max 33, one special character (. _ -) only in the middle
3
+ * username can have uppercase or lowercase chars
4
+ */
5
+ export const username = /(?=^.{3,33}$)^[a-z][a-z0-9]*[._-]?[a-z0-9]+$/gi
6
+ export const emailAlt =
7
+ /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
8
+
9
+ /*
10
+ * email can have multiple words
11
+ * email can use . - or + for smart labeling
12
+ */
13
+ export const email = /^\w+([\.+-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
14
+
15
+ /*
16
+ * password must contain 1 number (0-9)
17
+ * password must contain 1 uppercase chars
18
+ * password must contain 1 lowercase chars
19
+ * password must contain 1 non-alpha number
20
+ * password is 8-64 characters with no space
21
+ */
22
+ export const password = /^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[^\w\d\s:])([^\s]){8,16}$/
23
+ export const zipCode = /(^\d{5}$)|(^\d{5}-\d{4}$)/
24
+ export const taxCodePersona =
25
+ /^[a-zA-Z]{6}[0-9]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9]{2}([a-zA-Z]{1}[0-9]{3})[a-zA-Z]{1}$/
26
+
27
+ /*
28
+ * taxCode can have 2 letter (IT,DE,..) and 11 digits
29
+ */
30
+ export const taxCodeCompany = /^([A-Z]{2}|)[0-9]{11}$/
31
+ export const iban = /^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$/
32
+ export const mobilePhone = /^((00|\+)39)?3[0-9]{8,9}$/
33
+ export const landLinePhone = /^(((00|\+)39))?[\s]?(0{1}[1-9]{1,3})[\s]?(\d{4,6})$/
34
+ export const tollFreePhone = /^((00|\+)39)?(800|803|167)\d{3,6}$/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volcanicminds/backend",
3
- "version": "0.2.28",
3
+ "version": "0.2.30",
4
4
  "codename": "turin",
5
5
  "license": "MIT",
6
6
  "description": "The volcanic (minds) backend",
package/types/global.d.ts CHANGED
@@ -2,7 +2,7 @@ import { FastifyRequest, FastifyReply } from 'fastify'
2
2
 
3
3
  export interface AuthenticatedUser {
4
4
  id: number
5
- name: string
5
+ username: string
6
6
  email: string
7
7
  roles: Role[]
8
8
  }
@@ -68,6 +68,20 @@ export interface ConfiguredRoute {
68
68
  }
69
69
  }
70
70
 
71
+ export interface UserManagement {
72
+ createUser(data: any): any | null
73
+ resetExternalId(data: any): any | null
74
+ updateUserById(id: string, user: any): any | null
75
+ retrieveUserById(id: string): any | null
76
+ retrieveUserByEmail(email: string): any | null
77
+ retrieveUserByExternalId(externalId: string): any | null
78
+ retrieveUserByPassword(email: string, password: string): any | null
79
+ changePassword(email: string, password: string, oldPassword: string): any | null
80
+ enableUserById(id: string): any | null
81
+ disableUserById(id: string): any | null
82
+ isValidUser(data: any): boolean
83
+ }
84
+
71
85
  declare module 'fastify' {
72
86
  export interface FastifyRequest {
73
87
  user?: AuthenticatedUser
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.newAuthCode = exports.valid = exports.compare = exports.hash = void 0;
4
- const { customAlphabet } = require('nanoid');
5
- const nanoid = customAlphabet('AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789');
6
- const bcrypt = require('bcrypt');
7
- function hash(password) {
8
- return bcrypt.hashSync(password, bcrypt.genSaltSync());
9
- }
10
- exports.hash = hash;
11
- function compare(password, encryptedPassword) {
12
- return bcrypt.compareSync(password, encryptedPassword);
13
- }
14
- exports.compare = compare;
15
- function valid(password) {
16
- return true;
17
- }
18
- exports.valid = valid;
19
- function newAuthCode() {
20
- return nanoid(Number(process.env.AUTH_CODE_SIZE) || 10);
21
- }
22
- exports.newAuthCode = newAuthCode;
23
- //# sourceMappingURL=password.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"password.js","sourceRoot":"","sources":["../../../../../lib/api/auth/controller/password.ts"],"names":[],"mappings":";;;AAAA,MAAM,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAC5C,MAAM,MAAM,GAAG,cAAc,CAAC,gEAAgE,CAAC,CAAA;AAE/F,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAGhC,SAAgB,IAAI,CAAC,QAAgB;IACnC,OAAO,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,CAAA;AACxD,CAAC;AAFD,oBAEC;AAED,SAAgB,OAAO,CAAC,QAAgB,EAAE,iBAAyB;IACjE,OAAO,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAA;AACxD,CAAC;AAFD,0BAEC;AAED,SAAgB,KAAK,CAAC,QAAgB;IACpC,OAAO,IAAI,CAAA;AACb,CAAC;AAFD,sBAEC;AAED,SAAgB,WAAW;IACzB,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,CAAA;AACzD,CAAC;AAFD,kCAEC"}
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const log = global.log;
4
- module.exports = (req, res, next) => {
5
- try {
6
- return next();
7
- }
8
- catch (err) {
9
- log.e && log.error(`Upps, something just happened ${err}`);
10
- res.code(403).send(err);
11
- }
12
- };
13
- //# sourceMappingURL=example.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"example.js","sourceRoot":"","sources":["../../../lib/middleware/example.ts"],"names":[],"mappings":";;AAEA,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAA;AACtB,MAAM,CAAC,OAAO,GAAG,CAAC,GAAmB,EAAE,GAAiB,EAAE,IAAS,EAAE,EAAE;IACrE,IAAI;QAEF,OAAO,IAAI,EAAE,CAAA;KACd;IAAC,OAAO,GAAG,EAAE;QACZ,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,iCAAiC,GAAG,EAAE,CAAC,CAAA;QAC1D,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;KACxB;AACH,CAAC,CAAA"}
@@ -1,21 +0,0 @@
1
- const { customAlphabet } = require('nanoid')
2
- const nanoid = customAlphabet('AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789')
3
-
4
- const bcrypt = require('bcrypt')
5
- // const validator = require('../../../loader/validator')
6
-
7
- export function hash(password: string): string {
8
- return bcrypt.hashSync(password, bcrypt.genSaltSync())
9
- }
10
-
11
- export function compare(password: string, encryptedPassword: string): boolean {
12
- return bcrypt.compareSync(password, encryptedPassword)
13
- }
14
-
15
- export function valid(password: string): boolean {
16
- return true //password?.length > 7 && validator.password(password)
17
- }
18
-
19
- export function newAuthCode() {
20
- return nanoid(Number(process.env.AUTH_CODE_SIZE) || 10)
21
- }
@@ -1,12 +0,0 @@
1
- import { FastifyReply, FastifyRequest } from 'fastify'
2
-
3
- const log = global.log
4
- module.exports = (req: FastifyRequest, res: FastifyReply, next: any) => {
5
- try {
6
- // TODO: do something and then you can throw an exception or call next()..
7
- return next()
8
- } catch (err) {
9
- log.e && log.error(`Upps, something just happened ${err}`)
10
- res.code(403).send(err)
11
- }
12
- }