express-generator-typescript 2.4.2 → 2.4.4

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.
@@ -1 +1 @@
1
- {"users":[{"name":"Sean Maxwell","email":"sean.maxwell@gmail.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":1,"id":159123164363},{"name":"Gordan Freeman","email":"gordan.freeman@halflife.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":0,"id":906524522143},{"name":"John Smith","email":"jsmith@yahoo.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":0,"id":357437875835},{"id":75800032258,"name":"asdf","email":"asdfasdf","role":0}]}
1
+ {"users":[{"name":"Sean Maxwell","email":"sean.maxwell@gmail.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":1,"id":159123164363},{"name":"Gordan Freeman","email":"gordan.freeman@halflife.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":0,"id":906524522143},{"name":"John Smith","email":"jsmith@yahoo.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":0,"id":357437875835}]}
@@ -34,12 +34,12 @@ app.use(express.urlencoded({extended: true}));
34
34
  app.use(cookieParser(EnvVars.CookieProps.Secret));
35
35
 
36
36
  // Show routes called in console during development
37
- if (EnvVars.NodeEnv === NodeEnvs.Dev) {
37
+ if (EnvVars.NodeEnv === NodeEnvs.Dev.valueOf()) {
38
38
  app.use(morgan('dev'));
39
39
  }
40
40
 
41
41
  // Security
42
- if (EnvVars.NodeEnv === NodeEnvs.Production) {
42
+ if (EnvVars.NodeEnv === NodeEnvs.Production.valueOf()) {
43
43
  app.use(helmet());
44
44
  }
45
45
 
@@ -54,7 +54,7 @@ app.use((
54
54
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
55
55
  next: NextFunction,
56
56
  ) => {
57
- if (EnvVars.NodeEnv !== NodeEnvs.Test) {
57
+ if (EnvVars.NodeEnv !== NodeEnvs.Test.valueOf()) {
58
58
  logger.err(err, true);
59
59
  }
60
60
  let status = HttpStatusCodes.BAD_REQUEST;
@@ -34,12 +34,12 @@ app.use(express.urlencoded({extended: true}));
34
34
  app.use(cookieParser(EnvVars.CookieProps.Secret));
35
35
 
36
36
  // Show routes called in console during development
37
- if (EnvVars.NodeEnv === NodeEnvs.Dev) {
37
+ if (EnvVars.NodeEnv === NodeEnvs.Dev.valueOf()) {
38
38
  app.use(morgan('dev'));
39
39
  }
40
40
 
41
41
  // Security
42
- if (EnvVars.NodeEnv === NodeEnvs.Production) {
42
+ if (EnvVars.NodeEnv === NodeEnvs.Production.valueOf()) {
43
43
  app.use(helmet());
44
44
  }
45
45
 
@@ -54,7 +54,7 @@ app.use((
54
54
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
55
55
  next: NextFunction,
56
56
  ) => {
57
- if (EnvVars.NodeEnv !== NodeEnvs.Test) {
57
+ if (EnvVars.NodeEnv !== NodeEnvs.Test.valueOf()) {
58
58
  logger.err(err, true);
59
59
  }
60
60
  let status = HttpStatusCodes.BAD_REQUEST;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-generator-typescript",
3
- "version": "2.4.2",
3
+ "version": "2.4.4",
4
4
  "description": "Generate new Express applications similar to express-generate which but sets it up to use TypeScript instead",
5
5
  "scripts": {
6
6
  "express-generator-typescript": "node bin/cli.js",
@@ -1,56 +0,0 @@
1
- import UserRepo from '@src/repos/UserRepo';
2
-
3
- import PwdUtil from '@src/util/PwdUtil';
4
- import { tick } from '@src/util/misc';
5
-
6
- import HttpStatusCodes from '@src/constants/HttpStatusCodes';
7
- import { RouteError } from '@src/other/classes';
8
- import { IUser } from '@src/models/User';
9
-
10
-
11
- // **** Variables **** //
12
-
13
- // Errors
14
- export const Errors = {
15
- Unauth: 'Unauthorized',
16
- EmailNotFound(email: string) {
17
- return `User with email "${email}" not found`;
18
- },
19
- } as const;
20
-
21
-
22
- // **** Functions **** //
23
-
24
- /**
25
- * Login a user.
26
- */
27
- async function login(email: string, password: string): Promise<IUser> {
28
- // Fetch user
29
- const user = await UserRepo.getOne(email);
30
- if (!user) {
31
- throw new RouteError(
32
- HttpStatusCodes.UNAUTHORIZED,
33
- Errors.EmailNotFound(email),
34
- );
35
- }
36
- // Check password
37
- const hash = (user.pwdHash ?? ''),
38
- pwdPassed = await PwdUtil.compare(password, hash);
39
- if (!pwdPassed) {
40
- // If password failed, wait 500ms this will increase security
41
- await tick(500);
42
- throw new RouteError(
43
- HttpStatusCodes.UNAUTHORIZED,
44
- Errors.Unauth,
45
- );
46
- }
47
- // Return
48
- return user;
49
- }
50
-
51
-
52
- // **** Export default **** //
53
-
54
- export default {
55
- login,
56
- } as const;
@@ -1,39 +0,0 @@
1
- import bcrypt from 'bcrypt';
2
-
3
-
4
- // **** Variables **** //
5
-
6
- const SALT_ROUNDS = 12;
7
-
8
-
9
- // **** Functions **** //
10
-
11
- /**
12
- * Get a hash from the password.
13
- */
14
- function getHash(pwd: string): Promise<string> {
15
- return bcrypt.hash(pwd, SALT_ROUNDS);
16
- }
17
-
18
- /**
19
- * Useful for testing.
20
- */
21
- function hashSync(pwd: string): string {
22
- return bcrypt.hashSync(pwd, SALT_ROUNDS);
23
- }
24
-
25
- /**
26
- * See if a password passes the hash.
27
- */
28
- function compare(pwd: string, hash: string): Promise<boolean> {
29
- return bcrypt.compare(pwd, hash);
30
- }
31
-
32
-
33
- // **** Export Default **** //
34
-
35
- export default {
36
- getHash,
37
- hashSync,
38
- compare,
39
- } as const;
@@ -1,92 +0,0 @@
1
- import { Request, Response } from 'express';
2
-
3
- import HttpStatusCodes from '@src/constants/HttpStatusCodes';
4
- import { RouteError } from '@src/other/classes';
5
- import jsonwebtoken from 'jsonwebtoken';
6
-
7
- import EnvVars from '../constants/EnvVars';
8
-
9
-
10
- // **** Variables **** //
11
-
12
- // Errors
13
- const Errors = {
14
- ParamFalsey: 'Param is falsey',
15
- Validation: 'JSON-web-token validation failed.',
16
- } as const;
17
-
18
- // Options
19
- const Options = {
20
- expiresIn: EnvVars.Jwt.Exp,
21
- };
22
-
23
-
24
- // **** Functions **** //
25
-
26
- /**
27
- * Get session data from request object (i.e. ISessionUser)
28
- */
29
- function getSessionData<T>(req: Request): Promise<string | T | undefined> {
30
- const { Key } = EnvVars.CookieProps,
31
- jwt = req.signedCookies[Key];
32
- return _decode(jwt);
33
- }
34
-
35
- /**
36
- * Add a JWT to the response
37
- */
38
- async function addSessionData(
39
- res: Response,
40
- data: string | object,
41
- ): Promise<Response> {
42
- if (!res || !data) {
43
- throw new RouteError(HttpStatusCodes.BAD_REQUEST, Errors.ParamFalsey);
44
- }
45
- // Setup JWT
46
- const jwt = await _sign(data),
47
- { Key, Options } = EnvVars.CookieProps;
48
- // Return
49
- return res.cookie(Key, jwt, Options);
50
- }
51
-
52
- /**
53
- * Remove cookie
54
- */
55
- function clearCookie(res: Response): Response {
56
- const { Key, Options } = EnvVars.CookieProps;
57
- return res.clearCookie(Key, Options);
58
- }
59
-
60
-
61
- // **** Helper Functions **** //
62
-
63
- /**
64
- * Encrypt data and return jwt.
65
- */
66
- function _sign(data: string | object | Buffer): Promise<string> {
67
- return new Promise((res, rej) => {
68
- jsonwebtoken.sign(data, EnvVars.Jwt.Secret, Options, (err, token) => {
69
- return err ? rej(err) : res(token || '');
70
- });
71
- });
72
- }
73
-
74
- /**
75
- * Decrypt JWT and extract client data.
76
- */
77
- function _decode<T>(jwt: string): Promise<string | undefined | T> {
78
- return new Promise((res, rej) => {
79
- jsonwebtoken.verify(jwt, EnvVars.Jwt.Secret, (err, decoded) => {
80
- return err ? rej(Errors.Validation) : res(decoded as T);
81
- });
82
- });
83
- }
84
-
85
-
86
- // **** Export default **** //
87
-
88
- export default {
89
- addSessionData,
90
- getSessionData,
91
- clearCookie,
92
- } as const;