typescript-express-starter 8.1.3 → 9.1.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 +23 -27
- package/README.md +23 -25
- package/lib/default/Makefile +26 -3
- package/lib/default/package.json +9 -12
- package/lib/graphql/.env.development.local +2 -2
- package/lib/graphql/.env.production.local +2 -2
- package/lib/graphql/.env.test.local +2 -2
- package/lib/graphql/Makefile +26 -3
- package/lib/graphql/package.json +8 -11
- package/lib/graphql/src/app.ts +6 -3
- package/lib/graphql/src/config/index.ts +2 -1
- package/lib/graphql/src/entities/users.entity.ts +1 -1
- package/lib/graphql/src/repositories/auth.repository.ts +1 -1
- package/lib/graphql/src/repositories/users.repository.ts +1 -1
- package/lib/knex/Makefile +26 -3
- package/lib/knex/package.json +23 -26
- package/lib/mikro-orm/.dockerignore +18 -0
- package/lib/mikro-orm/.editorconfig +9 -0
- package/lib/mikro-orm/.env.development.local +18 -0
- package/lib/mikro-orm/.env.production.local +18 -0
- package/lib/mikro-orm/.env.test.local +18 -0
- package/lib/mikro-orm/.eslintignore +1 -0
- package/lib/mikro-orm/.eslintrc +18 -0
- package/lib/mikro-orm/.huskyrc +5 -0
- package/lib/mikro-orm/.lintstagedrc.json +5 -0
- package/lib/mikro-orm/.prettierrc +8 -0
- package/lib/mikro-orm/.swcrc +41 -0
- package/lib/mikro-orm/.vscode/launch.json +35 -0
- package/lib/mikro-orm/.vscode/settings.json +6 -0
- package/lib/mikro-orm/Dockerfile +24 -0
- package/lib/mikro-orm/Makefile +29 -0
- package/lib/mikro-orm/docker-compose.yml +46 -0
- package/lib/mikro-orm/ecosystem.config.js +57 -0
- package/lib/mikro-orm/jest.config.js +12 -0
- package/lib/mikro-orm/nginx.conf +40 -0
- package/lib/mikro-orm/nodemon.json +12 -0
- package/lib/mikro-orm/package.json +77 -0
- package/lib/mikro-orm/src/app.ts +98 -0
- package/lib/mikro-orm/src/config/index.ts +5 -0
- package/lib/mikro-orm/src/controllers/auth.controller.ts +46 -0
- package/lib/mikro-orm/src/controllers/index.controller.ts +13 -0
- package/lib/mikro-orm/src/controllers/users.controller.ts +65 -0
- package/lib/mikro-orm/src/databases/index.ts +19 -0
- package/lib/mikro-orm/src/dtos/users.dto.ts +9 -0
- package/lib/mikro-orm/src/entities/base.entity.ts +16 -0
- package/lib/mikro-orm/src/entities/users.entity.ts +17 -0
- package/lib/mikro-orm/src/exceptions/HttpException.ts +10 -0
- package/lib/mikro-orm/src/http/auth.http +32 -0
- package/lib/mikro-orm/src/http/users.http +34 -0
- package/lib/mikro-orm/src/interfaces/auth.interface.ts +15 -0
- package/lib/mikro-orm/src/interfaces/routes.interface.ts +6 -0
- package/lib/mikro-orm/src/interfaces/users.interface.ts +5 -0
- package/lib/mikro-orm/src/middlewares/auth.middleware.ts +32 -0
- package/lib/mikro-orm/src/middlewares/error.middleware.ts +17 -0
- package/lib/mikro-orm/src/middlewares/validation.middleware.ts +25 -0
- package/lib/mikro-orm/src/routes/auth.route.ts +24 -0
- package/lib/mikro-orm/src/routes/index.route.ts +19 -0
- package/lib/mikro-orm/src/routes/users.route.ts +25 -0
- package/lib/mikro-orm/src/server.ts +11 -0
- package/lib/mikro-orm/src/services/auth.service.ts +63 -0
- package/lib/mikro-orm/src/services/users.service.ts +68 -0
- package/lib/mikro-orm/src/tests/auth.test.ts +66 -0
- package/lib/mikro-orm/src/tests/index.test.ts +18 -0
- package/lib/mikro-orm/src/tests/users.test.ts +70 -0
- package/lib/mikro-orm/src/utils/logger.ts +65 -0
- package/lib/mikro-orm/src/utils/util.ts +19 -0
- package/lib/mikro-orm/src/utils/validateEnv.ts +10 -0
- package/lib/mikro-orm/swagger.yaml +122 -0
- package/lib/mikro-orm/tsconfig.json +41 -0
- package/lib/mongoose/Makefile +26 -3
- package/lib/mongoose/package.json +7 -10
- package/lib/prisma/Makefile +26 -3
- package/lib/prisma/package.json +7 -10
- package/lib/routing-controllers/Makefile +26 -3
- package/lib/routing-controllers/package.json +8 -10
- package/lib/sequelize/Makefile +26 -3
- package/lib/sequelize/package.json +7 -10
- package/lib/starter.js +30 -8
- package/lib/typegoose/Makefile +26 -3
- package/lib/typegoose/package.json +6 -9
- package/lib/typeorm/Makefile +26 -3
- package/lib/typeorm/package.json +6 -9
- package/package.json +6 -15
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
3
|
+
import { User } from '@interfaces/users.interface';
|
|
4
|
+
import userService from '@services/users.service';
|
|
5
|
+
|
|
6
|
+
class UsersController {
|
|
7
|
+
public userService = new userService();
|
|
8
|
+
|
|
9
|
+
public getUsers = async (req: Request, res: Response, next: NextFunction) => {
|
|
10
|
+
try {
|
|
11
|
+
const findAllUsersData: User[] = await this.userService.findAllUser();
|
|
12
|
+
|
|
13
|
+
res.status(200).json({ data: findAllUsersData, message: 'findAll' });
|
|
14
|
+
} catch (error) {
|
|
15
|
+
next(error);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
public getUserById = async (req: Request, res: Response, next: NextFunction) => {
|
|
20
|
+
try {
|
|
21
|
+
const userId: string = req.params.id;
|
|
22
|
+
const findOneUserData: User = await this.userService.findUserById(userId);
|
|
23
|
+
|
|
24
|
+
res.status(200).json({ data: findOneUserData, message: 'findOne' });
|
|
25
|
+
} catch (error) {
|
|
26
|
+
next(error);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
public createUser = async (req: Request, res: Response, next: NextFunction) => {
|
|
31
|
+
try {
|
|
32
|
+
const userData: CreateUserDto = req.body;
|
|
33
|
+
const createUserData: User = await this.userService.createUser(userData);
|
|
34
|
+
|
|
35
|
+
res.status(201).json({ data: createUserData, message: 'created' });
|
|
36
|
+
} catch (error) {
|
|
37
|
+
next(error);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
public updateUser = async (req: Request, res: Response, next: NextFunction) => {
|
|
42
|
+
try {
|
|
43
|
+
const userId: string = req.params.id;
|
|
44
|
+
const userData: CreateUserDto = req.body;
|
|
45
|
+
const updateUserData: User = await this.userService.updateUser(userId, userData);
|
|
46
|
+
|
|
47
|
+
res.status(200).json({ data: updateUserData, message: 'updated' });
|
|
48
|
+
} catch (error) {
|
|
49
|
+
next(error);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
public deleteUser = async (req: Request, res: Response, next: NextFunction) => {
|
|
54
|
+
try {
|
|
55
|
+
const userId: string = req.params.id;
|
|
56
|
+
const deleteUserData = await this.userService.deleteUser(userId);
|
|
57
|
+
|
|
58
|
+
res.status(200).json({ data: deleteUserData, message: 'deleted' });
|
|
59
|
+
} catch (error) {
|
|
60
|
+
next(error);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default UsersController;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EntityManager, EntityRepository, MikroORM, Options } from '@mikro-orm/core';
|
|
2
|
+
import { MongoHighlighter } from '@mikro-orm/mongo-highlighter';
|
|
3
|
+
import { NODE_ENV, DB_HOST, DB_PORT, DB_DATABASE } from '@config';
|
|
4
|
+
import { BaseEntity } from '@entities/base.entity';
|
|
5
|
+
import { UserEntity } from '@entities/users.entity';
|
|
6
|
+
|
|
7
|
+
export const dbOptions: Options = {
|
|
8
|
+
type: 'mongo',
|
|
9
|
+
clientUrl: `mongodb://${DB_HOST}:${DB_PORT}/${DB_DATABASE}`,
|
|
10
|
+
entities: [BaseEntity, UserEntity],
|
|
11
|
+
highlighter: new MongoHighlighter(),
|
|
12
|
+
debug: NODE_ENV === 'development' ? true : false,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const DI = {} as {
|
|
16
|
+
orm: MikroORM;
|
|
17
|
+
em: EntityManager;
|
|
18
|
+
userRepository: EntityRepository<UserEntity>;
|
|
19
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { PrimaryKey, Property, SerializedPrimaryKey } from '@mikro-orm/core';
|
|
2
|
+
import { ObjectId } from '@mikro-orm/mongodb';
|
|
3
|
+
|
|
4
|
+
export abstract class BaseEntity {
|
|
5
|
+
@PrimaryKey()
|
|
6
|
+
_id!: ObjectId;
|
|
7
|
+
|
|
8
|
+
@SerializedPrimaryKey({ type: 'text' })
|
|
9
|
+
id!: string;
|
|
10
|
+
|
|
11
|
+
@Property({ type: 'date' })
|
|
12
|
+
createdAt = new Date();
|
|
13
|
+
|
|
14
|
+
@Property({ type: 'date', onUpdate: () => new Date() })
|
|
15
|
+
updatedAt = new Date();
|
|
16
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Entity, Property } from '@mikro-orm/core';
|
|
2
|
+
import { BaseEntity } from '@entities/base.entity';
|
|
3
|
+
|
|
4
|
+
@Entity()
|
|
5
|
+
export class UserEntity extends BaseEntity {
|
|
6
|
+
constructor(email: string, password: string) {
|
|
7
|
+
super();
|
|
8
|
+
this.email = email;
|
|
9
|
+
this.password = password;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@Property({ type: 'text' })
|
|
13
|
+
email!: string;
|
|
14
|
+
|
|
15
|
+
@Property({ type: 'text' })
|
|
16
|
+
password!: string;
|
|
17
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# baseURL
|
|
2
|
+
@baseURL = http://localhost:3000
|
|
3
|
+
|
|
4
|
+
###
|
|
5
|
+
# User Signup
|
|
6
|
+
POST {{ baseURL }}/signup
|
|
7
|
+
Content-Type: application/json
|
|
8
|
+
|
|
9
|
+
{
|
|
10
|
+
"email": "example@email.com",
|
|
11
|
+
"password": "password"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
###
|
|
15
|
+
# User Login
|
|
16
|
+
POST {{ baseURL }}/login
|
|
17
|
+
Content-Type: application/json
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
"email": "example@email.com",
|
|
21
|
+
"password": "password"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
###
|
|
25
|
+
# User Logout
|
|
26
|
+
POST {{ baseURL }}/logout
|
|
27
|
+
Content-Type: application/json
|
|
28
|
+
|
|
29
|
+
{
|
|
30
|
+
"email": "example@email.com",
|
|
31
|
+
"password": "password"
|
|
32
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# baseURL
|
|
2
|
+
@baseURL = http://localhost:3000
|
|
3
|
+
|
|
4
|
+
###
|
|
5
|
+
# Find All Users
|
|
6
|
+
GET {{ baseURL }}/users
|
|
7
|
+
|
|
8
|
+
###
|
|
9
|
+
# Find User By Id
|
|
10
|
+
GET {{ baseURL }}/users/6257233cbfa5c2b8e249dbe7
|
|
11
|
+
|
|
12
|
+
###
|
|
13
|
+
# Create User
|
|
14
|
+
POST {{ baseURL }}/users
|
|
15
|
+
Content-Type: application/json
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
"email": "example@email.com",
|
|
19
|
+
"password": "password"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
###
|
|
23
|
+
# Modify User By Id
|
|
24
|
+
PUT {{ baseURL }}/users/6257233cbfa5c2b8e249dbe7
|
|
25
|
+
Content-Type: application/json
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
"email": "example@email.com",
|
|
29
|
+
"password": "password"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
###
|
|
33
|
+
# Delete User By Id
|
|
34
|
+
DELETE {{ baseURL }}/users/6257233cbfa5c2b8e249dbe7
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
import { User } from '@interfaces/users.interface';
|
|
3
|
+
|
|
4
|
+
export interface DataStoredInToken {
|
|
5
|
+
_id: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface TokenData {
|
|
9
|
+
token: string;
|
|
10
|
+
expiresIn: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface RequestWithUser extends Request {
|
|
14
|
+
user: User;
|
|
15
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { NextFunction, Response } from 'express';
|
|
2
|
+
import { verify } from 'jsonwebtoken';
|
|
3
|
+
import { SECRET_KEY } from '@config';
|
|
4
|
+
import { DI } from '@databases';
|
|
5
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
6
|
+
import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
|
|
7
|
+
|
|
8
|
+
const authMiddleware = async (req: RequestWithUser, res: Response, next: NextFunction) => {
|
|
9
|
+
try {
|
|
10
|
+
const Authorization = req.cookies['Authorization'] || (req.header('Authorization') ? req.header('Authorization').split('Bearer ')[1] : null);
|
|
11
|
+
|
|
12
|
+
if (Authorization) {
|
|
13
|
+
const secretKey: string = SECRET_KEY;
|
|
14
|
+
const verificationResponse = verify(Authorization, secretKey) as DataStoredInToken;
|
|
15
|
+
const userId = verificationResponse._id;
|
|
16
|
+
const findUser = await DI.userRepository.findOne(userId);
|
|
17
|
+
|
|
18
|
+
if (findUser) {
|
|
19
|
+
req.user = findUser;
|
|
20
|
+
next();
|
|
21
|
+
} else {
|
|
22
|
+
next(new HttpException(401, 'Wrong authentication token'));
|
|
23
|
+
}
|
|
24
|
+
} else {
|
|
25
|
+
next(new HttpException(404, 'Authentication token missing'));
|
|
26
|
+
}
|
|
27
|
+
} catch (error) {
|
|
28
|
+
next(new HttpException(401, 'Wrong authentication token'));
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export default authMiddleware;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
3
|
+
import { logger } from '@utils/logger';
|
|
4
|
+
|
|
5
|
+
const errorMiddleware = (error: HttpException, req: Request, res: Response, next: NextFunction) => {
|
|
6
|
+
try {
|
|
7
|
+
const status: number = error.status || 500;
|
|
8
|
+
const message: string = error.message || 'Something went wrong';
|
|
9
|
+
|
|
10
|
+
logger.error(`[${req.method}] ${req.path} >> StatusCode:: ${status}, Message:: ${message}`);
|
|
11
|
+
res.status(status).json({ message });
|
|
12
|
+
} catch (error) {
|
|
13
|
+
next(error);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default errorMiddleware;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { plainToClass } from 'class-transformer';
|
|
2
|
+
import { validate, ValidationError } from 'class-validator';
|
|
3
|
+
import { RequestHandler } from 'express';
|
|
4
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
5
|
+
|
|
6
|
+
const validationMiddleware = (
|
|
7
|
+
type: any,
|
|
8
|
+
value: string | 'body' | 'query' | 'params' = 'body',
|
|
9
|
+
skipMissingProperties = false,
|
|
10
|
+
whitelist = true,
|
|
11
|
+
forbidNonWhitelisted = true,
|
|
12
|
+
): RequestHandler => {
|
|
13
|
+
return (req, res, next) => {
|
|
14
|
+
validate(plainToClass(type, req[value]), { skipMissingProperties, whitelist, forbidNonWhitelisted }).then((errors: ValidationError[]) => {
|
|
15
|
+
if (errors.length > 0) {
|
|
16
|
+
const message = errors.map((error: ValidationError) => Object.values(error.constraints)).join(', ');
|
|
17
|
+
next(new HttpException(400, message));
|
|
18
|
+
} else {
|
|
19
|
+
next();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default validationMiddleware;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import AuthController from '@controllers/auth.controller';
|
|
3
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
5
|
+
import authMiddleware from '@middlewares/auth.middleware';
|
|
6
|
+
import validationMiddleware from '@middlewares/validation.middleware';
|
|
7
|
+
|
|
8
|
+
class AuthRoute implements Routes {
|
|
9
|
+
public path = '/';
|
|
10
|
+
public router = Router();
|
|
11
|
+
public authController = new AuthController();
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
this.initializeRoutes();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private initializeRoutes() {
|
|
18
|
+
this.router.post(`${this.path}signup`, validationMiddleware(CreateUserDto, 'body'), this.authController.signUp);
|
|
19
|
+
this.router.post(`${this.path}login`, validationMiddleware(CreateUserDto, 'body'), this.authController.logIn);
|
|
20
|
+
this.router.post(`${this.path}logout`, authMiddleware, this.authController.logOut);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default AuthRoute;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import IndexController from '@controllers/index.controller';
|
|
3
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
4
|
+
|
|
5
|
+
class IndexRoute implements Routes {
|
|
6
|
+
public path = '/';
|
|
7
|
+
public router = Router();
|
|
8
|
+
public indexController = new IndexController();
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
this.initializeRoutes();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
private initializeRoutes() {
|
|
15
|
+
this.router.get(`${this.path}`, this.indexController.index);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default IndexRoute;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import UsersController from '@controllers/users.controller';
|
|
3
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
5
|
+
import validationMiddleware from '@middlewares/validation.middleware';
|
|
6
|
+
|
|
7
|
+
class UsersRoute implements Routes {
|
|
8
|
+
public path = '/users';
|
|
9
|
+
public router = Router();
|
|
10
|
+
public usersController = new UsersController();
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.initializeRoutes();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private initializeRoutes() {
|
|
17
|
+
this.router.get(`${this.path}`, this.usersController.getUsers);
|
|
18
|
+
this.router.get(`${this.path}/:id`, this.usersController.getUserById);
|
|
19
|
+
this.router.post(`${this.path}`, validationMiddleware(CreateUserDto, 'body'), this.usersController.createUser);
|
|
20
|
+
this.router.put(`${this.path}/:id`, validationMiddleware(CreateUserDto, 'body', true), this.usersController.updateUser);
|
|
21
|
+
this.router.delete(`${this.path}/:id`, this.usersController.deleteUser);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default UsersRoute;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import App from '@/app';
|
|
2
|
+
import AuthRoute from '@routes/auth.route';
|
|
3
|
+
import IndexRoute from '@routes/index.route';
|
|
4
|
+
import UsersRoute from '@routes/users.route';
|
|
5
|
+
import validateEnv from '@utils/validateEnv';
|
|
6
|
+
|
|
7
|
+
validateEnv();
|
|
8
|
+
|
|
9
|
+
const app = new App([new IndexRoute(), new UsersRoute(), new AuthRoute()]);
|
|
10
|
+
|
|
11
|
+
app.listen();
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { hash, compare } from 'bcrypt';
|
|
2
|
+
import { sign } from 'jsonwebtoken';
|
|
3
|
+
import { SECRET_KEY } from '@config';
|
|
4
|
+
import { DI } from '@databases';
|
|
5
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
6
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
7
|
+
import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
|
|
8
|
+
import { User } from '@interfaces/users.interface';
|
|
9
|
+
import { isEmpty } from '@utils/util';
|
|
10
|
+
|
|
11
|
+
class AuthService {
|
|
12
|
+
public async signup(userData: CreateUserDto): Promise<User> {
|
|
13
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
14
|
+
|
|
15
|
+
const findUser: User = await DI.userRepository.findOne({ email: userData.email });
|
|
16
|
+
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
17
|
+
|
|
18
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
19
|
+
const createUserData: User = DI.userRepository.create({ ...userData, password: hashedPassword });
|
|
20
|
+
|
|
21
|
+
await DI.em.persistAndFlush(createUserData);
|
|
22
|
+
|
|
23
|
+
return createUserData;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async login(userData: CreateUserDto): Promise<{ cookie: string; findUser: User }> {
|
|
27
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
28
|
+
|
|
29
|
+
const findUser: User = await DI.userRepository.findOne({ email: userData.email });
|
|
30
|
+
if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
|
|
31
|
+
|
|
32
|
+
const isPasswordMatching: boolean = await compare(userData.password, findUser.password);
|
|
33
|
+
if (!isPasswordMatching) throw new HttpException(409, "You're password not matching");
|
|
34
|
+
|
|
35
|
+
const tokenData = this.createToken(findUser);
|
|
36
|
+
const cookie = this.createCookie(tokenData);
|
|
37
|
+
|
|
38
|
+
return { cookie, findUser };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async logout(userData: User): Promise<User> {
|
|
42
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
43
|
+
|
|
44
|
+
const findUser: User = await DI.userRepository.findOne({ email: userData.email, password: userData.password });
|
|
45
|
+
if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
|
|
46
|
+
|
|
47
|
+
return findUser;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public createToken(user: User): TokenData {
|
|
51
|
+
const dataStoredInToken: DataStoredInToken = { _id: user.id };
|
|
52
|
+
const secretKey: string = SECRET_KEY;
|
|
53
|
+
const expiresIn: number = 60 * 60;
|
|
54
|
+
|
|
55
|
+
return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public createCookie(tokenData: TokenData): string {
|
|
59
|
+
return `Authorization=${tokenData.token}; HttpOnly; Max-Age=${tokenData.expiresIn};`;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default AuthService;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { hash } from 'bcrypt';
|
|
2
|
+
import { wrap } from '@mikro-orm/core';
|
|
3
|
+
import { DI } from '@databases';
|
|
4
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
5
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
6
|
+
import { User } from '@interfaces/users.interface';
|
|
7
|
+
import { isEmpty } from '@utils/util';
|
|
8
|
+
|
|
9
|
+
class UserService {
|
|
10
|
+
public async findAllUser(): Promise<User[]> {
|
|
11
|
+
const users: User[] = await DI.userRepository.findAll();
|
|
12
|
+
return users;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async findUserById(userId: string): Promise<User> {
|
|
16
|
+
if (isEmpty(userId)) throw new HttpException(400, "You're not userId");
|
|
17
|
+
|
|
18
|
+
const findUser: User = await DI.userRepository.findOne(userId);
|
|
19
|
+
if (!findUser) throw new HttpException(409, "You're not user");
|
|
20
|
+
|
|
21
|
+
return findUser;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async createUser(userData: CreateUserDto): Promise<User> {
|
|
25
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
26
|
+
|
|
27
|
+
const findUser: User = await DI.userRepository.findOne({ email: userData.email });
|
|
28
|
+
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
29
|
+
|
|
30
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
31
|
+
const createUserData: User = DI.userRepository.create({ ...userData, password: hashedPassword });
|
|
32
|
+
|
|
33
|
+
await DI.em.persistAndFlush(createUserData);
|
|
34
|
+
|
|
35
|
+
return createUserData;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public async updateUser(userId: string, userData: CreateUserDto): Promise<User> {
|
|
39
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
40
|
+
|
|
41
|
+
if (userData.email) {
|
|
42
|
+
const findUser: User = await DI.userRepository.findOne({ email: userData.email });
|
|
43
|
+
if (findUser && findUser.id !== userId) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (userData.password) {
|
|
47
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
48
|
+
userData = { ...userData, password: hashedPassword };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const updateUserById: User = await DI.userRepository.findOne(userId);
|
|
52
|
+
wrap(updateUserById).assign(userData);
|
|
53
|
+
if (!updateUserById) throw new HttpException(409, "You're not user");
|
|
54
|
+
|
|
55
|
+
return updateUserById;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public async deleteUser(userId: string): Promise<User> {
|
|
59
|
+
const findUser = await DI.userRepository.findOne(userId);
|
|
60
|
+
|
|
61
|
+
if (!findUser) throw new HttpException(409, "You're not user");
|
|
62
|
+
await DI.userRepository.removeAndFlush(findUser);
|
|
63
|
+
|
|
64
|
+
return findUser;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default UserService;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import request from 'supertest';
|
|
2
|
+
import App from '../app';
|
|
3
|
+
import { DI } from '../databases';
|
|
4
|
+
import { CreateUserDto } from '../dtos/users.dto';
|
|
5
|
+
import AuthRoute from '../routes/auth.route';
|
|
6
|
+
|
|
7
|
+
const authRoute = new AuthRoute();
|
|
8
|
+
|
|
9
|
+
const app = new App([authRoute]);
|
|
10
|
+
|
|
11
|
+
let cookies: string;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
** MikroORM Seeding
|
|
15
|
+
** https://mikro-orm.io/docs/seeding#use-in-tests
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
beforeAll(async () => {
|
|
19
|
+
await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
|
|
20
|
+
|
|
21
|
+
await DI.orm.getSchemaGenerator().refreshDatabase();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterAll(async () => {
|
|
25
|
+
await DI.orm.close();
|
|
26
|
+
await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('Testing Auth', () => {
|
|
30
|
+
describe('[POST] /signup', () => {
|
|
31
|
+
it('response should have the Create userData', async () => {
|
|
32
|
+
const userData: CreateUserDto = {
|
|
33
|
+
email: 'test@email.com',
|
|
34
|
+
password: 'q1w2e3r4!',
|
|
35
|
+
};
|
|
36
|
+
return request(app.getServer()).post(`${authRoute.path}signup`).send(userData);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe('[POST] /login', () => {
|
|
40
|
+
it('response should have the Set-Cookie header with the Authorization token', async () => {
|
|
41
|
+
const userData: CreateUserDto = {
|
|
42
|
+
email: 'test@email.com',
|
|
43
|
+
password: 'q1w2e3r4!',
|
|
44
|
+
};
|
|
45
|
+
const res = await request(app.getServer())
|
|
46
|
+
.post(`${authRoute.path}login`)
|
|
47
|
+
.send(userData)
|
|
48
|
+
.expect('Set-Cookie', /^Authorization=.+/);
|
|
49
|
+
|
|
50
|
+
cookies = res.headers['set-cookie'];
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe('[POST] /logout', () => {
|
|
54
|
+
it('logout Set-Cookie Authorization=; Max-age=0', async () => {
|
|
55
|
+
const userData: CreateUserDto = {
|
|
56
|
+
email: 'test@email.com',
|
|
57
|
+
password: 'q1w2e3r4!',
|
|
58
|
+
};
|
|
59
|
+
return request(app.getServer())
|
|
60
|
+
.post(`${authRoute.path}logout`)
|
|
61
|
+
.send(userData)
|
|
62
|
+
.set('Cookie', cookies)
|
|
63
|
+
.expect('Set-Cookie', /^Authorization=\; Max-age=0/);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import request from 'supertest';
|
|
2
|
+
import App from '../app';
|
|
3
|
+
import IndexRoute from '../routes/index.route';
|
|
4
|
+
|
|
5
|
+
afterAll(async () => {
|
|
6
|
+
await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe('Testing Index', () => {
|
|
10
|
+
describe('[GET] /', () => {
|
|
11
|
+
it('response statusCode 200', () => {
|
|
12
|
+
const indexRoute = new IndexRoute();
|
|
13
|
+
const app = new App([indexRoute]);
|
|
14
|
+
|
|
15
|
+
return request(app.getServer()).get(`${indexRoute.path}`).expect(200);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|