express-generator-typescript 1.10.3 → 2.0.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 +37 -25
- package/bin/cli.js +2 -15
- package/lib/cli.js +1 -15
- package/lib/express-generator-typescript.js +11 -21
- package/lib/project-files/.eslintrc.json +15 -2
- package/lib/project-files/build.ts +2 -2
- package/lib/project-files/package.json +15 -5
- package/lib/project-files/spec/nodemon.json +1 -1
- package/lib/{auth-proj → project-files}/spec/support/login-agent.ts +0 -0
- package/lib/{auth-proj → project-files}/spec/tests/auth.spec.ts +18 -32
- package/lib/project-files/spec/tests/users.spec.ts +31 -22
- package/lib/project-files/spec/types/supertest/index.d.ts +14 -0
- package/lib/project-files/src/models/user-model.ts +18 -3
- package/lib/project-files/src/pre-start/env/development.env +9 -0
- package/lib/project-files/src/pre-start/env/production.env +10 -0
- package/lib/project-files/src/pre-start/env/test.env +9 -0
- package/lib/{auth-proj → project-files}/src/public/scripts/http.js +0 -0
- package/lib/{auth-proj → project-files}/src/public/scripts/login.js +0 -0
- package/lib/{auth-proj → project-files}/src/public/scripts/users.js +0 -0
- package/lib/{auth-proj → project-files}/src/public/stylesheets/login.css +0 -0
- package/lib/{auth-proj → project-files}/src/public/stylesheets/users.css +0 -0
- package/lib/project-files/src/repos/user-repo.ts +7 -7
- package/lib/project-files/src/routes/api.ts +9 -6
- package/lib/{auth-proj → project-files}/src/routes/auth-router.ts +3 -5
- package/lib/{auth-proj → project-files}/src/routes/middleware.ts +10 -8
- package/lib/project-files/src/routes/user-router.ts +9 -15
- package/lib/project-files/src/server.ts +82 -73
- package/lib/{auth-proj → project-files}/src/services/auth-service.ts +0 -0
- package/lib/project-files/src/services/user-service.ts +9 -9
- package/lib/project-files/src/shared/env-vars.ts +18 -0
- package/lib/project-files/src/shared/errors.ts +11 -0
- package/lib/project-files/src/types/express/index.d.ts +22 -0
- package/lib/{socket-io → project-files}/src/util/jwt-util.ts +7 -6
- package/lib/{auth-proj → project-files}/src/views/login.html +0 -0
- package/lib/{auth-proj → project-files}/src/views/users.html +0 -0
- package/lib/project-files/tsconfig.json +3 -0
- package/package.json +1 -1
- package/chat-screenshot.png +0 -0
- package/lib/auth-proj/.eslintignore +0 -1
- package/lib/auth-proj/.eslintrc.json +0 -27
- package/lib/auth-proj/build.ts +0 -64
- package/lib/auth-proj/gitignore +0 -3
- package/lib/auth-proj/package.json +0 -73
- package/lib/auth-proj/spec/index.ts +0 -57
- package/lib/auth-proj/spec/loadEnv.ts +0 -10
- package/lib/auth-proj/spec/nodemon.json +0 -6
- package/lib/auth-proj/spec/support/jasmine.json +0 -11
- package/lib/auth-proj/spec/tests/users.spec.ts +0 -283
- package/lib/auth-proj/src/index.ts +0 -14
- package/lib/auth-proj/src/models/user-model.ts +0 -56
- package/lib/auth-proj/src/pre-start/env/development.env +0 -23
- package/lib/auth-proj/src/pre-start/env/production.env +0 -24
- package/lib/auth-proj/src/pre-start/env/test.env +0 -23
- package/lib/auth-proj/src/pre-start/index.ts +0 -28
- package/lib/auth-proj/src/repos/database.json +0 -1
- package/lib/auth-proj/src/repos/mock-orm.ts +0 -41
- package/lib/auth-proj/src/repos/user-repo.ts +0 -88
- package/lib/auth-proj/src/routes/api.ts +0 -17
- package/lib/auth-proj/src/routes/user-router.ts +0 -80
- package/lib/auth-proj/src/server.ts +0 -82
- package/lib/auth-proj/src/services/user-service.ts +0 -52
- package/lib/auth-proj/src/shared/env-vars.ts +0 -20
- package/lib/auth-proj/src/shared/errors.ts +0 -45
- package/lib/auth-proj/src/shared/functions.ts +0 -18
- package/lib/auth-proj/src/types/express/index.d.ts +0 -32
- package/lib/auth-proj/src/util/jwt-util.ts +0 -47
- package/lib/auth-proj/tsconfig.json +0 -100
- package/lib/auth-proj/tsconfig.prod.json +0 -11
- package/lib/project-files/src/public/scripts/index.js +0 -183
- package/lib/project-files/src/public/stylesheets/style.css +0 -33
- package/lib/project-files/src/views/index.html +0 -31
- package/lib/socket-io/.eslintignore +0 -1
- package/lib/socket-io/.eslintrc.json +0 -27
- package/lib/socket-io/build.ts +0 -64
- package/lib/socket-io/gitignore +0 -3
- package/lib/socket-io/package.json +0 -75
- package/lib/socket-io/spec/index.ts +0 -57
- package/lib/socket-io/spec/loadEnv.ts +0 -10
- package/lib/socket-io/spec/nodemon.json +0 -6
- package/lib/socket-io/spec/support/jasmine.json +0 -11
- package/lib/socket-io/spec/support/login-agent.ts +0 -49
- package/lib/socket-io/spec/tests/auth.spec.ts +0 -159
- package/lib/socket-io/spec/tests/chat.spec.ts +0 -150
- package/lib/socket-io/spec/tests/users.spec.ts +0 -283
- package/lib/socket-io/src/index.ts +0 -14
- package/lib/socket-io/src/models/user-model.ts +0 -56
- package/lib/socket-io/src/pre-start/env/development.env +0 -23
- package/lib/socket-io/src/pre-start/env/production.env +0 -24
- package/lib/socket-io/src/pre-start/env/test.env +0 -23
- package/lib/socket-io/src/pre-start/index.ts +0 -28
- package/lib/socket-io/src/public/scripts/chat.js +0 -87
- package/lib/socket-io/src/public/scripts/http.js +0 -24
- package/lib/socket-io/src/public/scripts/index.js +0 -16
- package/lib/socket-io/src/public/scripts/login.js +0 -17
- package/lib/socket-io/src/public/scripts/users.js +0 -148
- package/lib/socket-io/src/public/stylesheets/chat.css +0 -43
- package/lib/socket-io/src/public/stylesheets/index.css +0 -13
- package/lib/socket-io/src/public/stylesheets/login.css +0 -20
- package/lib/socket-io/src/public/stylesheets/users.css +0 -40
- package/lib/socket-io/src/repos/database.json +0 -38
- package/lib/socket-io/src/repos/mock-orm.ts +0 -41
- package/lib/socket-io/src/repos/user-repo.ts +0 -88
- package/lib/socket-io/src/routes/api.ts +0 -19
- package/lib/socket-io/src/routes/auth-router.ts +0 -63
- package/lib/socket-io/src/routes/chat-router.ts +0 -97
- package/lib/socket-io/src/routes/middleware.ts +0 -55
- package/lib/socket-io/src/routes/user-router.ts +0 -77
- package/lib/socket-io/src/server.ts +0 -100
- package/lib/socket-io/src/services/auth-service.ts +0 -38
- package/lib/socket-io/src/services/user-service.ts +0 -52
- package/lib/socket-io/src/shared/env-vars.ts +0 -20
- package/lib/socket-io/src/shared/errors.ts +0 -56
- package/lib/socket-io/src/shared/functions.ts +0 -18
- package/lib/socket-io/src/types/express/index.d.ts +0 -34
- package/lib/socket-io/src/views/chat.html +0 -51
- package/lib/socket-io/src/views/login.html +0 -35
- package/lib/socket-io/src/views/users.html +0 -47
- package/lib/socket-io/tsconfig.json +0 -100
- package/lib/socket-io/tsconfig.prod.json +0 -11
|
@@ -12,3 +12,12 @@ JET_LOGGER_MODE=CONSOLE
|
|
|
12
12
|
JET_LOGGER_FILEPATH=jet-logger.log
|
|
13
13
|
JET_LOGGER_TIMESTAMP=TRUE
|
|
14
14
|
JET_LOGGER_FORMAT=LINE
|
|
15
|
+
|
|
16
|
+
## Authentication ##
|
|
17
|
+
COOKIE_DOMAIN=localhost
|
|
18
|
+
COOKIE_PATH=/
|
|
19
|
+
SECURE_COOKIE=false
|
|
20
|
+
JWT_SECRET=xxxxxxxxxxxxxx
|
|
21
|
+
COOKIE_SECRET=xxxxxxxxxxxxxx
|
|
22
|
+
# expires in 3 days
|
|
23
|
+
COOKIE_EXP=259200000
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -6,7 +6,7 @@ import orm from './mock-orm';
|
|
|
6
6
|
// **** Functions **** //
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Get one user
|
|
9
|
+
* Get one user.
|
|
10
10
|
*/
|
|
11
11
|
async function getOne(email: string): Promise<IUser | null> {
|
|
12
12
|
const db = await orm.openDb();
|
|
@@ -79,10 +79,10 @@ async function _delete(id: number): Promise<void> {
|
|
|
79
79
|
// **** Export default **** //
|
|
80
80
|
|
|
81
81
|
export default {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
getOne,
|
|
83
|
+
persists,
|
|
84
|
+
getAll,
|
|
85
|
+
add,
|
|
86
|
+
update,
|
|
87
|
+
delete: _delete,
|
|
88
88
|
} as const;
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
|
+
import { adminMw } from './middleware';
|
|
3
|
+
import authRouter from './auth-router';
|
|
2
4
|
import userRouter from './user-router';
|
|
3
5
|
|
|
4
6
|
|
|
5
|
-
//
|
|
6
|
-
const
|
|
7
|
+
// Init
|
|
8
|
+
const apiRouter = Router();
|
|
7
9
|
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
+
// Add api routes
|
|
11
|
+
apiRouter.use('/auth', authRouter);
|
|
12
|
+
apiRouter.use('/users', adminMw, userRouter);
|
|
10
13
|
|
|
11
14
|
|
|
12
|
-
//
|
|
15
|
+
// **** Export default **** //
|
|
13
16
|
|
|
14
|
-
export default
|
|
17
|
+
export default apiRouter;
|
|
@@ -9,7 +9,7 @@ import { IReq } from 'src/types/express';
|
|
|
9
9
|
|
|
10
10
|
// **** Types **** //
|
|
11
11
|
|
|
12
|
-
interface
|
|
12
|
+
interface ILoginReq {
|
|
13
13
|
email: string;
|
|
14
14
|
password: string;
|
|
15
15
|
}
|
|
@@ -33,15 +33,13 @@ export const p = {
|
|
|
33
33
|
/**
|
|
34
34
|
* Login a user.
|
|
35
35
|
*/
|
|
36
|
-
router.post(p.login, async (req: IReq<
|
|
37
|
-
// Check email and password present
|
|
36
|
+
router.post(p.login, async (req: IReq<ILoginReq>, res: Response) => {
|
|
38
37
|
const { email, password } = req.body;
|
|
39
38
|
if (!email || !password) {
|
|
40
39
|
throw new ParamMissingError();
|
|
41
40
|
}
|
|
42
|
-
// Get jwt
|
|
43
|
-
const jwt = await authService.login(email, password);
|
|
44
41
|
// Add jwt to cookie
|
|
42
|
+
const jwt = await authService.login(email, password);
|
|
45
43
|
const { key, options } = envVars.cookieProps;
|
|
46
44
|
res.cookie(key, jwt, options);
|
|
47
45
|
// Return
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import StatusCodes from 'http-status-codes';
|
|
2
2
|
import { Request, Response, NextFunction } from 'express';
|
|
3
|
-
import { JwtPayload } from 'jsonwebtoken';
|
|
4
3
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
4
|
+
import { IUser, UserRoles } from '@models/user-model';
|
|
5
|
+
import envVars from 'src/shared/env-vars';
|
|
7
6
|
import jwtUtil from '@util/jwt-util';
|
|
7
|
+
import { JwtPayload } from 'jsonwebtoken';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
// **** Variables **** //
|
|
@@ -28,22 +28,23 @@ export interface ISessionUser extends JwtPayload {
|
|
|
28
28
|
/**
|
|
29
29
|
* Middleware to verify if user logged in.
|
|
30
30
|
*/
|
|
31
|
-
export async function
|
|
31
|
+
export async function adminMw(req: Request, res: Response, next: NextFunction) {
|
|
32
32
|
try {
|
|
33
|
-
//
|
|
34
|
-
const
|
|
33
|
+
// Extract the token
|
|
34
|
+
const cookieName = envVars.cookieProps.key,
|
|
35
|
+
jwt = req.signedCookies[cookieName];
|
|
35
36
|
if (!jwt) {
|
|
36
37
|
throw Error(jwtNotPresentErr);
|
|
37
38
|
}
|
|
38
39
|
// Make sure user role is an admin
|
|
39
40
|
const clientData = await jwtUtil.decode<ISessionUser>(jwt);
|
|
40
|
-
if (
|
|
41
|
+
if (clientData.role === UserRoles.Admin) {
|
|
41
42
|
res.locals.sessionUser = clientData;
|
|
42
43
|
next();
|
|
43
44
|
} else {
|
|
44
45
|
throw Error(jwtNotPresentErr);
|
|
45
46
|
}
|
|
46
|
-
} catch (err) {
|
|
47
|
+
} catch (err: unknown) {
|
|
47
48
|
let error;
|
|
48
49
|
if (typeof err === 'string') {
|
|
49
50
|
error = err;
|
|
@@ -53,3 +54,4 @@ export async function authMw(req: Request, res: Response, next: NextFunction) {
|
|
|
53
54
|
return res.status(UNAUTHORIZED).json({ error });
|
|
54
55
|
}
|
|
55
56
|
}
|
|
57
|
+
|
|
@@ -3,15 +3,15 @@ import { Request, Response, Router } from 'express';
|
|
|
3
3
|
|
|
4
4
|
import userService from '@services/user-service';
|
|
5
5
|
import { ParamMissingError } from '@shared/errors';
|
|
6
|
-
import { IReq } from 'src/types/express';
|
|
7
6
|
import { IUser } from '@models/user-model';
|
|
7
|
+
import { IReq } from 'src/types/express';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
// **** Variables **** //
|
|
11
11
|
|
|
12
12
|
// Misc
|
|
13
|
-
const router = Router()
|
|
14
|
-
|
|
13
|
+
const router = Router(),
|
|
14
|
+
{ CREATED, OK } = StatusCodes;
|
|
15
15
|
|
|
16
16
|
// Paths
|
|
17
17
|
export const p = {
|
|
@@ -25,51 +25,45 @@ export const p = {
|
|
|
25
25
|
// **** Routes **** //
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* Get all users
|
|
28
|
+
* Get all users.
|
|
29
29
|
*/
|
|
30
30
|
router.get(p.get, async (_: Request, res: Response) => {
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const users = await userService.getAll();
|
|
32
|
+
return res.status(OK).json({ users });
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
* Add one user
|
|
36
|
+
* Add one user.
|
|
37
37
|
*/
|
|
38
38
|
router.post(p.add, async (req: IReq<{user: IUser}>, res: Response) => {
|
|
39
39
|
const { user } = req.body;
|
|
40
|
-
// Check param
|
|
41
40
|
if (!user) {
|
|
42
41
|
throw new ParamMissingError();
|
|
43
42
|
}
|
|
44
|
-
// Fetch data
|
|
45
43
|
await userService.addOne(user);
|
|
46
44
|
return res.status(CREATED).end();
|
|
47
45
|
});
|
|
48
46
|
|
|
49
47
|
/**
|
|
50
|
-
* Update one user
|
|
48
|
+
* Update one user.
|
|
51
49
|
*/
|
|
52
50
|
router.put(p.update, async (req: IReq<{user: IUser}>, res: Response) => {
|
|
53
51
|
const { user } = req.body;
|
|
54
|
-
// Check param
|
|
55
52
|
if (!user) {
|
|
56
53
|
throw new ParamMissingError();
|
|
57
54
|
}
|
|
58
|
-
// Fetch data
|
|
59
55
|
await userService.updateOne(user);
|
|
60
56
|
return res.status(OK).end();
|
|
61
57
|
});
|
|
62
58
|
|
|
63
59
|
/**
|
|
64
|
-
* Delete one user
|
|
60
|
+
* Delete one user.
|
|
65
61
|
*/
|
|
66
62
|
router.delete(p.delete, async (req: Request, res: Response) => {
|
|
67
63
|
const { id } = req.params;
|
|
68
|
-
// Check param
|
|
69
64
|
if (!id) {
|
|
70
65
|
throw new ParamMissingError();
|
|
71
66
|
}
|
|
72
|
-
// Fetch data
|
|
73
67
|
await userService.delete(Number(id));
|
|
74
68
|
return res.status(OK).end();
|
|
75
69
|
});
|
|
@@ -1,73 +1,82 @@
|
|
|
1
|
-
import cookieParser from 'cookie-parser';
|
|
2
|
-
import morgan from 'morgan';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import helmet from 'helmet';
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
// ****
|
|
17
|
-
|
|
18
|
-
const app = express();
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// **** Set basic express settings
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
app.use(express.
|
|
25
|
-
app.use(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
1
|
+
import cookieParser from 'cookie-parser';
|
|
2
|
+
import morgan from 'morgan';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import helmet from 'helmet';
|
|
5
|
+
import StatusCodes from 'http-status-codes';
|
|
6
|
+
import express, { Request, Response, NextFunction } from 'express';
|
|
7
|
+
|
|
8
|
+
import 'express-async-errors';
|
|
9
|
+
|
|
10
|
+
import BaseRouter from './routes/api';
|
|
11
|
+
import logger from 'jet-logger';
|
|
12
|
+
import { CustomError } from '@shared/errors';
|
|
13
|
+
import envVars from '@shared/env-vars';
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// **** Init express **** //
|
|
17
|
+
|
|
18
|
+
const app = express();
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
// **** Set basic express settings **** //
|
|
22
|
+
|
|
23
|
+
app.use(express.json());
|
|
24
|
+
app.use(express.urlencoded({extended: true}));
|
|
25
|
+
app.use(cookieParser(envVars.cookieProps.secret));
|
|
26
|
+
|
|
27
|
+
// Show routes called in console during development
|
|
28
|
+
if (envVars.nodeEnv === 'development') {
|
|
29
|
+
app.use(morgan('dev'));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Security
|
|
33
|
+
if (envVars.nodeEnv === 'production') {
|
|
34
|
+
app.use(helmet());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// **** Add API routes **** //
|
|
39
|
+
|
|
40
|
+
// Add APIs
|
|
41
|
+
app.use('/api', BaseRouter);
|
|
42
|
+
|
|
43
|
+
// Error handling
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
45
|
+
app.use((err: Error | CustomError, req: Request, res: Response, _: NextFunction) => {
|
|
46
|
+
logger.err(err, true);
|
|
47
|
+
const status = (err instanceof CustomError ? err.HttpStatus : StatusCodes.BAD_REQUEST);
|
|
48
|
+
return res.status(status).json({
|
|
49
|
+
error: err.message,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// **** Serve front-end content **** //
|
|
55
|
+
|
|
56
|
+
// Set views directory (html)
|
|
57
|
+
const viewsDir = path.join(__dirname, 'views');
|
|
58
|
+
app.set('views', viewsDir);
|
|
59
|
+
|
|
60
|
+
// Set static directory (js and css).
|
|
61
|
+
const staticDir = path.join(__dirname, 'public');
|
|
62
|
+
app.use(express.static(staticDir));
|
|
63
|
+
|
|
64
|
+
// Nav to login pg by default
|
|
65
|
+
app.get('/', (_: Request, res: Response) => {
|
|
66
|
+
res.sendFile('login.html', {root: viewsDir});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Redirect to login if not logged in.
|
|
70
|
+
app.get('/users', (req: Request, res: Response) => {
|
|
71
|
+
const jwt = req.signedCookies[envVars.cookieProps.key];
|
|
72
|
+
if (!jwt) {
|
|
73
|
+
res.redirect('/');
|
|
74
|
+
} else {
|
|
75
|
+
res.sendFile('users.html', {root: viewsDir});
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// **** Export default **** //
|
|
81
|
+
|
|
82
|
+
export default app;
|
|
File without changes
|
|
@@ -6,24 +6,24 @@ import { UserNotFoundError } from '@shared/errors';
|
|
|
6
6
|
// **** Functions **** //
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Get all users
|
|
9
|
+
* Get all users.
|
|
10
10
|
*/
|
|
11
11
|
function getAll(): Promise<IUser[]> {
|
|
12
12
|
return userRepo.getAll();
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* Add one user
|
|
16
|
+
* Add one user.
|
|
17
17
|
*/
|
|
18
18
|
function addOne(user: IUser): Promise<void> {
|
|
19
19
|
return userRepo.add(user);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Update one user
|
|
23
|
+
* Update one user.
|
|
24
24
|
*/
|
|
25
25
|
async function updateOne(user: IUser): Promise<void> {
|
|
26
|
-
const persists = await userRepo.persists(user.id
|
|
26
|
+
const persists = await userRepo.persists(user.id);
|
|
27
27
|
if (!persists) {
|
|
28
28
|
throw new UserNotFoundError();
|
|
29
29
|
}
|
|
@@ -31,7 +31,7 @@ async function updateOne(user: IUser): Promise<void> {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* Delete a user by their id
|
|
34
|
+
* Delete a user by their id.
|
|
35
35
|
*/
|
|
36
36
|
async function _delete(id: number): Promise<void> {
|
|
37
37
|
const persists = await userRepo.persists(id);
|
|
@@ -45,8 +45,8 @@ async function _delete(id: number): Promise<void> {
|
|
|
45
45
|
// **** Export default **** //
|
|
46
46
|
|
|
47
47
|
export default {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
getAll,
|
|
49
|
+
addOne,
|
|
50
|
+
updateOne,
|
|
51
|
+
delete: _delete,
|
|
52
52
|
} as const;
|
|
@@ -1,4 +1,22 @@
|
|
|
1
|
+
/* eslint-disable node/no-process-env */
|
|
2
|
+
|
|
1
3
|
export default {
|
|
2
4
|
nodeEnv: (process.env.NODE_ENV ?? ''),
|
|
3
5
|
port: (process.env.PORT ?? 0),
|
|
6
|
+
cookieProps: {
|
|
7
|
+
key: 'ExpressGeneratorTs',
|
|
8
|
+
secret: (process.env.COOKIE_SECRET ?? ''),
|
|
9
|
+
options: {
|
|
10
|
+
httpOnly: true,
|
|
11
|
+
signed: true,
|
|
12
|
+
path: (process.env.COOKIE_PATH ?? ''),
|
|
13
|
+
maxAge: Number(process.env.COOKIE_EXP ?? 0),
|
|
14
|
+
domain: (process.env.COOKIE_DOMAIN ?? ''),
|
|
15
|
+
secure: (process.env.SECURE_COOKIE === 'true'),
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
jwt: {
|
|
19
|
+
secret: (process.env.JWT_SECRET ?? ''),
|
|
20
|
+
exp: (process.env.COOKIE_EXP ?? ''), // exp at the same time as the cookie
|
|
21
|
+
},
|
|
4
22
|
} as const;
|
|
@@ -32,3 +32,14 @@ export class UserNotFoundError extends CustomError {
|
|
|
32
32
|
super(UserNotFoundError.Msg, UserNotFoundError.HttpStatus);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
export class UnauthorizedError extends CustomError {
|
|
38
|
+
|
|
39
|
+
public static readonly Msg = 'Login failed';
|
|
40
|
+
public static readonly HttpStatus = HttpStatusCodes.UNAUTHORIZED;
|
|
41
|
+
|
|
42
|
+
constructor() {
|
|
43
|
+
super(UnauthorizedError.Msg, UnauthorizedError.HttpStatus);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -1,6 +1,28 @@
|
|
|
1
|
+
import 'express';
|
|
1
2
|
import * as e from 'express';
|
|
2
3
|
import { Query } from 'express-serve-static-core';
|
|
3
4
|
|
|
5
|
+
import { ISessionUser } from '@routes/middleware';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// **** Declaration Merging **** //
|
|
10
|
+
|
|
11
|
+
declare module 'express' {
|
|
12
|
+
|
|
13
|
+
export interface Request {
|
|
14
|
+
signedCookies: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Response {
|
|
18
|
+
locals: {
|
|
19
|
+
sessionUser: ISessionUser;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
// **** Generics for Request Object **** //
|
|
4
26
|
|
|
5
27
|
export interface IReq<T> extends e.Request {
|
|
6
28
|
body: T;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import randomString from 'randomstring';
|
|
2
1
|
import jsonwebtoken, { JwtPayload } from 'jsonwebtoken';
|
|
2
|
+
import envVars from '../shared/env-vars';
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
// **** Variables **** //
|
|
@@ -9,9 +9,10 @@ const errors = {
|
|
|
9
9
|
validation: 'JSON-web-token validation failed.',
|
|
10
10
|
} as const;
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
const
|
|
14
|
-
|
|
12
|
+
// Options
|
|
13
|
+
const options = {
|
|
14
|
+
expiresIn: envVars.jwt.exp,
|
|
15
|
+
};
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
// **** Functions **** //
|
|
@@ -21,7 +22,7 @@ const options = {expiresIn: process.env.COOKIE_EXP};
|
|
|
21
22
|
*/
|
|
22
23
|
function sign(data: JwtPayload): Promise<string> {
|
|
23
24
|
return new Promise((resolve, reject) => {
|
|
24
|
-
jsonwebtoken.sign(data, secret, options, (err, token) => {
|
|
25
|
+
jsonwebtoken.sign(data, envVars.jwt.secret, options, (err, token) => {
|
|
25
26
|
return err ? reject(err) : resolve(token || '');
|
|
26
27
|
});
|
|
27
28
|
});
|
|
@@ -32,7 +33,7 @@ function sign(data: JwtPayload): Promise<string> {
|
|
|
32
33
|
*/
|
|
33
34
|
function decode<T>(jwt: string): Promise<T> {
|
|
34
35
|
return new Promise((res, rej) => {
|
|
35
|
-
jsonwebtoken.verify(jwt, secret, (err, decoded) => {
|
|
36
|
+
jsonwebtoken.verify(jwt, envVars.jwt.secret, (err, decoded) => {
|
|
36
37
|
return err ? rej(errors.validation) : res(decoded as T);
|
|
37
38
|
});
|
|
38
39
|
});
|
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-generator-typescript",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
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",
|
package/chat-screenshot.png
DELETED
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
src/public/
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"parser": "@typescript-eslint/parser",
|
|
3
|
-
"plugins": [
|
|
4
|
-
"@typescript-eslint"
|
|
5
|
-
],
|
|
6
|
-
"extends": [
|
|
7
|
-
"eslint:recommended",
|
|
8
|
-
"plugin:@typescript-eslint/recommended",
|
|
9
|
-
"plugin:@typescript-eslint/recommended-requiring-type-checking"
|
|
10
|
-
],
|
|
11
|
-
"parserOptions": {
|
|
12
|
-
"project": "./tsconfig.json"
|
|
13
|
-
},
|
|
14
|
-
"rules": {
|
|
15
|
-
"max-len": [
|
|
16
|
-
"error",
|
|
17
|
-
{
|
|
18
|
-
"code": 100
|
|
19
|
-
}
|
|
20
|
-
],
|
|
21
|
-
"no-console": 1,
|
|
22
|
-
"no-extra-boolean-cast": 0,
|
|
23
|
-
"semi": 1,
|
|
24
|
-
"@typescript-eslint/no-misused-promises": 0,
|
|
25
|
-
"@typescript-eslint/no-floating-promises": 0
|
|
26
|
-
}
|
|
27
|
-
}
|
package/lib/auth-proj/build.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remove old files, copy front-end ones.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import fs from 'fs-extra';
|
|
6
|
-
import logger from 'jet-logger';
|
|
7
|
-
import childProcess from 'child_process';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// Start
|
|
11
|
-
(async () => {
|
|
12
|
-
try {
|
|
13
|
-
// Remove current build
|
|
14
|
-
await remove('./dist/');
|
|
15
|
-
// Copy front-end files
|
|
16
|
-
await copy('./src/public', './dist/public');
|
|
17
|
-
await copy('./src/views', './dist/views');
|
|
18
|
-
// Copy production env file
|
|
19
|
-
await copy('./src/pre-start/env/production.env', './dist/pre-start/env/production.env');
|
|
20
|
-
// Copy back-end files
|
|
21
|
-
await exec('tsc --build tsconfig.prod.json', './');
|
|
22
|
-
} catch (err) {
|
|
23
|
-
logger.err(err);
|
|
24
|
-
}
|
|
25
|
-
})();
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Remove file
|
|
29
|
-
*/
|
|
30
|
-
function remove(loc: string): Promise<void> {
|
|
31
|
-
return new Promise((res, rej) => {
|
|
32
|
-
return fs.remove(loc, (err) => {
|
|
33
|
-
return (!!err ? rej(err) : res());
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Copy file.
|
|
40
|
-
*/
|
|
41
|
-
function copy(src: string, dest: string): Promise<void> {
|
|
42
|
-
return new Promise((res, rej) => {
|
|
43
|
-
return fs.copy(src, dest, (err) => {
|
|
44
|
-
return (!!err ? rej(err) : res());
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Do command line command.
|
|
51
|
-
*/
|
|
52
|
-
function exec(cmd: string, loc: string): Promise<void> {
|
|
53
|
-
return new Promise((res, rej) => {
|
|
54
|
-
return childProcess.exec(cmd, {cwd: loc}, (err, stdout, stderr) => {
|
|
55
|
-
if (!!stdout) {
|
|
56
|
-
logger.info(stdout);
|
|
57
|
-
}
|
|
58
|
-
if (!!stderr) {
|
|
59
|
-
logger.warn(stderr);
|
|
60
|
-
}
|
|
61
|
-
return (!!err ? rej(err) : res());
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
}
|