Skip to content

Commit

Permalink
chore: Refactor UserLoginService to use typed variables and improve t…
Browse files Browse the repository at this point in the history
…oken management
  • Loading branch information
drazisil committed Jun 8, 2024
1 parent 5f03fc3 commit 1dab559
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
67 changes: 34 additions & 33 deletions packages/main/src/UserLoginService.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,65 @@
/** @type {Array<{username: string, password: string, customerId: number}>} */
const users = [{ username: "admin", password: "admin", customerId: 1 }];
/** @type {Map<string, number>} */
const tokens = new Map();
type UserCredentials = Array<{
username: string;
password: string;
customerId: number;
}>;

const userCredentials: UserCredentials = [
{ username: "admin", password: "admin", customerId: 1 },
];
const authTokens: Map<string, number> = new Map();

export class UserLoginService {
/**
* Returns the customer ID if the user is valid, otherwise -1.
*
* @param {string} username
* @param {string} password
* @returns {number}
* Checks if the provided username and password match a user in the system.
* @param {string} username - The username to check.
* @param {string} password - The password to check.
* @returns {number} - The customer ID of the user if found, otherwise -1.
*/
checkUser(username: string, password: string) {
const user = users.find(
(user) => user.username === username && user.password === password,
authenticateUser(username: string, password: string): number {
const user = userCredentials.find(
(user) => user.username === username && user.password === password
);

return user ? user.customerId : -1;
}

/**
* Creates a token for the given customer ID.
*
* @param {number} customerId
* @returns {string}
* Generates a unique token for the given customer ID and stores it in the tokens map.
* @param customerId - The ID of the customer.
* @returns The generated token.
*/
createToken(customerId: number) {
generateToken(customerId: number): string {
const token = crypto.randomUUID();
tokens.set(token, customerId);
authTokens.set(token, customerId);
return token;
}

/**
* Checks if the token is valid and returns the customer ID.
* If the token is invalid, returns -1.
*
* @param {string} token
* @returns {number}
* Checks the validity of a token and returns the associated customer ID.
* @param token - The token to be checked.
* @returns The customer ID associated with the token, or -1 if the token is invalid.
*/
checkToken(token: string) {
const customerId = tokens.get(token);
getCustomerIdFromToken(token: string): number {
const customerId = authTokens.get(token);
return customerId ?? -1;
}

/**
* Deletes the token.
*
* @param {string} token
* Deletes a token from the collection.
* @param {string} token - The token to be deleted.
*/
deleteToken(token: string) {
tokens.delete(token);
removeToken(token: string): void {
authTokens.delete(token);
}

/**
* Deletes all tokens.
* @returns {Promise<void>}
* @returns A promise that resolves when all tokens are deleted.
*/
async deleteAllTokens(): Promise<void> {
async clearAllTokens(): Promise<void> {
return new Promise((resolve) => {
tokens.clear();
authTokens.clear();
console.log("All tokens deleted");
resolve();
});
Expand Down
14 changes: 7 additions & 7 deletions packages/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { verifyLegacyCipherSupport } from "./encryption.js";
type TOnDataHandler = (
port: number,
data: Buffer,
sendToClient: (data: Buffer) => void,
sendToClient: (data: Buffer) => void
) => void;

/**
Expand Down Expand Up @@ -140,19 +140,19 @@ function main() {
3000,
onWebListening,
onWebRequest,
onServerError,
onServerError
);
const loginServer = new TCPServer(
8226,
onSocketListening,
(socket: net.Socket) => onSocketConnection(socket, onNPSData),
onServerError,
onServerError
);
const personaServer = new TCPServer(
8228,
onSocketListening,
(socket: net.Socket) => onSocketConnection(socket, onNPSData),
onServerError,
onServerError
);

const shardService = new ShardService();
Expand All @@ -161,7 +161,7 @@ function main() {
"Rusty Motors",
"A test shard",
"10.10.5.20",
"Group - 1",
"Group - 1"
);

const userLoginService = new UserLoginService();
Expand All @@ -174,11 +174,11 @@ function main() {
mainLoop.addTask("stop", loginServer.close.bind(loginServer, onServerError));
mainLoop.addTask(
"stop",
personaServer.close.bind(personaServer, onServerError),
personaServer.close.bind(personaServer, onServerError)
);
mainLoop.addTask(
"stop",
userLoginService.deleteAllTokens.bind(userLoginService),
userLoginService.clearAllTokens.bind(userLoginService)
);

mainLoop.start();
Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ function authLogin(
password: string
) {
const userLoginService = new UserLoginService();
const customerId = userLoginService.checkUser(username, password);
const customerId = userLoginService.authenticateUser(username, password);

if (customerId === -1) {
return sendError(res, 401, "Invalid username or password");
}

const token = userLoginService.createToken(customerId);
const token = userLoginService.generateToken(customerId);
return sendTicket(res, token);
}

Expand Down

0 comments on commit 1dab559

Please sign in to comment.