Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 2 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@ export type HttpResponseHeaders = {
};

export const constants: ConstantType = {
AXIOS_TIMEOUT: 60 * 1000,
HTTP_ERROR_CODES: {
HTTP_OK: 200,
FORBIDDEN: 403,
Expand Down
27 changes: 11 additions & 16 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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,
};
4 changes: 2 additions & 2 deletions src/routes/auth.routes.ts
Original file line number Diff line number Diff line change
@@ -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;
100 changes: 48 additions & 52 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -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<ResponseType | null> => {
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<ResponseType | null> => {
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,
};
28 changes: 28 additions & 0 deletions src/utils/https.utils.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};