express-generator-typescript 2.1.10 → 2.1.12
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/lib/project-files/spec/support/login-agent.ts +1 -1
- package/lib/project-files/spec/tests/auth.spec.ts +3 -3
- package/lib/project-files/spec/tests/users.spec.ts +1 -1
- package/lib/project-files/spec/types/supertest/index.d.ts +1 -1
- package/lib/project-files/src/index.ts +3 -3
- package/lib/project-files/src/models/{user-model.ts → User.ts} +0 -0
- package/lib/project-files/src/repos/mock-orm.ts +1 -1
- package/lib/project-files/src/repos/user-repo.ts +3 -3
- package/lib/project-files/src/routes/api.ts +1 -1
- package/lib/project-files/src/routes/auth-routes.ts +3 -3
- package/lib/project-files/src/routes/middlware/adminMw.ts +3 -3
- package/lib/project-files/src/routes/user-routes.ts +1 -1
- package/lib/project-files/src/server.ts +5 -5
- package/lib/project-files/src/services/user-service.ts +1 -1
- package/lib/project-files/src/shared/{env-vars.ts → EnvVars.ts} +0 -0
- package/lib/project-files/src/shared/functions.ts +1 -8
- package/lib/project-files/src/util/jwt-util.ts +4 -4
- package/package.json +1 -1
|
@@ -5,8 +5,8 @@ import logger from 'jet-logger';
|
|
|
5
5
|
|
|
6
6
|
import app from '@server';
|
|
7
7
|
import userRepo from '@repos/user-repo';
|
|
8
|
-
import
|
|
9
|
-
import User, { UserRoles } from '@models/
|
|
8
|
+
import EnvVars from '@shared/EnvVars';
|
|
9
|
+
import User, { UserRoles } from '@models/User';
|
|
10
10
|
import authRoutes from '@routes/auth-routes';
|
|
11
11
|
import { pwdSaltRounds } from 'spec/support/login-agent';
|
|
12
12
|
import { UnauthorizedError } from '@shared/errors';
|
|
@@ -78,7 +78,7 @@ describe('auth-router', () => {
|
|
|
78
78
|
!!err && logger.err(err);
|
|
79
79
|
expect(res.status).toBe(OK);
|
|
80
80
|
const cookie = res.headers['set-cookie'][0];
|
|
81
|
-
expect(cookie).toContain(
|
|
81
|
+
expect(cookie).toContain(EnvVars.cookieProps.key);
|
|
82
82
|
done();
|
|
83
83
|
});
|
|
84
84
|
});
|
|
@@ -5,7 +5,7 @@ import logger from 'jet-logger';
|
|
|
5
5
|
|
|
6
6
|
import app from '@server';
|
|
7
7
|
import userRepo from '@repos/user-repo';
|
|
8
|
-
import User, { IUser } from '@models/
|
|
8
|
+
import User, { IUser } from '@models/User';
|
|
9
9
|
import userRoutes from '@routes/user-routes';
|
|
10
10
|
|
|
11
11
|
import {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import './pre-start'; // Must be the first import
|
|
2
2
|
import logger from 'jet-logger';
|
|
3
3
|
|
|
4
|
-
import
|
|
4
|
+
import EnvVars from '@shared/EnvVars';
|
|
5
5
|
import server from './server';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
// **** Start server **** //
|
|
9
9
|
|
|
10
|
-
const msg = ('Express server started on port: ' +
|
|
11
|
-
server.listen(
|
|
10
|
+
const msg = ('Express server started on port: ' + EnvVars.port.toString());
|
|
11
|
+
server.listen(EnvVars.port, () => logger.info(msg));
|
|
File without changes
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IUser } from '@models/
|
|
2
|
-
import
|
|
1
|
+
import { IUser } from '@models/User';
|
|
2
|
+
import { getRandomInt } from '@shared/functions';
|
|
3
3
|
import orm from './mock-orm';
|
|
4
4
|
|
|
5
5
|
|
|
@@ -44,7 +44,7 @@ async function getAll(): Promise<IUser[]> {
|
|
|
44
44
|
*/
|
|
45
45
|
async function add(user: IUser): Promise<void> {
|
|
46
46
|
const db = await orm.openDb();
|
|
47
|
-
user.id =
|
|
47
|
+
user.id = getRandomInt();
|
|
48
48
|
db.users.push(user);
|
|
49
49
|
return orm.saveDb(db);
|
|
50
50
|
}
|
|
@@ -2,7 +2,7 @@ import { Router } from 'express';
|
|
|
2
2
|
|
|
3
3
|
import adminMw from './middlware/adminMw';
|
|
4
4
|
import validate from './middlware/validate';
|
|
5
|
-
import User from '@models/
|
|
5
|
+
import User from '@models/User';
|
|
6
6
|
import authRoutes from './auth-routes';
|
|
7
7
|
import userRoutes from './user-routes';
|
|
8
8
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import StatusCodes from 'http-status-codes';
|
|
2
2
|
|
|
3
3
|
import authService from '@services/auth-service';
|
|
4
|
-
import
|
|
4
|
+
import EnvVars from '@shared/EnvVars';
|
|
5
5
|
import { IReq, IRes } from '@shared/types';
|
|
6
6
|
|
|
7
7
|
|
|
@@ -35,7 +35,7 @@ async function login(req: IReq<ILoginReq>, res: IRes) {
|
|
|
35
35
|
const { email, password } = req.body;
|
|
36
36
|
// Add jwt to cookie
|
|
37
37
|
const jwt = await authService.getJwt(email, password);
|
|
38
|
-
const { key, options } =
|
|
38
|
+
const { key, options } = EnvVars.cookieProps;
|
|
39
39
|
res.cookie(key, jwt, options);
|
|
40
40
|
// Return
|
|
41
41
|
return res.status(OK).end();
|
|
@@ -45,7 +45,7 @@ async function login(req: IReq<ILoginReq>, res: IRes) {
|
|
|
45
45
|
* Logout the user.
|
|
46
46
|
*/
|
|
47
47
|
function logout(_: IReq, res: IRes) {
|
|
48
|
-
const { key, options } =
|
|
48
|
+
const { key, options } = EnvVars.cookieProps;
|
|
49
49
|
res.clearCookie(key, options);
|
|
50
50
|
return res.status(OK).end();
|
|
51
51
|
}
|
|
@@ -6,9 +6,9 @@ import StatusCodes from 'http-status-codes';
|
|
|
6
6
|
import { Request, Response, NextFunction } from 'express';
|
|
7
7
|
import { JwtPayload } from 'jsonwebtoken';
|
|
8
8
|
|
|
9
|
-
import
|
|
9
|
+
import EnvVars from '@shared/EnvVars';
|
|
10
10
|
import jwtUtil from '@util/jwt-util';
|
|
11
|
-
import { IUser, UserRoles } from '@models/
|
|
11
|
+
import { IUser, UserRoles } from '@models/User';
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
// **** Variables **** //
|
|
@@ -39,7 +39,7 @@ async function adminMw(
|
|
|
39
39
|
) {
|
|
40
40
|
try {
|
|
41
41
|
// Extract the token
|
|
42
|
-
const cookieName =
|
|
42
|
+
const cookieName = EnvVars.cookieProps.key,
|
|
43
43
|
jwt = req.signedCookies[cookieName];
|
|
44
44
|
if (!jwt) {
|
|
45
45
|
throw Error(jwtNotPresentErr);
|
|
@@ -9,7 +9,7 @@ import 'express-async-errors';
|
|
|
9
9
|
|
|
10
10
|
import BaseRouter from './routes/api';
|
|
11
11
|
import logger from 'jet-logger';
|
|
12
|
-
import
|
|
12
|
+
import EnvVars from '@shared/EnvVars';
|
|
13
13
|
import { CustomError } from '@shared/errors';
|
|
14
14
|
|
|
15
15
|
import { NodeEnvs } from '@shared/enums';
|
|
@@ -24,15 +24,15 @@ const app = express();
|
|
|
24
24
|
|
|
25
25
|
app.use(express.json());
|
|
26
26
|
app.use(express.urlencoded({extended: true}));
|
|
27
|
-
app.use(cookieParser(
|
|
27
|
+
app.use(cookieParser(EnvVars.cookieProps.secret));
|
|
28
28
|
|
|
29
29
|
// Show routes called in console during development
|
|
30
|
-
if (
|
|
30
|
+
if (EnvVars.nodeEnv === NodeEnvs.Dev) {
|
|
31
31
|
app.use(morgan('dev'));
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
// Security
|
|
35
|
-
if (
|
|
35
|
+
if (EnvVars.nodeEnv === NodeEnvs.Production) {
|
|
36
36
|
app.use(helmet());
|
|
37
37
|
}
|
|
38
38
|
|
|
@@ -81,7 +81,7 @@ app.get('/', (_: Request, res: Response) => {
|
|
|
81
81
|
|
|
82
82
|
// Redirect to login if not logged in.
|
|
83
83
|
app.get('/users', (req: Request, res: Response) => {
|
|
84
|
-
const jwt = req.signedCookies[
|
|
84
|
+
const jwt = req.signedCookies[EnvVars.cookieProps.key];
|
|
85
85
|
if (!jwt) {
|
|
86
86
|
res.redirect('/');
|
|
87
87
|
} else {
|
|
File without changes
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get a random number between 1 and 1,000,000,000,000
|
|
3
3
|
*/
|
|
4
|
-
function getRandomInt(): number {
|
|
4
|
+
export function getRandomInt(): number {
|
|
5
5
|
return Math.floor(Math.random() * 1_000_000_000_000);
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// **** Export default **** //
|
|
10
|
-
|
|
11
|
-
export default {
|
|
12
|
-
getRandomInt,
|
|
13
|
-
} as const;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import jsonwebtoken from 'jsonwebtoken';
|
|
2
|
-
import
|
|
2
|
+
import EnvVars from '../shared/EnvVars';
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
// **** Variables **** //
|
|
@@ -11,7 +11,7 @@ const errors = {
|
|
|
11
11
|
|
|
12
12
|
// Options
|
|
13
13
|
const options = {
|
|
14
|
-
expiresIn:
|
|
14
|
+
expiresIn: EnvVars.jwt.exp,
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
|
|
@@ -22,7 +22,7 @@ const options = {
|
|
|
22
22
|
*/
|
|
23
23
|
function sign(data: string | object | Buffer): Promise<string> {
|
|
24
24
|
return new Promise((res, rej) => {
|
|
25
|
-
jsonwebtoken.sign(data,
|
|
25
|
+
jsonwebtoken.sign(data, EnvVars.jwt.secret, options, (err, token) => {
|
|
26
26
|
return err ? rej(err) : res(token || '');
|
|
27
27
|
});
|
|
28
28
|
});
|
|
@@ -33,7 +33,7 @@ function sign(data: string | object | Buffer): Promise<string> {
|
|
|
33
33
|
*/
|
|
34
34
|
function decode<T>(jwt: string): Promise<string | undefined | T> {
|
|
35
35
|
return new Promise((res, rej) => {
|
|
36
|
-
jsonwebtoken.verify(jwt,
|
|
36
|
+
jsonwebtoken.verify(jwt, EnvVars.jwt.secret, (err, decoded) => {
|
|
37
37
|
return err ? rej(errors.validation) : res(decoded as T);
|
|
38
38
|
});
|
|
39
39
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-generator-typescript",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.12",
|
|
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",
|