mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-13 16:06:21 +00:00
dd43acecea
* Added VSCode settings to git repo. Now you can match yours with mine, if you want. * Added .jsbeautifyrc for js-beautify (to be used by VSCode's Beautify extension). * Refactored all instances of require('**/*.js') with require('**/*') wherever applicable (basically gotten rid of the .js extension). * Refactored path in all instances of require() wherever applicable. * Sorted instances of require() wherever applicable. * Fixed 500 HTTP error trying to load an error page for 505 HTTP error. * Removed special treatement of NoJS page from uploadsController.processFilesForDisplay(). * Updated version string of all static files. * Beautified all HTML, HANDLEBARS and CSS files. * Refactored the structure of footer links in homepage and No-JS uploader. This should now fix homepage going out-of-bound in smaller screens. * Added CSS prefixes wherever applicable. * Improved back-end side of No-JS uploader. This will now handle errors properly. * No-JS uploader will now show max file size. * No-JS uploader will now show a proper message when private mode is enabled and/or registration is disabled.
125 lines
4.6 KiB
JavaScript
125 lines
4.6 KiB
JavaScript
const bcrypt = require('bcrypt')
|
|
const config = require('./../config')
|
|
const db = require('knex')(config.database)
|
|
const randomstring = require('randomstring')
|
|
const utils = require('./utilsController')
|
|
|
|
const authController = {}
|
|
|
|
authController.verify = async (req, res, next) => {
|
|
const username = req.body.username
|
|
const password = req.body.password
|
|
|
|
if (username === undefined) { return res.json({ success: false, description: 'No username provided.' }) }
|
|
if (password === undefined) { return res.json({ success: false, description: 'No password provided.' }) }
|
|
|
|
const user = await db.table('users').where('username', username).first()
|
|
if (!user) { return res.json({ success: false, description: 'Username doesn\'t exist.' }) }
|
|
if (user.enabled === false || user.enabled === 0) {
|
|
return res.json({
|
|
success: false,
|
|
description: 'This account has been disabled.'
|
|
})
|
|
}
|
|
|
|
bcrypt.compare(password, user.password, (error, result) => {
|
|
if (error) {
|
|
console.log(error)
|
|
return res.json({ success: false, description: 'There was an error.' })
|
|
}
|
|
if (result === false) { return res.json({ success: false, description: 'Wrong password.' }) }
|
|
return res.json({ success: true, token: user.token })
|
|
})
|
|
}
|
|
|
|
authController.register = async (req, res, next) => {
|
|
if (config.enableUserAccounts === false) {
|
|
return res.json({ success: false, description: 'Register is disabled at the moment.' })
|
|
}
|
|
|
|
const username = req.body.username
|
|
const password = req.body.password
|
|
|
|
if (username === undefined) { return res.json({ success: false, description: 'No username provided.' }) }
|
|
if (password === undefined) { return res.json({ success: false, description: 'No password provided.' }) }
|
|
|
|
if (username.length < 4 || username.length > 32) {
|
|
return res.json({ success: false, description: 'Username must have 4-32 characters.' })
|
|
}
|
|
if (password.length < 6 || password.length > 64) {
|
|
return res.json({ success: false, description: 'Password must have 6-64 characters.' })
|
|
}
|
|
|
|
const user = await db.table('users').where('username', username).first()
|
|
if (user) { return res.json({ success: false, description: 'Username already exists.' }) }
|
|
|
|
bcrypt.hash(password, 10, async (error, hash) => {
|
|
if (error) {
|
|
console.log(error)
|
|
return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻.' })
|
|
}
|
|
const token = randomstring.generate(64)
|
|
await db.table('users').insert({
|
|
username,
|
|
password: hash,
|
|
token,
|
|
enabled: 1
|
|
})
|
|
return res.json({ success: true, token })
|
|
})
|
|
}
|
|
|
|
authController.changePassword = async (req, res, next) => {
|
|
const user = await utils.authorize(req, res)
|
|
if (!user) { return }
|
|
|
|
const password = req.body.password
|
|
if (password === undefined) { return res.json({ success: false, description: 'No password provided.' }) }
|
|
|
|
if (password.length < 6 || password.length > 64) {
|
|
return res.json({ success: false, description: 'Password must have 6-64 characters.' })
|
|
}
|
|
|
|
bcrypt.hash(password, 10, async (error, hash) => {
|
|
if (error) {
|
|
console.log(error)
|
|
return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻.' })
|
|
}
|
|
|
|
await db.table('users').where('id', user.id).update({ password: hash })
|
|
return res.json({ success: true })
|
|
})
|
|
}
|
|
|
|
authController.getFileLengthConfig = async (req, res, next) => {
|
|
const user = await utils.authorize(req, res)
|
|
if (!user) { return }
|
|
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 name length is disabled at the moment.' })
|
|
}
|
|
|
|
const user = await utils.authorize(req, res)
|
|
if (!user) { return }
|
|
|
|
const fileLength = parseInt(req.body.fileLength)
|
|
if (fileLength === undefined) { return res.json({ success: false, description: 'No file name length provided.' }) }
|
|
if (isNaN(fileLength)) { return res.json({ success: false, description: 'File name length is not a valid number.' }) }
|
|
|
|
if (fileLength < config.uploads.fileLength.min || fileLength > config.uploads.fileLength.max) {
|
|
return res.json({ success: false, description: `File name 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 })
|
|
}
|
|
|
|
module.exports = authController
|