filesafe/controllers/authController.js

89 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-01-29 07:16:56 +00:00
const config = require('../config.js')
const db = require('knex')(config.database)
const bcrypt = require('bcrypt')
const saltRounds = 10
const randomstring = require('randomstring')
let authController = {}
2017-03-17 04:14:24 +00:00
authController.verify = function(req, res, next) {
2017-01-29 07:16:56 +00:00
let username = req.body.username
let password = req.body.password
2017-03-17 04:14:24 +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-01-29 07:16:56 +00:00
db.table('users').where('username', username).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.json({ success: false, description: 'Username doesn\'t exist' })
2017-01-29 07:16:56 +00:00
bcrypt.compare(password, user[0].password, function(err, result) {
2017-03-17 04:14:24 +00:00
if (result === false) return res.json({ success: false, description: 'Wrong password' })
2017-01-29 07:16:56 +00:00
return res.json({ success: true, token: user[0].token })
})
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-29 07:16:56 +00:00
}
2017-03-17 04:14:24 +00:00
authController.register = function(req, res, next) {
2017-01-29 07:16:56 +00:00
2017-03-17 04:14:24 +00:00
if (config.enableUserAccounts === false)
2017-01-29 07:16:56 +00:00
return res.json({ success: false, description: 'Register is disabled at the moment' })
2017-01-30 07:42:15 +00:00
let username = req.body.username
2017-01-29 07:16:56 +00:00
let password = req.body.password
2017-03-17 04:14:24 +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-01-29 07:16:56 +00:00
2017-03-17 04:14:24 +00:00
if (username.length < 4 || username.length > 32)
2017-01-30 08:16:08 +00:00
return res.json({ success: false, description: 'Username must have 4-32 characters' })
2017-03-17 04:14:24 +00:00
if (password.length < 6 || password.length > 64)
2017-01-29 07:16:56 +00:00
return res.json({ success: false, description: 'Password must have 6-64 characters' })
db.table('users').where('username', username).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length !== 0) return res.json({ success: false, description: 'Username already exists' })
2017-01-29 07:16:56 +00:00
bcrypt.hash(password, saltRounds, function(err, hash) {
2017-03-17 04:14:24 +00:00
if (err) return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' })
2017-01-29 07:16:56 +00:00
let token = randomstring.generate(64)
db.table('users').insert({
username: username,
password: hash,
token: token
}).then(() => {
2017-03-17 04:14:24 +00:00
return res.json({ success: true, token: token })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-29 07:16:56 +00:00
})
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-29 07:16:56 +00:00
}
2017-03-17 04:14:24 +00:00
authController.changePassword = function(req, res, next) {
2017-01-30 01:30:00 +00:00
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-30 01:30:00 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
2017-01-30 01:30:00 +00:00
let password = req.body.password
2017-03-17 04:14:24 +00:00
if (password === undefined) return res.json({ success: false, description: 'No password provided' })
if (password.length < 6 || password.length > 64)
2017-01-30 01:30:00 +00:00
return res.json({ success: false, description: 'Password must have 6-64 characters' })
bcrypt.hash(password, saltRounds, function(err, hash) {
2017-03-17 04:14:24 +00:00
if (err) return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' })
2017-01-30 01:30:00 +00:00
2017-03-17 04:14:24 +00:00
db.table('users').where('id', user[0].id).update({ password: hash }).then(() => {
return res.json({ success: true })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-30 01:30:00 +00:00
})
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-30 01:30:00 +00:00
}
2017-03-17 04:14:24 +00:00
module.exports = authController