Movedto postgres...
This commit is contained in:
parent
90c099f497
commit
8fc201f5f7
|
@ -3,3 +3,10 @@ GLAUTH_DB=""
|
||||||
|
|
||||||
# Which port should the server run on?
|
# Which port should the server run on?
|
||||||
PORT=8080
|
PORT=8080
|
||||||
|
|
||||||
|
# PostgreSQL Stuff
|
||||||
|
PG_HOST = "localhost"
|
||||||
|
PG_PORT = 5432
|
||||||
|
PG_USER = "glauth"
|
||||||
|
PG_PASS = "?secure12345passowrd!"
|
||||||
|
PG_DB = "glauth"
|
7
index.js
7
index.js
|
@ -9,7 +9,12 @@ dotenv.config()
|
||||||
const APP = express()
|
const APP = express()
|
||||||
const PORT = process.env.PORT ?? 12345
|
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(express.urlencoded({ extended: true }))
|
||||||
APP.use('/static', express.static('static'))
|
APP.use('/static', express.static('static'))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import SQLDatabase from 'better-sqlite3'
|
import pg from 'pg'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {object} User
|
* @typedef {object} User
|
||||||
|
@ -10,12 +10,23 @@ import SQLDatabase from 'better-sqlite3'
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {SQLDatabase.Database}
|
* @type {pg.Database}
|
||||||
*/
|
*/
|
||||||
export let glauth = undefined
|
export let glauth = undefined
|
||||||
|
|
||||||
export function dbInit() {
|
export async function dbInit() {
|
||||||
glauth = new SQLDatabase(process.env.GLAUTH_DB ?? 'auth.db', {})
|
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))
|
||||||
|
)
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@ export default function execawait(cmd) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.warn(error)
|
console.warn(error)
|
||||||
}
|
}
|
||||||
stdout ? resolve(stdout) : reject(stderr)
|
resolve(stdout ? stdout : stderr)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bcrypt": "^5.0.1",
|
"bcrypt": "^5.0.1",
|
||||||
"better-sqlite3": "^7.4.6",
|
|
||||||
"dotenv": "^12.0.3",
|
"dotenv": "^12.0.3",
|
||||||
"express": "^4.17.2",
|
"express": "^4.17.2",
|
||||||
"nunjucks": "^3.2.3"
|
"nunjucks": "^3.2.3",
|
||||||
|
"pg": "^8.7.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ const AUTH = Router()
|
||||||
let valid = {}
|
let valid = {}
|
||||||
|
|
||||||
|
|
||||||
AUTH.post('/register', (req, res) => {
|
AUTH.post('/register', async (req, res) => {
|
||||||
const { captcha, password, username } = req.body
|
const { captcha, password, username } = req.body
|
||||||
|
|
||||||
// Was input sent?
|
// Was input sent?
|
||||||
|
@ -31,30 +31,32 @@ AUTH.post('/register', (req, res) => {
|
||||||
if (!(/^(?=[a-zA-Z0-9]{2,20}$).*$/.test(username)))
|
if (!(/^(?=[a-zA-Z0-9]{2,20}$).*$/.test(username)))
|
||||||
return(res.send("Username does not match the requirements"))
|
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"))
|
return(res.send("User already exists"))
|
||||||
|
|
||||||
bcrypt.hash(password, 10).then(
|
bcrypt.hash(password, 10).then(
|
||||||
hash => {
|
hash => {
|
||||||
glauth.prepare(`
|
glauth.query(
|
||||||
INSERT INTO users(
|
"INSERT INTO users(name, primarygroup, passbcrypt) VALUES($1::text, 0, $2::text)",
|
||||||
name, primarygroup, passbcrypt
|
[ username, hash ]
|
||||||
) VALUES(?, 0, ?)
|
).then(
|
||||||
`).run(username, hash)
|
() => 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
|
const { password, username } = req.body
|
||||||
|
|
||||||
// Was input sent?
|
// Was input sent?
|
||||||
if (!username || !password )
|
if (!username || !password )
|
||||||
return(res.send(`Not entered:${username ? '' : ' username,'}${password ? '' : ' 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)
|
if (!user)
|
||||||
return(res.send("User doesn't exist!"))
|
return(res.send("User doesn't exist!"))
|
||||||
|
@ -71,16 +73,14 @@ AUTH.post('/login', (req, res) => {
|
||||||
|
|
||||||
AUTH.get('/captcha', async (req, res) => {
|
AUTH.get('/captcha', async (req, res) => {
|
||||||
const captcha = crypto.randomBytes(3).toString('hex')
|
const captcha = crypto.randomBytes(3).toString('hex')
|
||||||
execawait(`./captcha.sh ${captcha} > captcha.png`)
|
await execawait(`./captcha.sh ${captcha} > captcha.png`)
|
||||||
.then(() => {
|
|
||||||
// Make it valid for 10 minutes
|
|
||||||
valid[captcha] = new Date()
|
|
||||||
|
|
||||||
// Send the captcha image
|
// Make it valid for 10 minutes
|
||||||
res.contentType('image/png')
|
valid[captcha] = new Date()
|
||||||
.sendFile('captcha.png', { root: './' })
|
|
||||||
})
|
// Send the captcha image
|
||||||
.catch(() => res.status(501).send("ERROR! Failed to generate captcha."))
|
res.contentType('image/png')
|
||||||
|
.sendFile('captcha.png', { root: './' })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user