aboutsummaryrefslogtreecommitdiff
path: root/controllers/authController.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2017-01-29 04:16:56 -0300
committerPitu <[email protected]>2017-01-29 04:16:56 -0300
commit5f0ff7f6aac147fcba38d435843118fc8f2d7614 (patch)
tree8f088d66017769c59d82d3c7b051c2a20cbf87c3 /controllers/authController.js
parentRemoved unused dependency (diff)
downloadhost.fuwn.me-5f0ff7f6aac147fcba38d435843118fc8f2d7614.tar.xz
host.fuwn.me-5f0ff7f6aac147fcba38d435843118fc8f2d7614.zip
Created login/register controller
Diffstat (limited to 'controllers/authController.js')
-rw-r--r--controllers/authController.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/controllers/authController.js b/controllers/authController.js
new file mode 100644
index 0000000..86f92fd
--- /dev/null
+++ b/controllers/authController.js
@@ -0,0 +1,64 @@
+const config = require('../config.js')
+const db = require('knex')(config.database)
+const bcrypt = require('bcrypt')
+const saltRounds = 10
+const randomstring = require('randomstring')
+
+let authController = {}
+
+authController.verify = function(req, res, next){
+ let username = req.body.username
+ let 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' })
+
+ db.table('users').where('username', username).then((user) => {
+ if(user.length === 0) return res.json({ success: false, description: 'Username doesn\'t exist' })
+
+ bcrypt.compare(password, user[0].password, function(err, result) {
+ if(result === false) return res.json({ success: false, description: 'Wrong password' })
+ return res.json({ success: true, token: user[0].token })
+ })
+ }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
+
+}
+
+authController.register = function(req, res, next){
+
+ if(config.enableUserAccounts === false)
+ return res.json({ success: false, description: 'Register is disabled at the moment' })
+
+ let username = req.body.user
+ let 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 < 6 || username.length > 32)
+ return res.json({ success: false, description: 'Username must have 6-32 characters' })
+ if(password.length < 6 || password.length > 64)
+ return res.json({ success: false, description: 'Password must have 6-64 characters' })
+
+ db.table('users').where('username', username).then((user) => {
+ if(user.length !== 0) return res.json({ success: false, description: 'Username already exists' })
+
+ bcrypt.hash(password, saltRounds, function(err, hash) {
+ if(err) return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' })
+
+ let token = randomstring.generate(64)
+
+ db.table('users').insert({
+ username: username,
+ password: hash,
+ token: token
+ }).then(() => {
+ return res.json({ success: true, token: token})
+ }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
+ })
+
+ }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
+
+}
+
+module.exports = authController \ No newline at end of file