express-generator-typescript 2.4.25 → 2.5.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.
Files changed (93) hide show
  1. package/README.md +24 -33
  2. package/lib/cli.js +5 -25
  3. package/lib/express-generator-typescript.js +11 -19
  4. package/lib/project-files/README.md +18 -1
  5. package/lib/{project-files-auth/env/development.env → project-files/config/.env.development} +0 -9
  6. package/lib/project-files/config/.env.production +14 -0
  7. package/lib/project-files/{env/test.env → config/.env.test} +0 -10
  8. package/lib/project-files/config.js +18 -0
  9. package/lib/project-files/config.ts +22 -0
  10. package/lib/project-files/eslint.config.ts +96 -0
  11. package/lib/project-files/gitignore +2 -0
  12. package/lib/project-files/package.json +16 -9
  13. package/lib/project-files/scripts/build.ts +4 -0
  14. package/lib/project-files/spec/support/Paths.ts +1 -6
  15. package/lib/project-files/spec/support/apiCb.ts +18 -6
  16. package/lib/project-files/spec/tests/users.spec.ts +28 -33
  17. package/lib/project-files/spec/types/misc.ts +7 -6
  18. package/lib/project-files/spec/types/supertest/index.d.ts +2 -2
  19. package/lib/project-files/src/common/Env.ts +10 -0
  20. package/lib/project-files/src/common/route-errors.ts +41 -0
  21. package/lib/project-files/src/index.ts +3 -3
  22. package/lib/project-files/src/models/User.ts +9 -57
  23. package/lib/project-files/src/repos/MockOrm.ts +0 -2
  24. package/lib/project-files/src/repos/database.json +1 -1
  25. package/lib/project-files/src/routes/UserRoutes.ts +14 -5
  26. package/lib/project-files/src/{common → routes/common}/Paths.ts +0 -4
  27. package/lib/project-files/src/routes/common/index.ts +37 -0
  28. package/lib/project-files/src/routes/index.ts +1 -1
  29. package/lib/project-files/src/server.ts +7 -9
  30. package/lib/project-files/src/services/UserService.ts +1 -1
  31. package/lib/project-files/src/util/misc.ts +0 -16
  32. package/lib/project-files/src/util/schema.ts +11 -0
  33. package/lib/project-files/src/util/validators.ts +322 -0
  34. package/lib/project-files/tsconfig.json +20 -71
  35. package/lib/project-files/tsconfig.prod.json +22 -2
  36. package/package.json +1 -1
  37. package/lib/project-files/env/development.env +0 -24
  38. package/lib/project-files/env/production.env +0 -25
  39. package/lib/project-files/eslint.config.mjs +0 -58
  40. package/lib/project-files/preload.js +0 -23
  41. package/lib/project-files/src/common/EnvVars.ts +0 -24
  42. package/lib/project-files/src/common/classes.ts +0 -32
  43. package/lib/project-files/src/common/misc.ts +0 -5
  44. package/lib/project-files/src/routes/common/check.ts +0 -143
  45. package/lib/project-files/src/routes/common/types.ts +0 -8
  46. package/lib/project-files-auth/README.md +0 -35
  47. package/lib/project-files-auth/env/production.env +0 -25
  48. package/lib/project-files-auth/env/test.env +0 -24
  49. package/lib/project-files-auth/eslint.config.mjs +0 -58
  50. package/lib/project-files-auth/gitignore +0 -3
  51. package/lib/project-files-auth/package.json +0 -60
  52. package/lib/project-files-auth/preload.js +0 -23
  53. package/lib/project-files-auth/scripts/build.ts +0 -62
  54. package/lib/project-files-auth/spec/index.ts +0 -46
  55. package/lib/project-files-auth/spec/support/Paths.ts +0 -10
  56. package/lib/project-files-auth/spec/support/apiCb.ts +0 -76
  57. package/lib/project-files-auth/spec/support/jasmine.json +0 -11
  58. package/lib/project-files-auth/spec/support/login.ts +0 -42
  59. package/lib/project-files-auth/spec/tests/auth.spec.ts +0 -115
  60. package/lib/project-files-auth/spec/tests/users.spec.ts +0 -190
  61. package/lib/project-files-auth/spec/types/misc.ts +0 -12
  62. package/lib/project-files-auth/spec/types/supertest/index.d.ts +0 -14
  63. package/lib/project-files-auth/src/common/EnvVars.ts +0 -24
  64. package/lib/project-files-auth/src/common/HttpStatusCodes.ts +0 -386
  65. package/lib/project-files-auth/src/common/Paths.ts +0 -20
  66. package/lib/project-files-auth/src/common/classes.ts +0 -31
  67. package/lib/project-files-auth/src/index.ts +0 -12
  68. package/lib/project-files-auth/src/models/User.ts +0 -89
  69. package/lib/project-files-auth/src/public/scripts/http.js +0 -26
  70. package/lib/project-files-auth/src/public/scripts/login.js +0 -15
  71. package/lib/project-files-auth/src/public/scripts/users.js +0 -138
  72. package/lib/project-files-auth/src/public/stylesheets/login.css +0 -24
  73. package/lib/project-files-auth/src/public/stylesheets/users.css +0 -43
  74. package/lib/project-files-auth/src/repos/MockOrm.ts +0 -42
  75. package/lib/project-files-auth/src/repos/UserRepo.ts +0 -93
  76. package/lib/project-files-auth/src/repos/database.json +0 -1
  77. package/lib/project-files-auth/src/routes/AuthRoutes.ts +0 -42
  78. package/lib/project-files-auth/src/routes/UserRoutes.ts +0 -55
  79. package/lib/project-files-auth/src/routes/common/check.ts +0 -143
  80. package/lib/project-files-auth/src/routes/common/types.ts +0 -22
  81. package/lib/project-files-auth/src/routes/index.ts +0 -43
  82. package/lib/project-files-auth/src/routes/middleware/adminMw.ts +0 -47
  83. package/lib/project-files-auth/src/server.ts +0 -91
  84. package/lib/project-files-auth/src/services/AuthService.ts +0 -57
  85. package/lib/project-files-auth/src/services/UserService.ts +0 -59
  86. package/lib/project-files-auth/src/util/PwdUtil.ts +0 -39
  87. package/lib/project-files-auth/src/util/SessionUtil.ts +0 -94
  88. package/lib/project-files-auth/src/util/misc.ts +0 -22
  89. package/lib/project-files-auth/src/views/login.html +0 -58
  90. package/lib/project-files-auth/src/views/users.html +0 -140
  91. package/lib/project-files-auth/tsconfig.json +0 -87
  92. package/lib/project-files-auth/tsconfig.prod.json +0 -12
  93. /package/lib/{project-files-auth/src/common/misc.ts → project-files/src/common/constants.ts} +0 -0
