typescript-express-starter 7.0.0 → 8.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.
Files changed (55) hide show
  1. package/README.kr.md +8 -4
  2. package/README.md +8 -4
  3. package/lib/typegoose/.dockerignore +18 -0
  4. package/lib/typegoose/.editorconfig +9 -0
  5. package/lib/typegoose/.env.development.local +18 -0
  6. package/lib/typegoose/.env.production.local +18 -0
  7. package/lib/typegoose/.env.test.local +18 -0
  8. package/lib/typegoose/.eslintignore +1 -0
  9. package/lib/typegoose/.eslintrc +18 -0
  10. package/lib/typegoose/.huskyrc +5 -0
  11. package/lib/typegoose/.lintstagedrc.json +5 -0
  12. package/lib/typegoose/.prettierrc +8 -0
  13. package/lib/typegoose/.swcrc +40 -0
  14. package/lib/typegoose/.vscode/launch.json +35 -0
  15. package/lib/typegoose/.vscode/settings.json +6 -0
  16. package/lib/typegoose/Dockerfile +24 -0
  17. package/lib/typegoose/Makefile +6 -0
  18. package/lib/typegoose/docker-compose.yml +46 -0
  19. package/lib/typegoose/ecosystem.config.js +57 -0
  20. package/lib/typegoose/jest.config.js +12 -0
  21. package/lib/typegoose/nginx.conf +40 -0
  22. package/lib/typegoose/nodemon.json +12 -0
  23. package/lib/typegoose/package.json +80 -0
  24. package/lib/typegoose/src/app.ts +93 -0
  25. package/lib/typegoose/src/config/index.ts +5 -0
  26. package/lib/typegoose/src/controllers/auth.controller.ts +46 -0
  27. package/lib/typegoose/src/controllers/index.controller.ts +13 -0
  28. package/lib/typegoose/src/controllers/users.controller.ts +65 -0
  29. package/lib/typegoose/src/databases/index.ts +3 -0
  30. package/lib/typegoose/src/dtos/users.dto.ts +9 -0
  31. package/lib/typegoose/src/exceptions/HttpException.ts +10 -0
  32. package/lib/typegoose/src/http/auth.http +32 -0
  33. package/lib/typegoose/src/http/users.http +34 -0
  34. package/lib/typegoose/src/interfaces/auth.interface.ts +15 -0
  35. package/lib/typegoose/src/interfaces/routes.interface.ts +6 -0
  36. package/lib/typegoose/src/interfaces/users.interface.ts +5 -0
  37. package/lib/typegoose/src/middlewares/auth.middleware.ts +32 -0
  38. package/lib/typegoose/src/middlewares/error.middleware.ts +17 -0
  39. package/lib/typegoose/src/middlewares/validation.middleware.ts +25 -0
  40. package/lib/typegoose/src/models/users.model.ts +18 -0
  41. package/lib/typegoose/src/routes/auth.route.ts +24 -0
  42. package/lib/typegoose/src/routes/index.route.ts +19 -0
  43. package/lib/typegoose/src/routes/users.route.ts +25 -0
  44. package/lib/typegoose/src/server.ts +11 -0
  45. package/lib/typegoose/src/services/auth.service.ts +61 -0
  46. package/lib/typegoose/src/services/users.service.ts +64 -0
  47. package/lib/typegoose/src/tests/auth.test.ts +83 -0
  48. package/lib/typegoose/src/tests/index.test.ts +18 -0
  49. package/lib/typegoose/src/tests/users.test.ts +133 -0
  50. package/lib/typegoose/src/utils/logger.ts +65 -0
  51. package/lib/typegoose/src/utils/util.ts +19 -0
  52. package/lib/typegoose/src/utils/validateEnv.ts +10 -0
  53. package/lib/typegoose/swagger.yaml +122 -0
  54. package/lib/typegoose/tsconfig.json +40 -0
  55. package/package.json +2 -1
