express-generator-typescript 1.9.1 → 1.9.2
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/auth-proj/package-lock.json +6770 -0
- package/lib/auth-proj/src/index.ts +1 -1
- package/lib/auth-proj/src/pre-start/env/development.env +1 -1
- package/lib/auth-proj/src/repos/mock-orm.ts +13 -3
- package/lib/auth-proj/src/routes/auth-router.ts +1 -1
- package/lib/auth-proj/src/routes/middleware.ts +12 -6
- package/lib/auth-proj/src/routes/user-router.ts +1 -1
- package/lib/auth-proj/src/server.ts +2 -2
- package/lib/auth-proj/src/shared/functions.ts +2 -2
- package/lib/auth-proj/src/types/express/index.d.ts +22 -0
- package/package.json +1 -1
|
@@ -1,24 +1,34 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
2
|
+
|
|
1
3
|
import jsonfile from 'jsonfile';
|
|
4
|
+
import { IUser } from '@models/user-model';
|
|
2
5
|
|
|
3
6
|
|
|
4
|
-
// ****
|
|
7
|
+
// **** Variables **** //
|
|
5
8
|
|
|
6
9
|
const dbFilePath = 'src/repos/database.json';
|
|
7
10
|
|
|
8
11
|
|
|
12
|
+
// **** Types **** //
|
|
13
|
+
|
|
14
|
+
interface IDb {
|
|
15
|
+
users: IUser[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
9
19
|
// **** Functions **** //
|
|
10
20
|
|
|
11
21
|
/**
|
|
12
22
|
* Fetch the json from the file.
|
|
13
23
|
*/
|
|
14
|
-
function openDb(): Promise<
|
|
24
|
+
function openDb(): Promise<IDb> {
|
|
15
25
|
return jsonfile.readFile(dbFilePath);
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
29
|
* Update the file.
|
|
20
30
|
*/
|
|
21
|
-
function saveDb(db:
|
|
31
|
+
function saveDb(db: IDb): Promise<void> {
|
|
22
32
|
return jsonfile.writeFile(dbFilePath, db);
|
|
23
33
|
}
|
|
24
34
|
|
|
@@ -27,7 +27,7 @@ export const p = {
|
|
|
27
27
|
router.post(p.login, async (req: Request, res: Response) => {
|
|
28
28
|
// Check email and password present
|
|
29
29
|
const { email, password } = req.body;
|
|
30
|
-
if (!
|
|
30
|
+
if (!email || !password) {
|
|
31
31
|
throw new ParamMissingError();
|
|
32
32
|
}
|
|
33
33
|
// Get jwt
|
|
@@ -18,8 +18,9 @@ const jwtNotPresentErr = 'JWT not present in signed cookie.';
|
|
|
18
18
|
* Middleware to verify if user is an admin.
|
|
19
19
|
*/
|
|
20
20
|
export async function adminMw(req: Request, res: Response, next: NextFunction) {
|
|
21
|
+
// Try
|
|
21
22
|
try {
|
|
22
|
-
//
|
|
23
|
+
// Extract the token
|
|
23
24
|
const jwt = req.signedCookies[envVars.cookieProps.key];
|
|
24
25
|
if (!jwt) {
|
|
25
26
|
throw Error(jwtNotPresentErr);
|
|
@@ -32,9 +33,14 @@ export async function adminMw(req: Request, res: Response, next: NextFunction) {
|
|
|
32
33
|
} else {
|
|
33
34
|
throw Error(jwtNotPresentErr);
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
// Catch
|
|
37
|
+
} catch (err: unknown) {
|
|
38
|
+
let error;
|
|
39
|
+
if (typeof err === 'string') {
|
|
40
|
+
error = err;
|
|
41
|
+
} else if (err instanceof Error) {
|
|
42
|
+
error = err.message;
|
|
43
|
+
}
|
|
44
|
+
return res.status(UNAUTHORIZED).json({ error });
|
|
39
45
|
}
|
|
40
|
-
}
|
|
46
|
+
}
|
|
@@ -3,7 +3,7 @@ import morgan from 'morgan';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import helmet from 'helmet';
|
|
5
5
|
import StatusCodes from 'http-status-codes';
|
|
6
|
-
import express, {
|
|
6
|
+
import express, { Request, Response } from 'express';
|
|
7
7
|
|
|
8
8
|
import 'express-async-errors';
|
|
9
9
|
|
|
@@ -41,7 +41,7 @@ if (envVars.nodeEnv === 'production') {
|
|
|
41
41
|
app.use('/api', BaseRouter);
|
|
42
42
|
|
|
43
43
|
// Error handling
|
|
44
|
-
app.use((err: Error | CustomError, _: Request, res: Response
|
|
44
|
+
app.use((err: Error | CustomError, _: Request, res: Response) => {
|
|
45
45
|
logger.err(err, true);
|
|
46
46
|
const status = (err instanceof CustomError ? err.HttpStatus : StatusCodes.BAD_REQUEST);
|
|
47
47
|
return res.status(status).json({
|
|
@@ -8,11 +8,11 @@ export function pErr(err?: Error): void {
|
|
|
8
8
|
if (!!err) {
|
|
9
9
|
logger.err(err);
|
|
10
10
|
}
|
|
11
|
-
}
|
|
11
|
+
}
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Get a random number between 1 and 1,000,000,000,000
|
|
15
15
|
*/
|
|
16
16
|
export function getRandomInt(): number {
|
|
17
17
|
return Math.floor(Math.random() * 1_000_000_000_000);
|
|
18
|
-
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { IUser } from "@models/user-model";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
declare module 'express' {
|
|
5
|
+
export interface Request {
|
|
6
|
+
signedCookies: Record<string, string>,
|
|
7
|
+
body: {
|
|
8
|
+
user?: IUser
|
|
9
|
+
email?: string;
|
|
10
|
+
password?: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Put this back in the future for middleware stuff
|
|
16
|
+
// declare global {
|
|
17
|
+
// namespace Express {
|
|
18
|
+
// export interface Response {
|
|
19
|
+
// sessionUser: IClientData;
|
|
20
|
+
// }
|
|
21
|
+
// }
|
|
22
|
+
// }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-generator-typescript",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.2",
|
|
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",
|