diff --git a/.env.template b/.env.template index 0a58f1d..dbf9d5a 100644 --- a/.env.template +++ b/.env.template @@ -2,4 +2,11 @@ GLAUTH_DB="" # Which port should the server run on? -PORT=8080 \ No newline at end of file +PORT=8080 + +# PostgreSQL Stuff +PG_HOST = "localhost" +PG_PORT = 5432 +PG_USER = "glauth" +PG_PASS = "?secure12345passowrd!" +PG_DB = "glauth" \ No newline at end of file diff --git a/index.js b/index.js index 796c5e3..fe5a40a 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,12 @@ dotenv.config() const APP = express() const PORT = process.env.PORT ?? 12345 -dbInit() +await dbInit() + .then( + () => console.log("Connected to database!") + ).catch( + err => console.log("Error occured!") && console.dir(err, { depth: null }) + ) APP.use(express.urlencoded({ extended: true })) APP.use('/static', express.static('static')) diff --git a/libs/database.js b/libs/database.js index dfb04ec..1680ae7 100644 --- a/libs/database.js +++ b/libs/database.js @@ -1,4 +1,4 @@ -import SQLDatabase from 'better-sqlite3' +import pg from 'pg' /** * @typedef {object} User @@ -10,12 +10,23 @@ import SQLDatabase from 'better-sqlite3' */ /** - * @type {SQLDatabase.Database} + * @type {pg.Database} */ export let glauth = undefined -export function dbInit() { - glauth = new SQLDatabase(process.env.GLAUTH_DB ?? 'auth.db', {}) +export async function dbInit() { + glauth = new pg.Client({ + host: process.env.PG_HOST ?? 'localhost', + port: process.env.PG_PORT ?? 5432, + user: process.env.PG_USER ?? 'glauth', + password: process.env.PG_PASS ?? 'glauth-password', + database: process.env.PG_DB ?? 'glauth', + }) - console.log(`Loaded in GLAuth - users.db`) + return new Promise( + (resolve, reject) => glauth + .connect() + .then(() => resolve()) + .catch(err => reject(err)) + ) } \ No newline at end of file diff --git a/libs/execawait.js b/libs/execawait.js index 3798ef8..9666ef2 100644 --- a/libs/execawait.js +++ b/libs/execawait.js @@ -6,7 +6,7 @@ export default function execawait(cmd) { if (error) { console.warn(error) } - stdout ? resolve(stdout) : reject(stderr) + resolve(stdout ? stdout : stderr) }) }) } diff --git a/package.json b/package.json index 4072dd6..9ffe7fd 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,9 @@ "license": "ISC", "dependencies": { "bcrypt": "^5.0.1", - "better-sqlite3": "^7.4.6", "dotenv": "^12.0.3", "express": "^4.17.2", - "nunjucks": "^3.2.3" + "nunjucks": "^3.2.3", + "pg": "^8.7.1" } } diff --git a/routes/auth.js b/routes/auth.js index 8946827..7edbe68 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -8,7 +8,7 @@ const AUTH = Router() let valid = {} -AUTH.post('/register', (req, res) => { +AUTH.post('/register', async (req, res) => { const { captcha, password, username } = req.body // Was input sent? @@ -31,30 +31,32 @@ AUTH.post('/register', (req, res) => { if (!(/^(?=[a-zA-Z0-9]{2,20}$).*$/.test(username))) return(res.send("Username does not match the requirements")) - if (glauth.prepare(`SELECT * FROM users WHERE name = ?`).get(username)) + if ((await glauth.query("SELECT * FROM users WHERE name = $1::text", [ username ])).rowCount) return(res.send("User already exists")) bcrypt.hash(password, 10).then( hash => { - glauth.prepare(` - INSERT INTO users( - name, primarygroup, passbcrypt - ) VALUES(?, 0, ?) - `).run(username, hash) + glauth.query( + "INSERT INTO users(name, primarygroup, passbcrypt) VALUES($1::text, 0, $2::text)", + [ username, hash ] + ).then( + () => res.send("Account registered!") + ).catch( + err => res.json({ _: "Sorry an error occured!", err }) + ) - res.send("Account registered!") } ) }) -AUTH.post('/login', (req, res) => { +AUTH.post('/login', async (req, res) => { const { password, username } = req.body // Was input sent? if (!username || !password ) return(res.send(`Not entered:${username ? '' : ' username,'}${password ? '' : ' password'}`)) - const user = glauth.prepare(`SELECT * FROM users WHERE name = ?`).get(username) + const user = (await glauth.query("SELECT * FROM users WHERE name = $1::text", [ username ])).rows[0] if (!user) return(res.send("User doesn't exist!")) @@ -71,16 +73,14 @@ AUTH.post('/login', (req, res) => { AUTH.get('/captcha', async (req, res) => { const captcha = crypto.randomBytes(3).toString('hex') - execawait(`./captcha.sh ${captcha} > captcha.png`) - .then(() => { - // Make it valid for 10 minutes - valid[captcha] = new Date() - - // Send the captcha image - res.contentType('image/png') - .sendFile('captcha.png', { root: './' }) - }) - .catch(() => res.status(501).send("ERROR! Failed to generate captcha.")) + await execawait(`./captcha.sh ${captcha} > captcha.png`) + + // Make it valid for 10 minutes + valid[captcha] = new Date() + + // Send the captcha image + res.contentType('image/png') + .sendFile('captcha.png', { root: './' }) })