@@ -0,0 +1,93 @@
1
+ import compression from 'compression';
2
+ import cookieParser from 'cookie-parser';
3
+ import cors from 'cors';
4
+ import express from 'express';
5
+ import helmet from 'helmet';
6
+ import hpp from 'hpp';
7
+ import morgan from 'morgan';
8
+ import { connect, set } from 'mongoose';
9
+ import swaggerJSDoc from 'swagger-jsdoc';
10
+ import swaggerUi from 'swagger-ui-express';
11
+ import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
12
+ import { dbConnection } from '@databases';
13
+ import { Routes } from '@interfaces/routes.interface';
14
+ import errorMiddleware from '@middlewares/error.middleware';
15
+ import { logger, stream } from '@utils/logger';
16
+
17
+ class App {
18
+ public app: express.Application;
19
+ public env: string;
20
+ public port: string | number;
21
+
22
+ constructor(routes: Routes[]) {
23
+ this.app = express();
24
+ this.env = NODE_ENV || 'development';
25
+ this.port = PORT || 3000;
26
+
27
+ this.connectToDatabase();
28
+ this.initializeMiddlewares();
29
+ this.initializeRoutes(routes);
30
+ this.initializeSwagger();
31
+ this.initializeErrorHandling();
32
+ }
33
+
34
+ public listen() {
35
+ this.app.listen(this.port, () => {
36
+ logger.info(`=================================`);
37
+ logger.info(`======= ENV: ${this.env} =======`);
38
+ logger.info(`🚀 App listening on the port ${this.port}`);
39
+ logger.info(`=================================`);
40
+ });
41
+ }
42
+
43
+ public getServer() {
44
+ return this.app;
45
+ }
46
+
47
+ private connectToDatabase() {
48
+ if (this.env !== 'production') {
49
+ set('debug', true);
50
+ }
51
+
52
+ connect(dbConnection);
53
+ }
54
+
55
+ private initializeMiddlewares() {
56
+ this.app.use(morgan(LOG_FORMAT, { stream }));
57
+ this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
58
+ this.app.use(hpp());
59
+ this.app.use(helmet());
60
+ this.app.use(compression());
61
+ this.app.use(express.json());
62
+ this.app.use(express.urlencoded({ extended: true }));
63
+ this.app.use(cookieParser());
64
+ }
65
+
66
+ private initializeRoutes(routes: Routes[]) {
67
+ routes.forEach(route => {
68
+ this.app.use('/', route.router);
69
+ });
70
+ }
71
+
72
+ private initializeSwagger() {
73
+ const options = {
74
+ swaggerDefinition: {
75
+ info: {
76
+ title: 'REST API',
77
+ version: '1.0.0',
78
+ description: 'Example docs',
79
+ },
80
+ },
81
+ apis: ['swagger.yaml'],
82
+ };
83
+
84
+ const specs = swaggerJSDoc(options);
85
+ this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
86
+ }
87
+
88
+ private initializeErrorHandling() {
89
+ this.app.use(errorMiddleware);
90
+ }
91
+ }
92
+
93
+ export default App;
@@ -0,0 +1,5 @@
1
+ import { config } from 'dotenv';
2
+ config({ path: `.env.${process.env.NODE_ENV || 'development'}.local` });
3
+
4
+ export const CREDENTIALS = process.env.CREDENTIALS === 'true';
5
+ export const { NODE_ENV, PORT, DB_HOST, DB_PORT, DB_DATABASE, SECRET_KEY, LOG_FORMAT, LOG_DIR, ORIGIN } = process.env;
@@ -0,0 +1,46 @@
1
+ import { NextFunction, Request, Response } from 'express';
2
+ import { CreateUserDto } from '@dtos/users.dto';
3
+ import { RequestWithUser } from '@interfaces/auth.interface';
4
+ import { User } from '@interfaces/users.interface';
5
+ import AuthService from '@services/auth.service';
6
+
7
+ class AuthController {
8
+ public authService = new AuthService();
9
+
10
+ public signUp = async (req: Request, res: Response, next: NextFunction) => {
11
+ try {
12
+ const userData: CreateUserDto = req.body;
13
+ const signUpUserData: User = await this.authService.signup(userData);
14
+
15
+ res.status(201).json({ data: signUpUserData, message: 'signup' });
16
+ } catch (error) {
17
+ next(error);
18
+ }
19
+ };
20
+
21
+ public logIn = async (req: Request, res: Response, next: NextFunction) => {
22
+ try {
23
+ const userData: CreateUserDto = req.body;
24
+ const { cookie, findUser } = await this.authService.login(userData);
25
+
26
+ res.setHeader('Set-Cookie', [cookie]);
27
+ res.status(200).json({ data: findUser, message: 'login' });
28
+ } catch (error) {
29
+ next(error);
30
+ }
31
+ };
32
+
33
+ public logOut = async (req: RequestWithUser, res: Response, next: NextFunction) => {
34
+ try {
35
+ const userData: User = req.user;
36
+ const logOutUserData: User = await this.authService.logout(userData);
37
+
38
+ res.setHeader('Set-Cookie', ['Authorization=; Max-age=0']);
39
+ res.status(200).json({ data: logOutUserData, message: 'logout' });
40
+ } catch (error) {
41
+ next(error);
42
+ }
43
+ };
44
+ }
45
+
46
+ export default AuthController;
@@ -0,0 +1,13 @@
1
+ import { NextFunction, Request, Response } from 'express';
2
+
3
+ class IndexController {
4
+ public index = (req: Request, res: Response, next: NextFunction) => {
5
+ try {
6
+ res.sendStatus(200);
7
+ } catch (error) {
8
+ next(error);
9
+ }
10
+ };
11
+ }
12
+
13
+ export default IndexController;
@@ -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: User = 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,3 @@
1
+ import { DB_HOST, DB_PORT, DB_DATABASE } from '@config';
2
+
3
+ export const dbConnection = `mongodb://${DB_HOST}:${DB_PORT}/${DB_DATABASE}`;
@@ -0,0 +1,9 @@
1
+ import { IsEmail, IsString } from 'class-validator';
2
+
3
+ export class CreateUserDto {
4
+ @IsEmail()
5
+ public email: string;
6
+
7
+ @IsString()
8
+ public password: string;
9
+ }
@@ -0,0 +1,10 @@
1
+ export class HttpException extends Error {
2
+ public status: number;
3
+ public message: string;
4
+
5
+ constructor(status: number, message: string) {
6
+ super(message);
7
+ this.status = status;
8
+ this.message = message;
9
+ }
10
+ }
@@ -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/1
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/1
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/1
@@ -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,6 @@
1
+ import { Router } from 'express';
2
+
3
+ export interface Routes {
4
+ path?: string;
5
+ router: Router;
6
+ }
@@ -0,0 +1,5 @@
1
+ export interface User {
2
+ _id: string;
3
+ email: string;
4
+ password: string;
5
+ }
@@ -0,0 +1,32 @@
1
+ import { NextFunction, Response } from 'express';
2
+ import { verify } from 'jsonwebtoken';
3
+ import { SECRET_KEY } from '@config';
4
+ import { HttpException } from '@exceptions/HttpException';
5
+ import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
6
+ import userModel from '@models/users.model';
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 = (await verify(Authorization, secretKey)) as DataStoredInToken;
15
+ const userId = verificationResponse._id;
16
+ const findUser = await userModel.findById(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,18 @@
1
+ import { prop, getModelForClass, modelOptions } from '@typegoose/typegoose';
2
+
3
+ @modelOptions({ schemaOptions: { collection: 'users', timestamps: true } })
4
+ class User {
5
+ @prop({ type: String, required: true, unique: true })
6
+ public email: string;
7
+
8
+ @prop({ type: String, required: true })
9
+ public password: string;
10
+
11
+ public createdAt?: Date;
12
+
13
+ public updatedAt?: Date;
14
+ }
15
+
16
+ const UserModel = getModelForClass(User);
17
+
18
+ export default UserModel;
@@ -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,61 @@
1
+ import { hash, compare } from 'bcrypt';
2
+ import { sign } from 'jsonwebtoken';
3
+ import { SECRET_KEY } from '@config';
4
+ import { CreateUserDto } from '@dtos/users.dto';
5
+ import { HttpException } from '@exceptions/HttpException';
6
+ import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
7
+ import { User } from '@interfaces/users.interface';
8
+ import userModel from '@models/users.model';
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 userModel.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 = await userModel.create({ ...userData, password: hashedPassword });
20
+
21
+ return createUserData;
22
+ }
23
+
24
+ public async login(userData: CreateUserDto): Promise<{ cookie: string; findUser: User }> {
25
+ if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
26
+
27
+ const findUser: User = await userModel.findOne({ email: userData.email });
28
+ if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
29
+
30
+ const isPasswordMatching: boolean = await compare(userData.password, findUser.password);
31
+ if (!isPasswordMatching) throw new HttpException(409, "You're password not matching");
32
+
33
+ const tokenData = this.createToken(findUser);
34
+ const cookie = this.createCookie(tokenData);
35
+
36
+ return { cookie, findUser };
37
+ }
38
+
39
+ public async logout(userData: User): Promise<User> {
40
+ if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
41
+
42
+ const findUser: User = await userModel.findOne({ email: userData.email, password: userData.password });
43
+ if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
44
+
45
+ return findUser;
46
+ }
47
+
48
+ public createToken(user: User): TokenData {
49
+ const dataStoredInToken: DataStoredInToken = { _id: user._id };
50
+ const secretKey: string = SECRET_KEY;
51
+ const expiresIn: number = 60 * 60;
52
+
53
+ return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
54
+ }
55
+
56
+ public createCookie(tokenData: TokenData): string {
57
+ return `Authorization=${tokenData.token}; HttpOnly; Max-Age=${tokenData.expiresIn};`;
58
+ }
59
+ }
60
+
61
+ export default AuthService;
@@ -0,0 +1,64 @@
1
+ import { hash } from 'bcrypt';
2
+ import { CreateUserDto } from '@dtos/users.dto';
3
+ import { HttpException } from '@exceptions/HttpException';
4
+ import { User } from '@interfaces/users.interface';
5
+ import userModel from '@models/users.model';
6
+ import { isEmpty } from '@utils/util';
7
+
8
+ class UserService {
9
+ // public users = userModel;
10
+
11
+ public async findAllUser(): Promise<User[]> {
12
+ const users: User[] = await userModel.find();
13
+ return users;
14
+ }
15
+
16
+ public async findUserById(userId: string): Promise<User> {
17
+ if (isEmpty(userId)) throw new HttpException(400, "You're not userId");
18
+
19
+ const findUser: User = await userModel.findOne({ _id: userId });
20
+ if (!findUser) throw new HttpException(409, "You're not user");
21
+
22
+ return findUser;
23
+ }
24
+
25
+ public async createUser(userData: CreateUserDto): Promise<User> {
26
+ if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
27
+
28
+ const findUser: User = await userModel.findOne({ email: userData.email });
29
+ if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
30
+
31
+ const hashedPassword = await hash(userData.password, 10);
32
+ const createUserData: User = await userModel.create({ ...userData, password: hashedPassword });
33
+
34
+ return createUserData;
35
+ }
36
+
37
+ public async updateUser(userId: string, userData: CreateUserDto): Promise<User> {
38
+ if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
39
+
40
+ if (userData.email) {
41
+ const findUser: User = await userModel.findOne({ email: userData.email });
42
+ if (findUser && findUser._id != userId) throw new HttpException(409, `You're email ${userData.email} already exists`);
43
+ }
44
+
45
+ if (userData.password) {
46
+ const hashedPassword = await hash(userData.password, 10);
47
+ userData = { ...userData, password: hashedPassword };
48
+ }
49
+
50
+ const updateUserById: User = await userModel.findByIdAndUpdate(userId, { userData });
51
+ if (!updateUserById) throw new HttpException(409, "You're not user");
52
+
53
+ return updateUserById;
54
+ }
55
+
56
+ public async deleteUser(userId: string): Promise<User> {
57
+ const deleteUserById: User = await userModel.findByIdAndDelete(userId);
58
+ if (!deleteUserById) throw new HttpException(409, "You're not user");
59
+
60
+ return deleteUserById;
61
+ }
62
+ }
63
+
64
+ export default UserService;