authController.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const config = require('../config.js');
  2. const db = require('knex')(config.database);
  3. const bcrypt = require('bcrypt');
  4. const randomstring = require('randomstring');
  5. const utils = require('./utilsController.js');
  6. let authController = {};
  7. authController.verify = async (req, res, next) => {
  8. const username = req.body.username;
  9. const password = req.body.password;
  10. if (username === undefined) return res.json({ success: false, description: 'No username provided' });
  11. if (password === undefined) return res.json({ success: false, description: 'No password provided' });
  12. const user = await db.table('users').where('username', username).first();
  13. if (!user) return res.json({ success: false, description: 'Username doesn\'t exist' });
  14. if (user.enabled === false || user.enabled === 0) return res.json({
  15. success: false,
  16. description: 'This account has been disabled'
  17. });
  18. bcrypt.compare(password, user.password, (err, result) => {
  19. if (err) {
  20. console.log(err);
  21. return res.json({ success: false, description: 'There was an error' });
  22. }
  23. if (result === false) return res.json({ success: false, description: 'Wrong password' });
  24. return res.json({ success: true, token: user.token });
  25. });
  26. };
  27. authController.register = async (req, res, next) => {
  28. const currentUser = await utils.getUser(req);
  29. if(currentUser && currentUser.username != 'root') {
  30. return res.json({ success: false, description: 'Non-root users cannot create more accounts!' });
  31. }
  32. if (config.enableUserAccounts === false && !currentUser) {
  33. return res.json({ success: false, description: 'Register is disabled at the moment' });
  34. }
  35. const username = req.body.username;
  36. const password = req.body.password;
  37. if (username === undefined) return res.json({ success: false, description: 'No username provided' });
  38. if (password === undefined) return res.json({ success: false, description: 'No password provided' });
  39. if (username.length < 4 || username.length > 32) {
  40. return res.json({ success: false, description: 'Username must have 4-32 characters' });
  41. }
  42. if (password.length < 6 || password.length > 64) {
  43. return res.json({ success: false, description: 'Password must have 6-64 characters' });
  44. }
  45. const user = await db.table('users').where('username', username).first();
  46. if (user) return res.json({ success: false, description: 'Username already exists' });
  47. bcrypt.hash(password, 10, async (err, hash) => {
  48. if (err) {
  49. console.log(err);
  50. return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' });
  51. }
  52. const token = randomstring.generate(64);
  53. await db.table('users').insert({
  54. username: username,
  55. password: hash,
  56. token: token,
  57. enabled: 1
  58. });
  59. return res.json({ success: true, token: token });
  60. });
  61. };
  62. authController.changePassword = async (req, res, next) => {
  63. const {user, response} = await utils.authorize(req, res);
  64. if(!user) return response;
  65. let password = req.body.password;
  66. if (password === undefined) return res.json({ success: false, description: 'No password provided' });
  67. if (password.length < 6 || password.length > 64) {
  68. return res.json({ success: false, description: 'Password must have 6-64 characters' });
  69. }
  70. bcrypt.hash(password, 10, async (err, hash) => {
  71. if (err) {
  72. console.log(err);
  73. return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' });
  74. }
  75. await db.table('users').where('id', user.id).update({ password: hash });
  76. return res.json({ success: true });
  77. });
  78. };
  79. module.exports = authController;