typescript-express-starter 8.1.7 → 9.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 (56) hide show
  1. package/README.kr.md +5 -6
  2. package/README.md +5 -4
  3. package/lib/mikro-orm/.dockerignore +18 -0
  4. package/lib/mikro-orm/.editorconfig +9 -0
  5. package/lib/mikro-orm/.env.development.local +18 -0
  6. package/lib/mikro-orm/.env.production.local +18 -0
  7. package/lib/mikro-orm/.env.test.local +18 -0
  8. package/lib/mikro-orm/.eslintignore +1 -0
  9. package/lib/mikro-orm/.eslintrc +18 -0
  10. package/lib/mikro-orm/.huskyrc +5 -0
  11. package/lib/mikro-orm/.lintstagedrc.json +5 -0
  12. package/lib/mikro-orm/.prettierrc +8 -0
  13. package/lib/mikro-orm/.swcrc +41 -0
  14. package/lib/mikro-orm/.vscode/launch.json +35 -0
  15. package/lib/mikro-orm/.vscode/settings.json +6 -0
  16. package/lib/mikro-orm/Dockerfile +24 -0
  17. package/lib/mikro-orm/Makefile +29 -0
  18. package/lib/mikro-orm/docker-compose.yml +46 -0
  19. package/lib/mikro-orm/ecosystem.config.js +57 -0
  20. package/lib/mikro-orm/jest.config.js +12 -0
  21. package/lib/mikro-orm/nginx.conf +40 -0
  22. package/lib/mikro-orm/nodemon.json +12 -0
  23. package/lib/mikro-orm/package.json +77 -0
  24. package/lib/mikro-orm/src/app.ts +98 -0
  25. package/lib/mikro-orm/src/config/index.ts +5 -0
  26. package/lib/mikro-orm/src/controllers/auth.controller.ts +46 -0
  27. package/lib/mikro-orm/src/controllers/index.controller.ts +13 -0
  28. package/lib/mikro-orm/src/controllers/users.controller.ts +65 -0
  29. package/lib/mikro-orm/src/databases/index.ts +19 -0
  30. package/lib/mikro-orm/src/dtos/users.dto.ts +9 -0
  31. package/lib/mikro-orm/src/entities/base.entity.ts +16 -0
  32. package/lib/mikro-orm/src/entities/users.entity.ts +17 -0
  33. package/lib/mikro-orm/src/exceptions/HttpException.ts +10 -0
  34. package/lib/mikro-orm/src/http/auth.http +32 -0
  35. package/lib/mikro-orm/src/http/users.http +34 -0
  36. package/lib/mikro-orm/src/interfaces/auth.interface.ts +15 -0
  37. package/lib/mikro-orm/src/interfaces/routes.interface.ts +6 -0
  38. package/lib/mikro-orm/src/interfaces/users.interface.ts +5 -0
  39. package/lib/mikro-orm/src/middlewares/auth.middleware.ts +32 -0
  40. package/lib/mikro-orm/src/middlewares/error.middleware.ts +17 -0
  41. package/lib/mikro-orm/src/middlewares/validation.middleware.ts +25 -0
  42. package/lib/mikro-orm/src/routes/auth.route.ts +24 -0
  43. package/lib/mikro-orm/src/routes/index.route.ts +19 -0
  44. package/lib/mikro-orm/src/routes/users.route.ts +25 -0
  45. package/lib/mikro-orm/src/server.ts +11 -0
  46. package/lib/mikro-orm/src/services/auth.service.ts +63 -0
  47. package/lib/mikro-orm/src/services/users.service.ts +68 -0
  48. package/lib/mikro-orm/src/tests/auth.test.ts +66 -0
  49. package/lib/mikro-orm/src/tests/index.test.ts +18 -0
  50. package/lib/mikro-orm/src/tests/users.test.ts +70 -0
  51. package/lib/mikro-orm/src/utils/logger.ts +65 -0
  52. package/lib/mikro-orm/src/utils/util.ts +19 -0
  53. package/lib/mikro-orm/src/utils/validateEnv.ts +10 -0
  54. package/lib/mikro-orm/swagger.yaml +122 -0
  55. package/lib/mikro-orm/tsconfig.json +41 -0
  56. package/package.json +6 -15
@@ -0,0 +1,98 @@
1
+ import express from 'express';
2
+ import cookieParser from 'cookie-parser';
3
+ import compression from 'compression';
4
+ import cors from 'cors';
5
+ import helmet from 'helmet';
6
+ import hpp from 'hpp';
7
+ import morgan from 'morgan';
8
+ import swaggerJSDoc from 'swagger-jsdoc';
9
+ import swaggerUi from 'swagger-ui-express';
10
+ import { logger, stream } from '@utils/logger';
11
+ import { MikroORM, RequestContext } from '@mikro-orm/core';
12
+ import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
13
+ import { DI, dbOptions } from '@databases';
14
+ import { UserEntity } from '@entities/users.entity';
15
+ import { Routes } from '@interfaces/routes.interface';
16
+ import errorMiddleware from '@middlewares/error.middleware';
17
+
18
+ class App {
19
+ public app: express.Application;
20
+ public env: string;
21
+ public port: string | number;
22
+ public host: string;
23
+
24
+ constructor(routes: Routes[]) {
25
+ this.app = express();
26
+ this.env = NODE_ENV || 'development';
27
+ this.port = PORT || 3000;
28
+
29
+ this.initializeMiddlewares();
30
+ this.connectToDatabase();
31
+ this.initializeRoutes(routes);
32
+ this.initializeSwagger();
33
+ this.initializeErrorHandling();
34
+ }
35
+
36
+ public listen() {
37
+ this.app.listen(this.port, () => {
38
+ logger.info(`=================================`);
39
+ logger.info(`======= ENV: ${this.env} =======`);
40
+ logger.info(`🚀 App listening on the port ${this.port}`);
41
+ logger.info(`=================================`);
42
+ });
43
+ }
44
+
45
+ public getServer() {
46
+ return this.app;
47
+ }
48
+
49
+ private async connectToDatabase() {
50
+ try {
51
+ DI.orm = await MikroORM.init(dbOptions);
52
+ DI.em = DI.orm.em.fork();
53
+ DI.userRepository = DI.orm.em.fork().getRepository(UserEntity);
54
+ } catch (error) {
55
+ logger.error(error);
56
+ }
57
+ this.app.use((_1, _2, next) => RequestContext.create(DI.orm.em, next));
58
+ }
59
+
60
+ private initializeMiddlewares() {
61
+ this.app.use(morgan(LOG_FORMAT, { stream }));
62
+ this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
63
+ this.app.use(hpp());
64
+ this.app.use(helmet());
65
+ this.app.use(compression());
66
+ this.app.use(express.json());
67
+ this.app.use(express.urlencoded({ extended: true }));
68
+ this.app.use(cookieParser());
69
+ }
70
+
71
+ private initializeRoutes(routes: Routes[]) {
72
+ routes.forEach(route => {
73
+ this.app.use('/', route.router);
74
+ });
75
+ }
76
+
77
+ private initializeSwagger() {
78
+ const options = {
79
+ swaggerDefinition: {
80
+ info: {
81
+ title: 'REST API',
82
+ version: '1.0.0',
83
+ description: 'Example docs',
84
+ },
85
+ },
86
+ apis: ['swagger.yaml'],
87
+ };
88
+
89
+ const specs = swaggerJSDoc(options);
90
+ this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
91
+ }
92
+
93
+ private initializeErrorHandling() {
94
+ this.app.use(errorMiddleware);
95
+ }
96
+ }
97
+
98
+ 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 = 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,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,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,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/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,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 { 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;