Skip to content

Commit

Permalink
update express-2fa to Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Oct 11, 2024
1 parent 9146a68 commit 2d3f768
Show file tree
Hide file tree
Showing 10 changed files with 3,576 additions and 3,229 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/express-2fa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI express-2fa

on:
push:
branches: [master]
paths:
- "express-2fa/**"
pull_request:
branches: [master]
paths:
- "express-2fa/**"

jobs:
type-check:
runs-on: ubuntu-latest
defaults:
run:
working-directory: express-2fa
strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run type-check
9 changes: 9 additions & 0 deletions express-2fa/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
parserOptions: {
ecmaVersion: 6,
},
rules: {
'new-cap': 'off',
'no-console': 'off',
},
};
1 change: 1 addition & 0 deletions express-2fa/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.17.0
2 changes: 2 additions & 0 deletions express-2fa/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodejs 20.17.0
npm 10.8.1
93 changes: 0 additions & 93 deletions express-2fa/db.js

This file was deleted.

91 changes: 91 additions & 0 deletions express-2fa/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import mysql from "mysql";

let connection: mysql.Connection;

const config: mysql.ConnectionConfig = {
host: process.env.DB_SERVER_NAME,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
};

const dbConnection = (config: mysql.ConnectionConfig) => {
connection = mysql.createConnection(config);
return new Promise<void>((resolve, reject) => {
connection.connect((err, result) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};

export const insertUser = async (
name: string,
password: string,
number: string
) => {
try {
await dbConnection(config);
} catch (err) {
console.log("Error connecting to the database");
console.log(err);
}
const query =
"INSERT INTO `users`(`username`, `password`, `phone`, `verified`) VALUES (?, ?, ?, 0)";

return new Promise((resolve, reject) => {
connection.query(query, [name, password, number], (err, result) => {
if (err) {
reject(err);
} else {
console.log(`Inserted 1 user`);
resolve(result);
}
});
});
};

export const verifyUser = async (name: string) => {
try {
await dbConnection(config);
} catch (err) {
console.log("Error connecting to the database");
console.log(err);
}

const query = "UPDATE `users` SET `verified`=1 WHERE `username`=?";

return new Promise((resolve, reject) => {
connection.query(query, [name], (err, result) => {
if (err) {
reject(err);
} else {
console.log(`Verified ${name} in the database`);
resolve(result);
}
});
});
};

export const getUsername = async (name: string) => {
try {
await dbConnection(config);
} catch (err) {
console.log("Error connecting to the database");
console.log(err);
}
const query = "SELECT * FROM `users` WHERE `username`=?";

return new Promise<unknown[]>((resolve, reject) => {
connection.query(query, [name], (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
};
Loading

0 comments on commit 2d3f768

Please sign in to comment.