typescript-express-starter 6.2.1 → 6.3.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.kr.md +35 -12
- package/README.md +36 -13
- package/bin/cli.js +4 -3
- package/lib/default/.swcrc +41 -0
- package/lib/default/nodemon.json +2 -2
- package/lib/default/package.json +12 -8
- package/lib/default/src/app.ts +1 -2
- package/lib/default/src/http/auth.http +12 -15
- package/lib/default/src/http/users.http +12 -16
- package/lib/default/src/index.ts +1 -0
- package/lib/default/src/middlewares/auth.middleware.ts +3 -3
- package/lib/default/src/models/users.model.ts +5 -5
- package/lib/default/src/server.ts +1 -2
- package/lib/default/src/services/auth.service.ts +5 -5
- package/lib/default/src/services/users.service.ts +3 -3
- package/lib/default/src/tests/auth.test.ts +4 -4
- package/lib/default/src/tests/users.test.ts +4 -4
- package/lib/default/src/utils/logger.ts +5 -5
- package/lib/default/tsconfig.json +2 -2
- package/lib/knex/.swcrc +39 -0
- package/lib/knex/knexfile.ts +6 -6
- package/lib/knex/nodemon.json +2 -2
- package/lib/knex/package.json +11 -8
- package/lib/knex/src/app.ts +2 -3
- package/lib/knex/src/databases/index.ts +6 -6
- package/lib/knex/src/http/auth.http +12 -15
- package/lib/knex/src/http/users.http +12 -16
- package/lib/knex/src/index.ts +1 -0
- package/lib/knex/src/middlewares/auth.middleware.ts +4 -4
- package/lib/knex/src/server.ts +1 -2
- package/lib/knex/src/services/auth.service.ts +5 -5
- package/lib/knex/src/services/users.service.ts +3 -3
- package/lib/knex/src/tests/auth.test.ts +2 -2
- package/lib/knex/src/tests/index.test.ts +1 -1
- package/lib/knex/src/tests/users.test.ts +2 -2
- package/lib/knex/src/utils/logger.ts +5 -5
- package/lib/knex/tsconfig.json +2 -2
- package/lib/mongoose/.swcrc +39 -0
- package/lib/mongoose/docker-compose.yml +0 -4
- package/lib/mongoose/nodemon.json +2 -2
- package/lib/mongoose/package.json +12 -8
- package/lib/mongoose/src/app.ts +2 -3
- package/lib/mongoose/src/configs/development.json +1 -1
- package/lib/mongoose/src/configs/production.json +1 -1
- package/lib/mongoose/src/configs/test.json +1 -1
- package/lib/mongoose/src/http/auth.http +32 -0
- package/lib/mongoose/src/http/users.http +34 -0
- package/lib/mongoose/src/index.ts +1 -0
- package/lib/mongoose/src/middlewares/auth.middleware.ts +3 -3
- package/lib/mongoose/src/server.ts +1 -2
- package/lib/mongoose/src/services/auth.service.ts +5 -5
- package/lib/mongoose/src/services/users.service.ts +3 -3
- package/lib/mongoose/src/utils/logger.ts +5 -5
- package/lib/mongoose/tsconfig.json +2 -2
- package/lib/prisma/.env +1 -1
- package/lib/prisma/.swcrc +37 -0
- package/lib/prisma/docker-compose.yml +1 -6
- package/lib/prisma/nodemon.json +2 -2
- package/lib/prisma/package.json +14 -10
- package/lib/prisma/src/app.ts +2 -3
- package/lib/prisma/src/http/auth.http +12 -15
- package/lib/prisma/src/http/users.http +12 -16
- package/lib/prisma/src/index.ts +1 -0
- package/lib/prisma/src/middlewares/auth.middleware.ts +4 -4
- package/lib/prisma/src/server.ts +1 -2
- package/lib/prisma/src/services/auth.service.ts +5 -5
- package/lib/prisma/src/services/users.service.ts +3 -3
- package/lib/prisma/src/utils/logger.ts +5 -5
- package/lib/prisma/tsconfig.json +2 -2
- package/lib/routing-controllers/.swcrc +37 -0
- package/lib/routing-controllers/nodemon.json +2 -2
- package/lib/routing-controllers/package.json +12 -9
- package/lib/routing-controllers/src/app.ts +3 -4
- package/lib/routing-controllers/src/http/auth.http +12 -15
- package/lib/routing-controllers/src/http/users.http +12 -16
- package/lib/routing-controllers/src/index.ts +1 -0
- package/lib/routing-controllers/src/middlewares/auth.middleware.ts +3 -3
- package/lib/routing-controllers/src/middlewares/validation.middleware.ts +10 -2
- package/lib/routing-controllers/src/models/users.model.ts +5 -5
- package/lib/routing-controllers/src/server.ts +1 -3
- package/lib/routing-controllers/src/services/auth.service.ts +5 -5
- package/lib/routing-controllers/src/services/users.service.ts +3 -3
- package/lib/routing-controllers/src/utils/logger.ts +5 -5
- package/lib/routing-controllers/tsconfig.json +2 -2
- package/lib/sequelize/.swcrc +39 -0
- package/lib/sequelize/docker-compose.yml +1 -6
- package/lib/sequelize/nodemon.json +2 -2
- package/lib/sequelize/package.json +12 -8
- package/lib/sequelize/src/app.ts +1 -2
- package/lib/sequelize/src/configs/development.json +2 -2
- package/lib/sequelize/src/configs/production.json +1 -1
- package/lib/sequelize/src/configs/test.json +1 -1
- package/lib/sequelize/src/http/auth.http +32 -0
- package/lib/sequelize/src/http/users.http +34 -0
- package/lib/sequelize/src/index.ts +1 -0
- package/lib/sequelize/src/middlewares/auth.middleware.ts +4 -4
- package/lib/sequelize/src/server.ts +1 -2
- package/lib/sequelize/src/services/auth.service.ts +5 -5
- package/lib/sequelize/src/services/users.service.ts +3 -3
- package/lib/sequelize/src/utils/logger.ts +5 -5
- package/lib/sequelize/tsconfig.json +2 -2
- package/lib/starter.js +4 -6
- package/lib/typeorm/.swcrc +39 -0
- package/lib/typeorm/docker-compose.yml +1 -1
- package/lib/typeorm/nodemon.json +2 -2
- package/lib/typeorm/package.json +12 -8
- package/lib/typeorm/src/app.ts +1 -2
- package/lib/typeorm/src/configs/development.json +1 -1
- package/lib/typeorm/src/configs/production.json +1 -1
- package/lib/typeorm/src/configs/test.json +1 -1
- package/lib/typeorm/src/databases/index.ts +5 -5
- package/lib/typeorm/src/{entity → entities}/users.entity.ts +3 -3
- package/lib/typeorm/src/http/auth.http +12 -15
- package/lib/typeorm/src/http/users.http +12 -16
- package/lib/typeorm/src/index.ts +1 -0
- package/lib/typeorm/src/middlewares/auth.middleware.ts +5 -9
- package/lib/typeorm/src/server.ts +1 -2
- package/lib/typeorm/src/services/auth.service.ts +13 -17
- package/lib/typeorm/src/services/users.service.ts +16 -22
- package/lib/typeorm/src/utils/logger.ts +5 -5
- package/lib/typeorm/tsconfig.json +3 -3
- package/package.json +11 -4
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import config from 'config';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import { existsSync, mkdirSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
4
|
import winston from 'winston';
|
|
5
5
|
import winstonDaily from 'winston-daily-rotate-file';
|
|
6
6
|
|
|
7
7
|
// logs dir
|
|
8
|
-
const logDir: string =
|
|
8
|
+
const logDir: string = join(__dirname, config.get('log.dir'));
|
|
9
9
|
|
|
10
|
-
if (!
|
|
11
|
-
|
|
10
|
+
if (!existsSync(logDir)) {
|
|
11
|
+
mkdirSync(logDir);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// Define log format
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"pretty": true,
|
|
14
14
|
"sourceMap": true,
|
|
15
15
|
"declaration": true,
|
|
16
|
-
"outDir": "
|
|
16
|
+
"outDir": "dist",
|
|
17
17
|
"allowJs": true,
|
|
18
18
|
"noEmit": false,
|
|
19
19
|
"esModuleInterop": true,
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"include": ["src/**/*.ts", "src/**/*.json", ".env"],
|
|
38
|
-
"exclude": ["node_modules"]
|
|
38
|
+
"exclude": ["node_modules", "src/http", "src/logs", "src/tests"]
|
|
39
39
|
}
|
package/lib/starter.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/*****************************************************************
|
|
2
|
-
* Create
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Create Typescript Express Starter
|
|
3
|
+
* 2019.12.18 ~ 🎮
|
|
4
|
+
* Made By AGUMON 🦖
|
|
5
|
+
* https://github.com/ljlm0402/typescript-express-starter
|
|
5
6
|
*****************************************************************/
|
|
6
7
|
|
|
7
8
|
const chalk = require("chalk");
|
|
@@ -44,14 +45,11 @@ const createProject = async (projectName) => {
|
|
|
44
45
|
spinner = ora();
|
|
45
46
|
spinner.start();
|
|
46
47
|
|
|
47
|
-
// manage node_modules
|
|
48
48
|
await installNodeModules(projectName, spinner);
|
|
49
49
|
isUpdated && (await updateNodeModules(projectName, spinner));
|
|
50
50
|
isDeduped && (await dedupeNodeModules(projectName, spinner));
|
|
51
|
-
|
|
52
51
|
await postInstallScripts(projectName, template, spinner);
|
|
53
52
|
|
|
54
|
-
// set gitignore
|
|
55
53
|
await createGitignore(projectName, spinner);
|
|
56
54
|
await initGit(projectName);
|
|
57
55
|
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"jsc": {
|
|
3
|
+
"parser": {
|
|
4
|
+
"syntax": "typescript",
|
|
5
|
+
"tsx": false,
|
|
6
|
+
"dynamicImport": true,
|
|
7
|
+
"decorators": true
|
|
8
|
+
},
|
|
9
|
+
"transform": {
|
|
10
|
+
"legacyDecorator": true,
|
|
11
|
+
"decoratorMetadata": true
|
|
12
|
+
},
|
|
13
|
+
"target": "es2017",
|
|
14
|
+
"externalHelpers": false,
|
|
15
|
+
"keepClassNames": true,
|
|
16
|
+
"loose": false,
|
|
17
|
+
"minify": {
|
|
18
|
+
"compress": false,
|
|
19
|
+
"mangle": false
|
|
20
|
+
},
|
|
21
|
+
"baseUrl": "src",
|
|
22
|
+
"paths": {
|
|
23
|
+
"@/*": ["*"],
|
|
24
|
+
"@controllers/*": ["controllers/*"],
|
|
25
|
+
"@databases": ["databases"],
|
|
26
|
+
"@dtos/*": ["dtos/*"],
|
|
27
|
+
"@entities/*": ["entities/*"],
|
|
28
|
+
"@exceptions/*": ["exceptions/*"],
|
|
29
|
+
"@interfaces/*": ["interfaces/*"],
|
|
30
|
+
"@middlewares/*": ["middlewares/*"],
|
|
31
|
+
"@routes/*": ["routes/*"],
|
|
32
|
+
"@services/*": ["services/*"],
|
|
33
|
+
"@utils/*": ["utils/*"]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"module": {
|
|
37
|
+
"type": "commonjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/lib/typeorm/nodemon.json
CHANGED
package/lib/typeorm/package.json
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
"scripts": {
|
|
8
8
|
"start": "npm run build && cross-env NODE_ENV=production node dist/server.js",
|
|
9
9
|
"dev": "cross-env NODE_ENV=development nodemon",
|
|
10
|
-
"build": "
|
|
10
|
+
"build": "swc src -d dist --source-maps --copy-files",
|
|
11
|
+
"build:tsc": "tsc && tsc-alias",
|
|
11
12
|
"test": "jest --forceExit --detectOpenHandles",
|
|
12
13
|
"lint": "eslint --ignore-path .gitignore --ext .ts src/",
|
|
13
14
|
"lint:fix": "npm run lint -- --fix",
|
|
@@ -22,28 +23,24 @@
|
|
|
22
23
|
"config": "^3.3.6",
|
|
23
24
|
"cookie-parser": "^1.4.5",
|
|
24
25
|
"cors": "^2.8.5",
|
|
25
|
-
"cross-env": "^7.0.3",
|
|
26
26
|
"dotenv": "^10.0.0",
|
|
27
27
|
"envalid": "^7.1.0",
|
|
28
28
|
"express": "^4.17.1",
|
|
29
29
|
"helmet": "^4.6.0",
|
|
30
30
|
"hpp": "^0.2.3",
|
|
31
|
-
"jest": "^27.0.6",
|
|
32
31
|
"jsonwebtoken": "^8.5.1",
|
|
33
32
|
"morgan": "^1.10.0",
|
|
34
|
-
"pm2": "^5.1.0",
|
|
35
33
|
"pg": "^8.6.0",
|
|
36
34
|
"reflect-metadata": "^0.1.13",
|
|
37
35
|
"swagger-jsdoc": "^6.0.0",
|
|
38
36
|
"swagger-ui-express": "^4.1.6",
|
|
39
|
-
"ts-jest": "^27.0.3",
|
|
40
|
-
"ts-node": "^10.0.0",
|
|
41
37
|
"typeorm": "^0.2.34",
|
|
42
|
-
"typescript": "^4.3.5",
|
|
43
38
|
"winston": "^3.3.3",
|
|
44
39
|
"winston-daily-rotate-file": "^4.5.5"
|
|
45
40
|
},
|
|
46
41
|
"devDependencies": {
|
|
42
|
+
"@swc/cli": "^0.1.51",
|
|
43
|
+
"@swc/core": "^1.2.108",
|
|
47
44
|
"@types/bcrypt": "^5.0.0",
|
|
48
45
|
"@types/compression": "^1.7.1",
|
|
49
46
|
"@types/config": "^0.0.39",
|
|
@@ -63,16 +60,23 @@
|
|
|
63
60
|
"@types/winston": "^2.4.4",
|
|
64
61
|
"@typescript-eslint/eslint-plugin": "^4.28.2",
|
|
65
62
|
"@typescript-eslint/parser": "^4.28.2",
|
|
63
|
+
"cross-env": "^7.0.3",
|
|
66
64
|
"eslint": "^7.30.0",
|
|
67
65
|
"eslint-config-prettier": "^8.3.0",
|
|
68
66
|
"eslint-plugin-prettier": "^3.4.0",
|
|
69
67
|
"husky": "^7.0.1",
|
|
68
|
+
"jest": "^27.0.6",
|
|
70
69
|
"lint-staged": "^11.0.0",
|
|
71
70
|
"node-config": "^0.0.2",
|
|
72
71
|
"node-gyp": "^8.1.0",
|
|
73
72
|
"nodemon": "^2.0.9",
|
|
73
|
+
"pm2": "^5.1.0",
|
|
74
74
|
"prettier": "^2.3.2",
|
|
75
75
|
"supertest": "^6.1.3",
|
|
76
|
-
"
|
|
76
|
+
"ts-jest": "^27.0.7",
|
|
77
|
+
"ts-node": "^10.4.0",
|
|
78
|
+
"tsc-alias": "^1.4.1",
|
|
79
|
+
"tsconfig-paths": "^3.10.1",
|
|
80
|
+
"typescript": "^4.5.2"
|
|
77
81
|
}
|
|
78
82
|
}
|
package/lib/typeorm/src/app.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import config from 'config';
|
|
2
|
-
import
|
|
2
|
+
import { join } from 'path';
|
|
3
3
|
import { ConnectionOptions } from 'typeorm';
|
|
4
4
|
import { dbConfig } from '@interfaces/db.interface';
|
|
5
5
|
|
|
@@ -13,11 +13,11 @@ export const dbConnection: ConnectionOptions = {
|
|
|
13
13
|
database: database,
|
|
14
14
|
synchronize: true,
|
|
15
15
|
logging: false,
|
|
16
|
-
entities: [
|
|
17
|
-
migrations: [
|
|
18
|
-
subscribers: [
|
|
16
|
+
entities: [join(__dirname, '../**/*.entity{.ts,.js}')],
|
|
17
|
+
migrations: [join(__dirname, '../**/*.migration{.ts,.js}')],
|
|
18
|
+
subscribers: [join(__dirname, '../**/*.subscriber{.ts,.js}')],
|
|
19
19
|
cli: {
|
|
20
|
-
entitiesDir: 'src/
|
|
20
|
+
entitiesDir: 'src/entities',
|
|
21
21
|
migrationsDir: 'src/migration',
|
|
22
22
|
subscribersDir: 'src/subscriber',
|
|
23
23
|
},
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { IsNotEmpty } from 'class-validator';
|
|
2
|
-
import { Entity, PrimaryGeneratedColumn, Column, Unique, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
|
2
|
+
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, Unique, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
|
3
3
|
import { User } from '@interfaces/users.interface';
|
|
4
4
|
|
|
5
5
|
@Entity()
|
|
6
|
-
|
|
7
|
-
export class UserEntity implements User {
|
|
6
|
+
export class UserEntity extends BaseEntity implements User {
|
|
8
7
|
@PrimaryGeneratedColumn()
|
|
9
8
|
id: number;
|
|
10
9
|
|
|
11
10
|
@Column()
|
|
12
11
|
@IsNotEmpty()
|
|
12
|
+
@Unique(['email'])
|
|
13
13
|
email: string;
|
|
14
14
|
|
|
15
15
|
@Column()
|
|
@@ -1,35 +1,32 @@
|
|
|
1
|
-
# auth API
|
|
2
|
-
|
|
3
|
-
###
|
|
4
1
|
# baseURL
|
|
5
2
|
@baseURL = http://localhost:3000
|
|
6
3
|
|
|
7
4
|
###
|
|
8
|
-
#
|
|
9
|
-
POST {{baseURL}}/signup
|
|
5
|
+
# User Signup
|
|
6
|
+
POST {{ baseURL }}/signup
|
|
10
7
|
Content-Type: application/json
|
|
11
8
|
|
|
12
9
|
{
|
|
13
|
-
"email": "example@
|
|
14
|
-
"password": "
|
|
10
|
+
"email": "example@email.com",
|
|
11
|
+
"password": "password"
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
###
|
|
18
|
-
# Login
|
|
19
|
-
POST {{baseURL}}/login
|
|
15
|
+
# User Login
|
|
16
|
+
POST {{ baseURL }}/login
|
|
20
17
|
Content-Type: application/json
|
|
21
18
|
|
|
22
19
|
{
|
|
23
|
-
"email": "
|
|
24
|
-
"password": "
|
|
20
|
+
"email": "example@email.com",
|
|
21
|
+
"password": "password"
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
###
|
|
28
|
-
# Logout
|
|
29
|
-
POST {{baseURL}}/logout
|
|
25
|
+
# User Logout
|
|
26
|
+
POST {{ baseURL }}/logout
|
|
30
27
|
Content-Type: application/json
|
|
31
28
|
|
|
32
29
|
{
|
|
33
|
-
"email": "
|
|
34
|
-
"password": "
|
|
30
|
+
"email": "example@email.com",
|
|
31
|
+
"password": "password"
|
|
35
32
|
}
|
|
@@ -1,38 +1,34 @@
|
|
|
1
|
-
# users API
|
|
2
|
-
|
|
3
|
-
###
|
|
4
1
|
# baseURL
|
|
5
2
|
@baseURL = http://localhost:3000
|
|
6
3
|
|
|
7
4
|
###
|
|
8
5
|
# Find All Users
|
|
9
|
-
GET {{baseURL}}/users
|
|
6
|
+
GET {{ baseURL }}/users
|
|
10
7
|
|
|
11
8
|
###
|
|
12
9
|
# Find User By Id
|
|
13
|
-
GET {{baseURL}}/users/1
|
|
14
|
-
|
|
10
|
+
GET {{ baseURL }}/users/1
|
|
15
11
|
|
|
16
12
|
###
|
|
17
|
-
#
|
|
18
|
-
POST {{baseURL}}/users
|
|
13
|
+
# Create User
|
|
14
|
+
POST {{ baseURL }}/users
|
|
19
15
|
Content-Type: application/json
|
|
20
16
|
|
|
21
17
|
{
|
|
22
|
-
"email": "example@
|
|
23
|
-
"password": "
|
|
18
|
+
"email": "example@email.com",
|
|
19
|
+
"password": "password"
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
###
|
|
27
|
-
#
|
|
28
|
-
PUT {{baseURL}}/users/1
|
|
23
|
+
# Modify User By Id
|
|
24
|
+
PUT {{ baseURL }}/users/1
|
|
29
25
|
Content-Type: application/json
|
|
30
26
|
|
|
31
27
|
{
|
|
32
|
-
"email": "example@
|
|
33
|
-
"password": "
|
|
28
|
+
"email": "example@email.com",
|
|
29
|
+
"password": "password"
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
###
|
|
37
|
-
#
|
|
38
|
-
DELETE {{baseURL}}/users/1
|
|
33
|
+
# Delete User By Id
|
|
34
|
+
DELETE {{ baseURL }}/users/1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
process.env['NODE_CONFIG_DIR'] = __dirname + '/configs';
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import config from 'config';
|
|
2
2
|
import { NextFunction, Response } from 'express';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import { UserEntity } from '@entity/users.entity';
|
|
3
|
+
import { verify } from 'jsonwebtoken';
|
|
4
|
+
import { UserEntity } from '@entities/users.entity';
|
|
6
5
|
import { HttpException } from '@exceptions/HttpException';
|
|
7
6
|
import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
|
|
8
7
|
|
|
9
8
|
const authMiddleware = async (req: RequestWithUser, res: Response, next: NextFunction) => {
|
|
10
9
|
try {
|
|
11
|
-
const Authorization = req.cookies['Authorization'] || req.header('Authorization').split('Bearer ')[1]
|
|
10
|
+
const Authorization = req.cookies['Authorization'] || (req.header('Authorization') ? req.header('Authorization').split('Bearer ')[1] : null);
|
|
12
11
|
|
|
13
12
|
if (Authorization) {
|
|
14
13
|
const secretKey: string = config.get('secretKey');
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
const userRepository = getRepository(UserEntity);
|
|
19
|
-
const findUser = await userRepository.findOne(userId, { select: ['id', 'email', 'password'] });
|
|
14
|
+
const { id } = (await verify(Authorization, secretKey)) as DataStoredInToken;
|
|
15
|
+
const findUser = await UserEntity.findOne(id, { select: ['id', 'email', 'password'] });
|
|
20
16
|
|
|
21
17
|
if (findUser) {
|
|
22
18
|
req.user = findUser;
|
|
@@ -1,37 +1,34 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { compare, hash } from 'bcrypt';
|
|
2
2
|
import config from 'config';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
3
|
+
import { sign } from 'jsonwebtoken';
|
|
4
|
+
import { EntityRepository, Repository } from 'typeorm';
|
|
5
5
|
import { CreateUserDto } from '@dtos/users.dto';
|
|
6
|
-
import { UserEntity } from '@
|
|
6
|
+
import { UserEntity } from '@entities/users.entity';
|
|
7
7
|
import { HttpException } from '@exceptions/HttpException';
|
|
8
8
|
import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
|
|
9
9
|
import { User } from '@interfaces/users.interface';
|
|
10
10
|
import { isEmpty } from '@utils/util';
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
@EntityRepository()
|
|
13
|
+
class AuthService extends Repository<UserEntity> {
|
|
15
14
|
public async signup(userData: CreateUserDto): Promise<User> {
|
|
16
15
|
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
17
16
|
|
|
18
|
-
const
|
|
19
|
-
const findUser: User = await userRepository.findOne({ where: { email: userData.email } });
|
|
17
|
+
const findUser: User = await UserEntity.findOne({ where: { email: userData.email } });
|
|
20
18
|
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
21
19
|
|
|
22
|
-
const hashedPassword = await
|
|
23
|
-
const createUserData: User = await
|
|
20
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
21
|
+
const createUserData: User = await UserEntity.create({ ...userData, password: hashedPassword }).save();
|
|
24
22
|
return createUserData;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
public async login(userData: CreateUserDto): Promise<{ cookie: string; findUser: User }> {
|
|
28
26
|
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
29
27
|
|
|
30
|
-
const
|
|
31
|
-
const findUser: User = await userRepository.findOne({ where: { email: userData.email } });
|
|
28
|
+
const findUser: User = await UserEntity.findOne({ where: { email: userData.email } });
|
|
32
29
|
if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
|
|
33
30
|
|
|
34
|
-
const isPasswordMatching: boolean = await
|
|
31
|
+
const isPasswordMatching: boolean = await compare(userData.password, findUser.password);
|
|
35
32
|
if (!isPasswordMatching) throw new HttpException(409, "You're password not matching");
|
|
36
33
|
|
|
37
34
|
const tokenData = this.createToken(findUser);
|
|
@@ -43,8 +40,7 @@ class AuthService {
|
|
|
43
40
|
public async logout(userData: User): Promise<User> {
|
|
44
41
|
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
45
42
|
|
|
46
|
-
const
|
|
47
|
-
const findUser: User = await userRepository.findOne({ where: { email: userData.email, password: userData.password } });
|
|
43
|
+
const findUser: User = await UserEntity.findOne({ where: { email: userData.email, password: userData.password } });
|
|
48
44
|
if (!findUser) throw new HttpException(409, "You're not user");
|
|
49
45
|
|
|
50
46
|
return findUser;
|
|
@@ -55,7 +51,7 @@ class AuthService {
|
|
|
55
51
|
const secretKey: string = config.get('secretKey');
|
|
56
52
|
const expiresIn: number = 60 * 60;
|
|
57
53
|
|
|
58
|
-
return { expiresIn, token:
|
|
54
|
+
return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
|
|
59
55
|
}
|
|
60
56
|
|
|
61
57
|
public createCookie(tokenData: TokenData): string {
|
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { hash } from 'bcrypt';
|
|
2
|
+
import { EntityRepository, Repository } from 'typeorm';
|
|
3
3
|
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
|
-
import { UserEntity } from '@
|
|
4
|
+
import { UserEntity } from '@entities/users.entity';
|
|
5
5
|
import { HttpException } from '@exceptions/HttpException';
|
|
6
6
|
import { User } from '@interfaces/users.interface';
|
|
7
7
|
import { isEmpty } from '@utils/util';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
@EntityRepository()
|
|
10
|
+
class UserService extends Repository<UserEntity> {
|
|
12
11
|
public async findAllUser(): Promise<User[]> {
|
|
13
|
-
const
|
|
14
|
-
const users: User[] = await userRepository.find();
|
|
12
|
+
const users: User[] = await UserEntity.find();
|
|
15
13
|
return users;
|
|
16
14
|
}
|
|
17
15
|
|
|
18
16
|
public async findUserById(userId: number): Promise<User> {
|
|
19
17
|
if (isEmpty(userId)) throw new HttpException(400, "You're not userId");
|
|
20
18
|
|
|
21
|
-
const
|
|
22
|
-
const findUser: User = await userRepository.findOne({ where: { id: userId } });
|
|
19
|
+
const findUser: User = await UserEntity.findOne({ where: { id: userId } });
|
|
23
20
|
if (!findUser) throw new HttpException(409, "You're not user");
|
|
24
21
|
|
|
25
22
|
return findUser;
|
|
@@ -28,12 +25,11 @@ class UserService {
|
|
|
28
25
|
public async createUser(userData: CreateUserDto): Promise<User> {
|
|
29
26
|
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
30
27
|
|
|
31
|
-
const
|
|
32
|
-
const findUser: User = await userRepository.findOne({ where: { email: userData.email } });
|
|
28
|
+
const findUser: User = await UserEntity.findOne({ where: { email: userData.email } });
|
|
33
29
|
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
34
30
|
|
|
35
|
-
const hashedPassword = await
|
|
36
|
-
const createUserData: User = await
|
|
31
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
32
|
+
const createUserData: User = await UserEntity.create({ ...userData, password: hashedPassword }).save();
|
|
37
33
|
|
|
38
34
|
return createUserData;
|
|
39
35
|
}
|
|
@@ -41,25 +37,23 @@ class UserService {
|
|
|
41
37
|
public async updateUser(userId: number, userData: CreateUserDto): Promise<User> {
|
|
42
38
|
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
43
39
|
|
|
44
|
-
const
|
|
45
|
-
const findUser: User = await userRepository.findOne({ where: { id: userId } });
|
|
40
|
+
const findUser: User = await UserEntity.findOne({ where: { id: userId } });
|
|
46
41
|
if (!findUser) throw new HttpException(409, "You're not user");
|
|
47
42
|
|
|
48
|
-
const hashedPassword = await
|
|
49
|
-
await
|
|
43
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
44
|
+
await UserEntity.update(userId, { ...userData, password: hashedPassword });
|
|
50
45
|
|
|
51
|
-
const updateUser: User = await
|
|
46
|
+
const updateUser: User = await UserEntity.findOne({ where: { id: userId } });
|
|
52
47
|
return updateUser;
|
|
53
48
|
}
|
|
54
49
|
|
|
55
50
|
public async deleteUser(userId: number): Promise<User> {
|
|
56
51
|
if (isEmpty(userId)) throw new HttpException(400, "You're not userId");
|
|
57
52
|
|
|
58
|
-
const
|
|
59
|
-
const findUser: User = await userRepository.findOne({ where: { id: userId } });
|
|
53
|
+
const findUser: User = await UserEntity.findOne({ where: { id: userId } });
|
|
60
54
|
if (!findUser) throw new HttpException(409, "You're not user");
|
|
61
55
|
|
|
62
|
-
await
|
|
56
|
+
await UserEntity.delete({ id: userId });
|
|
63
57
|
return findUser;
|
|
64
58
|
}
|
|
65
59
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import config from 'config';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import { existsSync, mkdirSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
4
|
import winston from 'winston';
|
|
5
5
|
import winstonDaily from 'winston-daily-rotate-file';
|
|
6
6
|
|
|
7
7
|
// logs dir
|
|
8
|
-
const logDir: string =
|
|
8
|
+
const logDir: string = join(__dirname, config.get('log.dir'));
|
|
9
9
|
|
|
10
|
-
if (!
|
|
11
|
-
|
|
10
|
+
if (!existsSync(logDir)) {
|
|
11
|
+
mkdirSync(logDir);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// Define log format
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"pretty": true,
|
|
14
14
|
"sourceMap": true,
|
|
15
15
|
"declaration": true,
|
|
16
|
-
"outDir": "
|
|
16
|
+
"outDir": "dist",
|
|
17
17
|
"allowJs": true,
|
|
18
18
|
"noEmit": false,
|
|
19
19
|
"esModuleInterop": true,
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@controllers/*": ["controllers/*"],
|
|
26
26
|
"@databases": ["databases"],
|
|
27
27
|
"@dtos/*": ["dtos/*"],
|
|
28
|
-
"@
|
|
28
|
+
"@entities/*": ["entities/*"],
|
|
29
29
|
"@exceptions/*": ["exceptions/*"],
|
|
30
30
|
"@interfaces/*": ["interfaces/*"],
|
|
31
31
|
"@middlewares/*": ["middlewares/*"],
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"include": ["src/**/*.ts", "src/**/*.json", ".env"],
|
|
38
|
-
"exclude": ["node_modules"]
|
|
38
|
+
"exclude": ["node_modules", "src/http", "src/logs", "src/tests"]
|
|
39
39
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-express-starter",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.3.0",
|
|
4
4
|
"description": "Quick and Easy TypeScript Express Starter",
|
|
5
5
|
"author": "AGUMON <ljlm0402@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
"typeorm",
|
|
15
15
|
"knex",
|
|
16
16
|
"prisma",
|
|
17
|
-
"querybuilder",
|
|
18
17
|
"jest",
|
|
19
18
|
"nodemon",
|
|
20
19
|
"jsonwebtoken",
|
|
@@ -32,7 +31,8 @@
|
|
|
32
31
|
"mysql",
|
|
33
32
|
"mariadb",
|
|
34
33
|
"mongodb",
|
|
35
|
-
"postgresql"
|
|
34
|
+
"postgresql",
|
|
35
|
+
"swc"
|
|
36
36
|
],
|
|
37
37
|
"main": "bin/cli.js",
|
|
38
38
|
"bin": {
|
|
@@ -50,6 +50,13 @@
|
|
|
50
50
|
"ora": "^4.0.3"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">= 10.13.0",
|
|
58
|
+
"npm": ">= 6.11.0"
|
|
59
|
+
},
|
|
53
60
|
"repository": {
|
|
54
61
|
"type": "git",
|
|
55
62
|
"url": "git+https://github.com/ljlm0402/typescript-express-starter.git"
|
|
@@ -58,4 +65,4 @@
|
|
|
58
65
|
"url": "https://github.com/ljlm0402/typescript-express-starter/issues"
|
|
59
66
|
},
|
|
60
67
|
"homepage": "https://github.com/ljlm0402/typescript-express-starter#readme"
|
|
61
|
-
}
|
|
68
|
+
}
|