@@ -1,138 +0,0 @@
1
- // **** Variables **** //
2
-
3
- const DateFormatter = new Intl.DateTimeFormat('en-US', {
4
- year: 'numeric',
5
- month: '2-digit',
6
- day: '2-digit',
7
- });
8
-
9
- const formatDate = (date) => DateFormatter.format(new Date(date));
10
-
11
-
12
- // **** Run **** //
13
-
14
- // Start
15
- displayUsers();
16
-
17
- /**
18
- * Call api
19
- */
20
- function displayUsers() {
21
- Http
22
- .get('/api/users/all')
23
- .then(resp => resp.json())
24
- .then(resp => {
25
- var allUsersTemplate = document.getElementById('all-users-template'),
26
- allUsersTemplateHtml = allUsersTemplate.innerHTML,
27
- template = Handlebars.compile(allUsersTemplateHtml);
28
- var allUsersAnchor = document.getElementById('all-users-anchor');
29
- allUsersAnchor.innerHTML = template({
30
- users: resp.users.map(user => ({
31
- ...user,
32
- createdFormatted: formatDate(user.created),
33
- })),
34
- });
35
- });
36
- }
37
-
38
- // Setup event listener for button click
39
- document.addEventListener('click', event => {
40
- event.preventDefault();
41
- var ele = event.target;
42
- if (ele.matches('#add-user-btn')) {
43
- addUser();
44
- } else if (ele.matches('.edit-user-btn')) {
45
- showEditView(ele.parentNode.parentNode);
46
- } else if (ele.matches('.cancel-edit-btn')) {
47
- cancelEdit(ele.parentNode.parentNode);
48
- } else if (ele.matches('.submit-edit-btn')) {
49
- submitEdit(ele);
50
- } else if (ele.matches('.delete-user-btn')) {
51
- deleteUser(ele);
52
- } else if (ele.matches('#logout-btn')) {
53
- logoutUser();
54
- }
55
- }, false);
56
-
57
- /**
58
- * Add a new user.
59
- */
60
- function addUser() {
61
- var nameInput = document.getElementById('name-input');
62
- var emailInput = document.getElementById('email-input');
63
- var data = {
64
- user: {
65
- id: -1,
66
- name: nameInput.value,
67
- email: emailInput.value,
68
- created: new Date(),
69
- },
70
- };
71
- // Call api
72
- Http
73
- .post('/api/users/add', data)
74
- .then(() => {
75
- nameInput.value = '';
76
- emailInput.value = '';
77
- displayUsers();
78
- });
79
- }
80
-
81
- /**
82
- * Show edit view.
83
- */
84
- function showEditView(userEle) {
85
- var normalView = userEle.getElementsByClassName('normal-view')[0];
86
- var editView = userEle.getElementsByClassName('edit-view')[0];
87
- normalView.style.display = 'none';
88
- editView.style.display = 'block';
89
- }
90
-
91
- /**
92
- * Cancel edit.
93
- */
94
- function cancelEdit(userEle) {
95
- var normalView = userEle.getElementsByClassName('normal-view')[0];
96
- var editView = userEle.getElementsByClassName('edit-view')[0];
97
- normalView.style.display = 'block';
98
- editView.style.display = 'none';
99
- }
100
-
101
- /**
102
- * Submit edit.
103
- */
104
- function submitEdit(ele) {
105
- var userEle = ele.parentNode.parentNode;
106
- var nameInput = userEle.getElementsByClassName('name-edit-input')[0];
107
- var emailInput = userEle.getElementsByClassName('email-edit-input')[0];
108
- var id = ele.getAttribute('data-user-id');
109
- var created = ele.getAttribute('data-user-created');
110
- console.log(ele, created)
111
- var data = {
112
- user: {
113
- id: Number(id),
114
- name: nameInput.value,
115
- email: emailInput.value,
116
- created: new Date(created),
117
- },
118
- };
119
- Http
120
- .put('/api/users/update', data)
121
- .then(() => displayUsers());
122
- }
123
-
124
- /**
125
- * Delete a user
126
- */
127
- function deleteUser(ele) {
128
- var id = ele.getAttribute('data-user-id');
129
- Http
130
- .delete('/api/users/delete/' + id)
131
- .then(() => displayUsers());
132
- }
133
-
134
- function logoutUser() {
135
- Http
136
- .get('/api/auth/logout')
137
- .then(() => window.location.href = '/');
138
- }
@@ -1,24 +0,0 @@
1
- body {
2
- padding: 100px;
3
- font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4
- }
5
-
6
- body .login-block {
7
- display: inline-block;
8
- margin-right: 2em;
9
- vertical-align: top;
10
- }
11
-
12
- body .login-block .login-header {
13
- font-weight: bold;
14
- margin-bottom: 1em;
15
- }
16
-
17
- body .login-block .login-input {
18
- margin-bottom: 1em;
19
- }
20
-
21
- body .login-block .default-login-user {
22
- margin-top: 1em;
23
- color: #909090;
24
- }
@@ -1,43 +0,0 @@
1
- body {
2
- padding: 100px;
3
- font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4
- }
5
-
6
- body .users-column {
7
- display: inline-block;
8
- margin-right: 2em;
9
- vertical-align: top;
10
- }
11
-
12
- body .users-column .column-header {
13
- padding-bottom: 5px;
14
- font-weight: 700;
15
- font-size: 1.2em;
16
- }
17
-
18
-
19
- /** Add User Column **/
20
-
21
- body .add-user-col input {
22
- margin-bottom: 10px;
23
- }
24
-
25
-
26
- /** Users Display Column **/
27
-
28
- .email-edit {
29
- padding-bottom: 8px;
30
- }
31
-
32
- body .users-column .user-display-ele {
33
- padding-bottom: 10px;
34
- }
35
-
36
- body .users-column .user-display-ele button {
37
- margin-top: 2px;
38
- margin-bottom: 10px;
39
- }
40
-
41
- body .users-column .user-display-ele .edit-view {
42
- display: none;
43
- }
@@ -1,42 +0,0 @@
1
-
2
-
3
- import jsonfile from 'jsonfile';
4
-
5
- import { IUser } from '@src/models/User';
6
-
7
-
8
- // **** Variables **** //
9
-
10
- const DB_FILE_NAME = 'database.json';
11
-
12
-
13
- // **** Types **** //
14
-
15
- interface IDb {
16
- users: IUser[];
17
- }
18
-
19
-
20
- // **** Functions **** //
21
-
22
- /**
23
- * Fetch the json from the file.
24
- */
25
- function openDb(): Promise<IDb> {
26
- return jsonfile.readFile(__dirname + '/' + DB_FILE_NAME) as Promise<IDb>;
27
- }
28
-
29
- /**
30
- * Update the file.
31
- */
32
- function saveDb(db: IDb): Promise<void> {
33
- return jsonfile.writeFile((__dirname + '/' + DB_FILE_NAME), db);
34
- }
35
-
36
-
37
- // **** Export default **** //
38
-
39
- export default {
40
- openDb,
41
- saveDb,
42
- } as const;
@@ -1,93 +0,0 @@
1
- import { IUser } from '@src/models/User';
2
- import { getRandomInt } from '@src/util/misc';
3
- import orm from './MockOrm';
4
-
5
-
6
- // **** Functions **** //
7
-
8
- /**
9
- * Get one user.
10
- */
11
- async function getOne(email: string): Promise<IUser | null> {
12
- const db = await orm.openDb();
13
- for (const user of db.users) {
14
- if (user.email === email) {
15
- return user;
16
- }
17
- }
18
- return null;
19
- }
20
-
21
- /**
22
- * See if a user with the given id exists.
23
- */
24
- async function persists(id: number): Promise<boolean> {
25
- const db = await orm.openDb();
26
- for (const user of db.users) {
27
- if (user.id === id) {
28
- return true;
29
- }
30
- }
31
- return false;
32
- }
33
-
34
- /**
35
- * Get all users.
36
- */
37
- async function getAll(): Promise<IUser[]> {
38
- const db = await orm.openDb();
39
- return db.users;
40
- }
41
-
42
- /**
43
- * Add one user.
44
- */
45
- async function add(user: IUser): Promise<void> {
46
- const db = await orm.openDb();
47
- user.id = getRandomInt();
48
- db.users.push(user);
49
- return orm.saveDb(db);
50
- }
51
-
52
- /**
53
- * Update a user.
54
- */
55
- async function update(user: IUser): Promise<void> {
56
- const db = await orm.openDb();
57
- for (let i = 0; i < db.users.length; i++) {
58
- if (db.users[i].id === user.id) {
59
- const dbUser = db.users[i];
60
- db.users[i] = {
61
- ...dbUser,
62
- name: user.name,
63
- email: user.email,
64
- };
65
- return orm.saveDb(db);
66
- }
67
- }
68
- }
69
-
70
- /**
71
- * Delete one user.
72
- */
73
- async function delete_(id: number): Promise<void> {
74
- const db = await orm.openDb();
75
- for (let i = 0; i < db.users.length; i++) {
76
- if (db.users[i].id === id) {
77
- db.users.splice(i, 1);
78
- return orm.saveDb(db);
79
- }
80
- }
81
- }
82
-
83
-
84
- // **** Export default **** //
85
-
86
- export default {
87
- getOne,
88
- persists,
89
- getAll,
90
- add,
91
- update,
92
- delete: delete_,
93
- } as const;
@@ -1 +0,0 @@
1
- {"users":[{"name":"Sean Maxwell","email":"sean.maxwell@gmail.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":1,"id":159123164363,"created":"2024-03-22T05:20:55.079Z"},{"name":"John Smith","email":"john.smith@yahoo.com","pwdHash":"$2b$12$1mE2OI9hMS/rgH9Mi0s85OM2V5gzm7aF3gJIWH1y0S1MqVBueyjsy","role":0,"id":357437875835,"created":"2024-03-22T05:20:55.079Z"},{"id":28541714318,"name":"Gordan Freeman","email":"nova@prospect.com","created":"2024-03-23T15:56:42.615Z"}]}
@@ -1,42 +0,0 @@
1
- import HttpStatusCodes from '@src/common/HttpStatusCodes';
2
- import SessionUtil from '@src/util/SessionUtil';
3
- import AuthService from '@src/services/AuthService';
4
-
5
- import { IReq, IRes } from './common/types';
6
- import check from './common/check';
7
-
8
-
9
- // **** Functions **** //
10
-
11
- /**
12
- * Login a user.
13
- */
14
- async function login(req: IReq, res: IRes) {
15
- const [ email, password ] = check.isStr(req.body, ['email', 'password']),
16
- user = await AuthService.login(email, password);
17
- // Setup Admin Cookie
18
- await SessionUtil.addSessionData(res, {
19
- id: user.id,
20
- email: user.name,
21
- name: user.name,
22
- role: user.role,
23
- });
24
- // Return
25
- res.status(HttpStatusCodes.OK).end();
26
- }
27
-
28
- /**
29
- * Logout the user.
30
- */
31
- function logout(_: IReq, res: IRes) {
32
- SessionUtil.clearCookie(res);
33
- res.status(HttpStatusCodes.OK).end();
34
- }
35
-
36
-
37
- // **** Export default **** //
38
-
39
- export default {
40
- login,
41
- logout,
42
- } as const;
@@ -1,55 +0,0 @@
1
- import HttpStatusCodes from '@src/common/HttpStatusCodes';
2
-
3
- import UserService from '@src/services/UserService';
4
- import User from '@src/models/User';
5
-
6
- import { IReq, IRes } from './common/types';
7
- import check from './common/check';
8
-
9
-
10
- // **** Functions **** //
11
-
12
- /**
13
- * Get all users.
14
- */
15
- async function getAll(_: IReq, res: IRes) {
16
- const users = await UserService.getAll();
17
- res.status(HttpStatusCodes.OK).json({ users });
18
- }
19
-
20
- /**
21
- * Add one user.
22
- */
23
- async function add(req: IReq, res: IRes) {
24
- const user = check.isValid(req.body, 'user', User.isUser);
25
- await UserService.addOne(user);
26
- res.status(HttpStatusCodes.CREATED).end();
27
- }
28
-
29
- /**
30
- * Update one user.
31
- */
32
- async function update(req: IReq, res: IRes) {
33
- const user = check.isValid(req.body, 'user', User.isUser);
34
- await UserService.updateOne(user);
35
- res.status(HttpStatusCodes.OK).end();
36
- }
37
-
38
- /**
39
- * Delete one user.
40
- */
41
- async function delete_(req: IReq, res: IRes) {
42
- const id = check.isNum(req.params, 'id');
43
- await UserService.delete(id);
44
- res.status(HttpStatusCodes.OK).end();
45
- }
46
-
47
-
48
- // **** Export default **** //
49
-
50
- export default {
51
- getAll,
52
- add,
53
- update,
54
- delete: delete_,
55
- } as const;
@@ -1,143 +0,0 @@
1
- import { ValidationErr } from '@src/common/classes';
2
-
3
-
4
- type TReqObj = Record<string, unknown>;
5
-
6
-
7
- /**
8
- * Check that param/s is a string
9
- */
10
- function isStr(reqObj: TReqObj, params: string): string;
11
- function isStr(reqObj: TReqObj, params: readonly string[]): string[];
12
- function isStr(
13
- reqObj: TReqObj,
14
- params: string | readonly string[],
15
- ): string | string[] {
16
- return _checkWrapper(reqObj, params, _checkStr);
17
- }
18
-
19
- /**
20
- * Check validator for string
21
- */
22
- function _checkStr(val: unknown): string | undefined {
23
- if (!!val && typeof val === 'string') {
24
- return val;
25
- } else {
26
- return undefined;
27
- }
28
- }
29
-
30
- /**
31
- * Check that param/s is a number.
32
- */
33
- function isNum(reqObj: TReqObj, params: string): number;
34
- function isNum(reqObj: TReqObj, params: readonly string[]): number[];
35
- function isNum(
36
- reqObj: TReqObj,
37
- params: string | readonly string[],
38
- ): number | number[] {
39
- return _checkWrapper(reqObj, params, _checkNum);
40
- }
41
-
42
- /**
43
- * Check validator for string
44
- */
45
- function _checkNum(val: unknown): number | undefined {
46
- const valF = Number(val);
47
- if (!isNaN(valF)) {
48
- return valF;
49
- } else {
50
- return undefined;
51
- }
52
- }
53
-
54
- /**
55
- * Check that param/s is a number
56
- */
57
- function isBool(reqObj: TReqObj, params: string): boolean;
58
- function isBool(reqObj: TReqObj, params: readonly string[]): boolean[];
59
- function isBool(
60
- reqObj: TReqObj,
61
- params: string | readonly string[],
62
- ): boolean | boolean[] {
63
- return _checkWrapper(reqObj, params, _checkBool);
64
- }
65
-
66
- /**
67
- * Check validator for string
68
- */
69
- function _checkBool(val: unknown): boolean | undefined {
70
- if (typeof val === 'boolean') {
71
- return val;
72
- } else if (typeof val === 'string') {
73
- val = val.toLowerCase();
74
- if (val === 'true') {
75
- return true;
76
- } else if (val === 'false') {
77
- return false;
78
- } else {
79
- return undefined;
80
- }
81
- } else {
82
- return undefined;
83
- }
84
- }
85
-
86
- /**
87
- * Check that param satisfies the validator function.
88
- */
89
- function isValid<T>(
90
- reqObj: TReqObj,
91
- param: string,
92
- validatorFn: (param: unknown) => param is T,
93
- ): T {
94
- const val = reqObj[param];
95
- if (validatorFn(val)) {
96
- return val;
97
- } else {
98
- throw new ValidationErr(param);
99
- }
100
- }
101
-
102
-
103
- // **** Shared Helpers **** //
104
-
105
- /**
106
- * Do stuff based on whether or not params is an array
107
- */
108
- function _checkWrapper<T>(
109
- reqObj: TReqObj,
110
- params: string | readonly string[],
111
- checkFn: (val: unknown) => T | undefined,
112
- ): T | T[] {
113
- // If is array
114
- if (params instanceof Array) {
115
- const retVal: T[] = [];
116
- for (const param of params) {
117
- const val = checkFn(reqObj[param]);
118
- if (val !== undefined) {
119
- retVal.push(val);
120
- } else {
121
- throw new ValidationErr(param);
122
- }
123
- }
124
- return retVal;
125
- }
126
- // If not an array
127
- const val = checkFn(reqObj[params]);
128
- if (val !== undefined) {
129
- return val;
130
- }
131
- // Throw error
132
- throw new ValidationErr(params);
133
- }
134
-
135
-
136
- // **** Export Default **** //
137
-
138
- export default {
139
- isStr,
140
- isNum,
141
- isBool,
142
- isValid,
143
- } as const;
@@ -1,22 +0,0 @@
1
- /**
2
- * Shared types for routes.
3
- */
4
-
5
- import { Response, Request } from 'express';
6
- import { ISessionUser } from '@src/models/User';
7
-
8
-
9
- // **** Express **** //
10
-
11
- type TObj = Record<string, unknown>;
12
-
13
- export interface IReq extends Request<TObj, void, TObj, TObj> {
14
- signedCookies: Record<string, string>;
15
- }
16
-
17
-
18
- interface ILocals {
19
- sessionUser: ISessionUser;
20
- }
21
-
22
- export type IRes = Response<unknown, ILocals>;
@@ -1,43 +0,0 @@
1
- import { Router } from 'express';
2
-
3
- import Paths from '@src/common/Paths';
4
-
5
- import adminMw from './middleware/adminMw';
6
- import AuthRoutes from './AuthRoutes';
7
- import UserRoutes from './UserRoutes';
8
-
9
-
10
- // **** Variables **** //
11
-
12
- const apiRouter = Router();
13
-
14
-
15
- // **** AuthRouter **** //
16
-
17
- const authRouter = Router();
18
-
19
- // Routes
20
- authRouter.post(Paths.Auth.Login, AuthRoutes.login);
21
- authRouter.get(Paths.Auth.Logout, AuthRoutes.logout);
22
-
23
- // Add AuthRouter
24
- apiRouter.use(Paths.Auth.Base, authRouter);
25
-
26
-
27
- // **** UserRouter **** //
28
-
29
- const userRouter = Router();
30
-
31
- // User Routes
32
- userRouter.get(Paths.Users.Get, UserRoutes.getAll);
33
- userRouter.post(Paths.Users.Add, UserRoutes.add);
34
- userRouter.put(Paths.Users.Update, UserRoutes.update);
35
- userRouter.delete(Paths.Users.Delete, UserRoutes.delete);
36
-
37
- // Add UserRouter
38
- apiRouter.use(Paths.Users.Base, adminMw, userRouter);
39
-
40
-
41
- // **** Export default **** //
42
-
43
- export default apiRouter;
@@ -1,47 +0,0 @@
1
- import { NextFunction } from 'express';
2
- import { JwtPayload } from 'jsonwebtoken';
3
-
4
- import HttpStatusCodes from '@src/common/HttpStatusCodes';
5
- import SessionUtil from '@src/util/SessionUtil';
6
- import { ISessionUser, UserRoles } from '@src/models/User';
7
-
8
- import { IReq, IRes } from '../common/types';
9
-
10
-
11
- // **** Variables **** //
12
-
13
- const USER_UNAUTHORIZED_ERR = 'User not authorized to perform this action';
14
-
15
- type TSessionData = ISessionUser & JwtPayload;
16
-
17
-
18
- /**
19
- * See note at beginning of file.
20
- */
21
- async function adminMw(
22
- req: IReq,
23
- res: IRes,
24
- next: NextFunction,
25
- ) {
26
- // Get session data
27
- const sessionData = await SessionUtil.getSessionData<TSessionData>(req);
28
- // Set session data to locals
29
- if (
30
- typeof sessionData === 'object' &&
31
- sessionData?.role === UserRoles.Admin
32
- ) {
33
- res.locals.sessionUser = sessionData;
34
- return next();
35
- // Return an unauth error if user is not an admin
36
- } else {
37
- res
38
- .status(HttpStatusCodes.UNAUTHORIZED)
39
- .json({ error: USER_UNAUTHORIZED_ERR });
40
- return;
41
- }
42
- }
43
-
44
-
45
- // **** Export Default **** //
46
-
47
- export default adminMw;