diff --git a/.gitignore b/.gitignore index f8716bf8..9bb9af6b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ dist/ *.user *.userosscache *.sln.docstates - +.env # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/package.json b/package.json index 14151634..181ef024 100644 --- a/package.json +++ b/package.json @@ -26,11 +26,14 @@ "@types/express": "^4.17.21", "axios": "^1.6.2", "cors": "^2.8.5", + "dotenv": "^16.3.1", "express": "^4.18.2", - "helmet": "^6.0.1" + "helmet": "^6.0.1", + "jsonwebtoken": "^9.0.2" }, "devDependencies": { "@types/cors": "^2.8.17", + "@types/jsonwebtoken": "^9.0.5", "@types/node": "^20.10.4", "@typescript-eslint/eslint-plugin": "^6.15.0", "@typescript-eslint/parser": "^6.15.0", diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index d2b915ff..45a251cb 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -1,16 +1,20 @@ import { Request, Response } from "express"; import { userService } from "../services/auth.service"; -const authController = { - async login(req: Request, res: Response): Promise { +const migrationAuthController = () => { + const login = async (req: Request, res: Response): Promise => { try { const apiResp = await userService.loginUser(req); - res.status(500).json(apiResp); + res.status(200).json(apiResp); } catch (error) { console.error(error); res.status(500).json({ message: "Internal Server Error" }); } - }, + }; + + return { + login, + }; }; -export default authController; +export const authControllr = migrationAuthController(); diff --git a/src/middlewares/auth.middleware.ts b/src/middlewares/auth.middleware.ts index 0402416f..21a7333d 100644 --- a/src/middlewares/auth.middleware.ts +++ b/src/middlewares/auth.middleware.ts @@ -8,7 +8,6 @@ export const authenticateUser = ( ): void => { // Simulate authentication logic (e.g., check for a token) const isAuthenticated = /* Your authentication logic here */ true; - if (isAuthenticated) { next(); // User is authenticated, proceed to the next middleware or route handler } else { diff --git a/src/models/types.ts b/src/models/types.ts new file mode 100644 index 00000000..c5fc46bd --- /dev/null +++ b/src/models/types.ts @@ -0,0 +1,15 @@ +export interface User { + email: string; + password: string; +} + +export interface ResponseType { + message: string; + status: number; + migration_token: string | null; +} + +export interface MigrationPayload { + region: string; + user_id: string; +} diff --git a/src/models/user.model.ts b/src/models/user.model.ts deleted file mode 100644 index 25f4c7eb..00000000 --- a/src/models/user.model.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface User { - email: string; - password: string; -} diff --git a/src/routes/auth.routes.ts b/src/routes/auth.routes.ts index 5f46daeb..e214e55e 100644 --- a/src/routes/auth.routes.ts +++ b/src/routes/auth.routes.ts @@ -1,10 +1,10 @@ import express from "express"; -import authController from "../controllers/auth.controller"; +import { authControllr } from "../controllers/auth.controller"; import { authenticateUser } from "../middlewares/auth.middleware"; const router = express.Router(); // Login route -router.post("/login", authenticateUser, authController.login); +router.post("/login", authenticateUser, authControllr.login); export default router; diff --git a/src/server.ts b/src/server.ts index eeb113cb..204ba7e4 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2,12 +2,14 @@ import { parseCLIArgsFromProcess, loadConfigFile } from "./utils"; import { constants } from "./constants"; import express, { NextFunction, Request, Response } from "express"; import cors from "cors"; +import dotenv from "dotenv"; import helmet from "helmet"; const PORT = process.env.PORT || 5000; import authRoutes from "./routes/auth.routes"; try { loadConfigFile(parseCLIArgsFromProcess(process.argv)); + dotenv.config(); const app = express(); app.use( diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 1d4421ef..4129b62b 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -1,20 +1,58 @@ import { Request } from "express"; import axios from "axios"; import { API_MGMT_URL } from "../config"; +import { generateToken } from "../utils/jwt.utils"; +import * as fs from "fs/promises"; +import { MigrationPayload, ResponseType } from "../models/types"; const createUserService = () => { - const loginUser = async (req: Request): Promise => { + const loginUser = async (req: Request): Promise => { const userData = req.body; - const apiResponse = await axios({ - method: "POST", - url: API_MGMT_URL, - headers: { - "Content-Type": "application/json", - }, - data: userData, - }); - - return apiResponse.data; + + try { + const apiResponse = await axios({ + method: "POST", + url: API_MGMT_URL, + 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, + }; + } }; return { diff --git a/src/utils/jwt.utils.ts b/src/utils/jwt.utils.ts new file mode 100644 index 00000000..ef3f877a --- /dev/null +++ b/src/utils/jwt.utils.ts @@ -0,0 +1,10 @@ +/// src/utils/jwt.utils.ts +import jwt from "jsonwebtoken"; +import { MigrationPayload } from "../models/types"; + +const secretKey = process.env.JWT_SECRET_KEY ?? "default_secret_key"; + +// @typescript-eslint/no-explicit-any +export const generateToken = (payload: MigrationPayload): string => { + return jwt.sign(payload, secretKey, { expiresIn: "1h" }); +};