Movedto postgres...

This commit is contained in:
BurnyLlama 2022-01-23 15:52:12 +01:00
parent 90c099f497
commit 8fc201f5f7
6 changed files with 53 additions and 30 deletions

View File

@ -3,3 +3,10 @@ GLAUTH_DB=""
# Which port should the server run on?
PORT=8080
# PostgreSQL Stuff
PG_HOST = "localhost"
PG_PORT = 5432
PG_USER = "glauth"
PG_PASS = "?secure12345passowrd!"
PG_DB = "glauth"

View File

@ -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'))

View File

@ -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))
)
}

View File

@ -6,7 +6,7 @@ export default function execawait(cmd) {
if (error) {
console.warn(error)
}
stdout ? resolve(stdout) : reject(stderr)
resolve(stdout ? stdout : stderr)
})
})
}

View File

@ -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"
}
}

View File

@ -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,8 +73,8 @@ 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(() => {
await execawait(`./captcha.sh ${captcha} > captcha.png`)
// Make it valid for 10 minutes
valid[captcha] = new Date()
@ -80,8 +82,6 @@ AUTH.get('/captcha', async (req, res) => {
res.contentType('image/png')
.sendFile('captcha.png', { root: './' })
})
.catch(() => res.status(501).send("ERROR! Failed to generate captcha."))
})
export default AUTH