2018-01-23 20:06:30 +00:00
|
|
|
const config = require('../config.js')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const bcrypt = require('bcrypt')
|
|
|
|
const randomstring = require('randomstring')
|
|
|
|
const utils = require('./utilsController.js')
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
let authController = {}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
authController.verify = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const username = req.body.username
|
|
|
|
const password = req.body.password
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-03-24 13:52:47 +00:00
|
|
|
if (username === undefined) return res.json({ success: false, description: 'No username provided.' })
|
|
|
|
if (password === undefined) return res.json({ success: false, description: 'No password provided.' })
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await db.table('users').where('username', username).first()
|
2018-03-24 13:52:47 +00:00
|
|
|
if (!user) return res.json({ success: false, description: 'Username doesn\'t exist.' })
|
2018-03-14 06:57:09 +00:00
|
|
|
if (user.enabled === false || user.enabled === 0) {
|
|
|
|
return res.json({
|
|
|
|
success: false,
|
2018-03-24 13:52:47 +00:00
|
|
|
description: 'This account has been disabled.'
|
2018-03-14 06:57:09 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
bcrypt.compare(password, user.password, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'There was an error.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2018-03-24 13:52:47 +00:00
|
|
|
if (result === false) return res.json({ success: false, description: 'Wrong password.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
return res.json({ success: true, token: user.token })
|
|
|
|
})
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
authController.register = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
if (config.enableUserAccounts === false) {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'Register is disabled at the moment.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const username = req.body.username
|
|
|
|
const password = req.body.password
|
|
|
|
|
2018-03-24 13:52:47 +00:00
|
|
|
if (username === undefined) return res.json({ success: false, description: 'No username provided.' })
|
|
|
|
if (password === undefined) return res.json({ success: false, description: 'No password provided.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
if (username.length < 4 || username.length > 32) {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'Username must have 4-32 characters.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
if (password.length < 6 || password.length > 64) {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'Password must have 6-64 characters.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const user = await db.table('users').where('username', username).first()
|
2018-03-24 13:52:47 +00:00
|
|
|
if (user) return res.json({ success: false, description: 'Username already exists.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
bcrypt.hash(password, 10, async (err, hash) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
const token = randomstring.generate(64)
|
|
|
|
await db.table('users').insert({
|
|
|
|
username: username,
|
|
|
|
password: hash,
|
2018-03-14 06:57:09 +00:00
|
|
|
token: token,
|
|
|
|
enabled: 1
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
|
|
|
return res.json({ success: true, token: token })
|
|
|
|
})
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
|
|
|
authController.changePassword = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
let password = req.body.password
|
2018-03-24 13:52:47 +00:00
|
|
|
if (password === undefined) return res.json({ success: false, description: 'No password provided.' })
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
if (password.length < 6 || password.length > 64) {
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'Password must have 6-64 characters.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
bcrypt.hash(password, 10, async (err, hash) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
2018-03-24 13:52:47 +00:00
|
|
|
return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻.' })
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
await db.table('users').where('id', user.id).update({ password: hash })
|
|
|
|
return res.json({ success: true })
|
|
|
|
})
|
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-03-24 13:52:47 +00:00
|
|
|
authController.getFileLengthConfig = async (req, res, next) => {
|
|
|
|
const user = await utils.authorize(req, res)
|
|
|
|
return res.json({ success: true, fileLength: user.fileLength, config: config.uploads.fileLength })
|
|
|
|
}
|
|
|
|
|
|
|
|
authController.changeFileLength = async (req, res, next) => {
|
|
|
|
if (config.uploads.fileLength.userChangeable === false) {
|
|
|
|
return res.json({ success: false, description: 'Changing file length is disabled at the moment.' })
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await utils.authorize(req, res)
|
|
|
|
|
|
|
|
let fileLength = parseInt(req.body.fileLength)
|
|
|
|
if (fileLength === undefined) return res.json({ success: false, description: 'No file length provided.' })
|
|
|
|
if (isNaN(fileLength)) return res.json({ success: false, description: 'File length is not a valid number.' })
|
|
|
|
|
|
|
|
if (fileLength < config.uploads.fileLength.min || fileLength > config.uploads.fileLength.max) {
|
|
|
|
return res.json({ success: false, description: `File length must be ${config.uploads.fileLength.min} to ${config.uploads.fileLength.max} characters` })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileLength === user.fileLength) {
|
|
|
|
return res.json({ success: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.table('users').where('id', user.id).update({ fileLength })
|
|
|
|
return res.json({ success: true })
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = authController
|