authController.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. bcrypt.compare(password, user.password, (err, result) => {
  15. if (err) {
  16. console.log(err);
  17. return res.json({ success: false, description: 'There was an error' });
  18. }
  19. if (result === false) return res.json({ success: false, description: 'Wrong password' });
  20. return res.json({ success: true, token: user.token });
  21. });
  22. };
  23. authController.register = async (req, res, next) => {
  24. if (config.enableUserAccounts === false) {
  25. return res.json({ success: false, description: 'Register is disabled at the moment' });
  26. }
  27. const username = req.body.username;
  28. const password = req.body.password;
  29. if (username === undefined) return res.json({ success: false, description: 'No username provided' });
  30. if (password === undefined) return res.json({ success: false, description: 'No password provided' });
  31. if (username.length < 4 || username.length > 32) {
  32. return res.json({ success: false, description: 'Username must have 4-32 characters' })
  33. }
  34. if (password.length < 6 || password.length > 64) {
  35. return res.json({ success: false, description: 'Password must have 6-64 characters' })
  36. }
  37. const user = await db.table('users').where('username', username).first();
  38. if (user) return res.json({ success: false, description: 'Username already exists' });
  39. bcrypt.hash(password, 10, async (err, hash) => {
  40. if (err) {
  41. console.log(err);
  42. return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' });
  43. }
  44. const token = randomstring.generate(64);
  45. await db.table('users').insert({
  46. username: username,
  47. password: hash,
  48. token: token
  49. });
  50. return res.json({ success: true, token: token })
  51. });
  52. };
  53. authController.changePassword = async (req, res, next) => {
  54. const user = await utils.authorize(req, res);
  55. let password = req.body.password;
  56. if (password === undefined) return res.json({ success: false, description: 'No password provided' });
  57. if (password.length < 6 || password.length > 64) {
  58. return res.json({ success: false, description: 'Password must have 6-64 characters' });
  59. }
  60. bcrypt.hash(password, 10, async (err, hash) => {
  61. if (err) {
  62. console.log(err);
  63. return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' });
  64. }
  65. await db.table('users').where('id', user.id).update({ password: hash });
  66. return res.json({ success: true });
  67. });
  68. };
  69. module.exports = authController;