From 9ece687bfb8140fa87b8b89153a0533fb410c474 Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Wed, 3 Jan 2024 00:52:08 +0530 Subject: [PATCH] chore: added https service & modified 'auth' route. --- .eslintrc.json | 3 +- src/constants/index.ts | 2 + src/controllers/auth.controller.ts | 27 ++++---- src/routes/auth.routes.ts | 4 +- src/services/auth.service.ts | 100 ++++++++++++++--------------- src/utils/https.utils.ts | 28 ++++++++ 6 files changed, 93 insertions(+), 71 deletions(-) create mode 100644 src/utils/https.utils.ts diff --git a/.eslintrc.json b/.eslintrc.json index dee1611f9..d371d680f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -27,6 +27,7 @@ } ], "func-names": [0], - "no-console": ["error", { "allow": ["warn", "error", "info"] }] + "no-console": ["error", { "allow": ["warn", "error", "info"] }], + "@typescript-eslint/no-explicit-any": "off" } } diff --git a/src/constants/index.ts b/src/constants/index.ts index 1139c4b1d..a18227a96 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -8,6 +8,7 @@ export type HttpErrorCodes = { MOVED_PERMANENTLY: number; }; export type ConstantType = { + AXIOS_TIMEOUT: number; HTTP_ERROR_CODES: HttpErrorCodes; HTTP_ERROR_TEXTS: HttpErrorTexts; HTTP_RESPONSE_HEADERS: HttpResponseHeaders; @@ -26,6 +27,7 @@ export type HttpResponseHeaders = { }; export const constants: ConstantType = { + AXIOS_TIMEOUT: 60 * 1000, HTTP_ERROR_CODES: { HTTP_OK: 200, FORBIDDEN: 403, diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index 45a251cb4..411af0755 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -1,20 +1,15 @@ import { Request, Response } from "express"; import { userService } from "../services/auth.service"; - -const migrationAuthController = () => { - const login = async (req: Request, res: Response): Promise => { - try { - const apiResp = await userService.loginUser(req); - res.status(200).json(apiResp); - } catch (error) { - console.error(error); - res.status(500).json({ message: "Internal Server Error" }); - } - }; - - return { - login, - }; +const login = async (req: Request, res: Response) => { + try { + const apiResp = await userService.login(req); + res.status(200).json(apiResp); + } catch (error) { + console.error(error); + res.status(500).json({ message: "Internal Server Error" }); + } }; -export const authControllr = migrationAuthController(); +export const authController = { + login, +}; diff --git a/src/routes/auth.routes.ts b/src/routes/auth.routes.ts index e214e55ec..413b65634 100644 --- a/src/routes/auth.routes.ts +++ b/src/routes/auth.routes.ts @@ -1,10 +1,10 @@ import express from "express"; -import { authControllr } from "../controllers/auth.controller"; +import { authController } from "../controllers/auth.controller"; import { authenticateUser } from "../middlewares/auth.middleware"; const router = express.Router(); // Login route -router.post("/login", authenticateUser, authControllr.login); +router.post("/login", authenticateUser, authController.login); export default router; diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index fdb2456a2..46aee53ed 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -1,63 +1,59 @@ import { Request } from "express"; -import axios from "axios"; import { config } from "../config"; import { generateToken } from "../utils/jwt.utils"; +import https from "../utils/https.utils"; import * as fs from "fs/promises"; import { MigrationPayload, ResponseType } from "../models/types"; -const createUserService = () => { - const loginUser = async (req: Request): Promise => { - const userData = req.body; - - try { - const apiResponse = await axios({ - method: "POST", - url: config.CS_API.US, - headers: { - "Content-Type": "application/json", - }, - data: userData, - }); - - if (apiResponse) { - const migration_payload: MigrationPayload = { - region: userData.region, - user_id: apiResponse.data.user.uid, - }; - const migration_token = generateToken(migration_payload); - const response = { - message: apiResponse.data.notice, - status: 200, - migration_token, - }; - - // Create an object with migration_token as the key - const dataToWrite = { - [migration_token]: apiResponse.data.user.authtoken, - }; - - // Write the data to a JSON file (e.g., tokens.json) - await fs.writeFile("tokens.json", JSON.stringify(dataToWrite, null, 2)); - - return response; - } - - // Explicit return statement in case apiResponse is falsy - return null; - } catch (error) { - // Handle errors (e.g., log, return an error response) - console.error(error); - return { - message: "Error during login", - status: 500, - migration_token: null, +const login = async (req: Request): Promise => { + const userData = req.body; + + try { + const apiResponse = await https({ + method: "POST", + url: config.CS_API.US, + headers: { + "Content-Type": "application/json", + }, + data: userData, + }); + + if (apiResponse) { + const migration_payload: MigrationPayload = { + region: userData.region, + user_id: apiResponse.data.user.uid, }; + const migration_token = generateToken(migration_payload); + const response = { + message: apiResponse.data.notice, + status: 200, + migration_token, + }; + + // Create an object with migration_token as the key + const dataToWrite = { + [migration_token]: apiResponse.data.user.authtoken, + }; + + // Write the data to a JSON file (e.g., tokens.json) + await fs.writeFile("tokens.json", JSON.stringify(dataToWrite, null, 2)); + + return response; } - }; - return { - loginUser, - }; + // Explicit return statement in case apiResponse is falsy + return null; + } catch (error) { + // Handle errors (e.g., log, return an error response) + console.error(error); + return { + message: "Error during login", + status: 500, + migration_token: null, + }; + } }; -export const userService = createUserService(); +export const userService = { + login, +}; diff --git a/src/utils/https.utils.ts b/src/utils/https.utils.ts new file mode 100644 index 000000000..e3a3995b4 --- /dev/null +++ b/src/utils/https.utils.ts @@ -0,0 +1,28 @@ +import axios from "axios"; +import { constants } from "../constants"; + +type httpType = { + url: string; + method: string; + headers?: any; + data?: any; + timeout?: number; +}; +export default async (obj: httpType) => { + const { url, method, headers, data, timeout } = obj; + const res = await axios(url, { + method, + headers: headers, + ...(headers && { headers }), + timeout: timeout ?? constants.AXIOS_TIMEOUT, + ...(constants.METHODS_TO_INCLUDE_DATA_IN_AXIOS.includes(method) && { + data, + }), + }); + + return { + headers: res?.headers, + status: res?.status, + data: res?.data, + }; +};