|
1 |
| -const db = require('../postgres'); // db module |
2 |
| -const bcrypt = require('bcrypt'); |
| 1 | +import bcrypt from 'bcrypt'; |
| 2 | +import models from '../models/index.js'; |
3 | 3 |
|
4 |
| -const authController = {}; |
5 |
| - |
6 |
| -authController.createAccount = async (req, res, next) => { |
7 |
| - // console.log('inside create account'); |
8 |
| - try { |
9 |
| - const { username, password } = req.body; |
10 |
| - const checkQuery = 'SELECT username FROM users WHERE username = $1'; |
11 |
| - await db.query(checkQuery, [username], async (err, user) => { |
12 |
| - if (user.rowCount > 0) { |
13 |
| - res.locals.isVerified = false; |
14 |
| - return next(); |
15 |
| - } else if (err) { |
16 |
| - return next({ |
17 |
| - log: 'Error occurred during create account.', |
18 |
| - status: 400, |
19 |
| - message: { err: 'Error occurred during create account.', err }, |
20 |
| - }); |
21 |
| - } else { |
22 |
| - const passHash = await bcrypt.hash(password, 10); |
23 |
| - const newUser = |
24 |
| - 'INSERT INTO users (username, password) VALUES ($1, $2)'; |
25 |
| - await db.query(newUser, [username, passHash], (err, user) => { |
26 |
| - if (err) { |
27 |
| - return next({ |
28 |
| - log: 'Error occurred when creating new account.', |
29 |
| - status: 401, |
30 |
| - message: { |
31 |
| - err: 'Error occurred when creating new account.', |
32 |
| - err, |
33 |
| - }, |
34 |
| - }); |
35 |
| - } else { |
36 |
| - res.locals.isVerified = true; |
37 |
| - res.locals.cookieID = username; |
38 |
| - return next(); |
39 |
| - } |
40 |
| - }); |
41 |
| - } |
42 |
| - }); |
43 |
| - } catch (err) { |
44 |
| - return next({ |
45 |
| - log: 'Error occurred during create account.', |
46 |
| - status: 400, |
47 |
| - message: { err: 'Error occurred during create account.', err }, |
| 4 | +const authController = { |
| 5 | + createAccount: async (req, res, next) => { |
| 6 | + const password = await bcrypt.hash(req.body.password, 10).catch(next); |
| 7 | + await models.User.create({ |
| 8 | + username: req.body.username, |
| 9 | + password: password, |
| 10 | + }).catch((error) => { |
| 11 | + next({ |
| 12 | + log: 'Error occurred during create account.', |
| 13 | + status: 400, |
| 14 | + message: { err: 'Error occurred during create account.', error }, |
| 15 | + }); |
48 | 16 | });
|
49 |
| - } |
| 17 | + return next(); |
| 18 | + }, |
| 19 | + verifyUser: async (req, res, next) => { |
| 20 | + return next(); |
| 21 | + }, |
| 22 | + addBrokers: async (req, res, next) => { |
| 23 | + return next(); |
| 24 | + }, |
| 25 | + addConnectionString: async (req, res, next) => { |
| 26 | + return next(); |
| 27 | + }, |
50 | 28 | };
|
51 | 29 |
|
52 |
| -authController.verifyUser = async (req, res, next) => { |
53 |
| - try { |
54 |
| - const { username, password } = req.body; |
55 |
| - let hashedPassword = await db.query( |
56 |
| - 'SELECT password FROM users WHERE username = $1', |
57 |
| - [username] |
58 |
| - ); |
59 |
| - hashedPassword = hashedPassword.rows[0].password; |
60 |
| - const matched = await bcrypt.compare(password, hashedPassword); |
61 |
| - if (!matched) { |
62 |
| - res.locals.isVerified = false; |
63 |
| - return next(); |
64 |
| - } else { |
65 |
| - res.locals.isVerified = true; |
66 |
| - res.locals.cookieID = username; |
67 |
| - return next(); |
68 |
| - } |
69 |
| - } catch (err) { |
70 |
| - // console.log('catch in verify user') |
71 |
| - return next({ |
72 |
| - log: 'Error inside verify user.', |
73 |
| - status: 401, |
74 |
| - message: { err: 'Error inside verify user.', err }, |
75 |
| - }); |
76 |
| - } |
77 |
| -}; |
| 30 | +// authController.createAccount = async (req, res, next) => { |
| 31 | +// if (req.body.username && req.body.password) { |
| 32 | +// try { |
| 33 | +// const password = await bcrypt.hash(req.body.password, 10); |
| 34 | +// const { username } = req.body; |
| 35 | +// await User.create({ |
| 36 | +// username, |
| 37 | +// password, |
| 38 | +// }); |
| 39 | +// return next(); |
| 40 | +// } catch (err) { |
| 41 | +// console.log('ERROR: ', err); |
| 42 | +// return next({ |
| 43 | +// log: 'Error occurred during create account.', |
| 44 | +// status: 400, |
| 45 | +// message: { err: 'Error occurred during create account.', err }, |
| 46 | +// }); |
| 47 | +// } |
| 48 | +// } |
| 49 | +// return next({ |
| 50 | +// log: 'No username/password provided.', |
| 51 | +// status: 400, |
| 52 | +// message: { err: 'No username/password provided.' }, |
| 53 | +// }); |
| 54 | +// }; |
78 | 55 |
|
79 |
| -authController.addBrokers = async (req, res, next) => { |
80 |
| - console.log('inside addBrokers'); |
81 |
| - try { |
82 |
| - const { idsArray, username } = req.body; |
83 |
| - console.log('req.body in addBrokers... ', req.body); |
84 |
| - const queryString = ` |
85 |
| - UPDATE users |
86 |
| - SET broker_ids = $1 |
87 |
| - WHERE username = $2 |
88 |
| - `; |
89 |
| - let inserted = await db.query(queryString, [idsArray, username]); |
90 |
| - console.log('inserted.. ', inserted); |
91 |
| - return next(); |
92 |
| - } catch (err) { |
93 |
| - // console.log('catch in verify user') |
94 |
| - return next({ |
95 |
| - log: 'Error inside add Brokers.', |
96 |
| - status: 401, |
97 |
| - message: { err: 'Unable to add brokers to database table users.', err }, |
98 |
| - }); |
99 |
| - } |
100 |
| -}; |
| 56 | +// authController.verifyUser = async (req, res, next) => { |
| 57 | +// if (req.body.username && req.body.password) { |
| 58 | +// try { |
| 59 | +// const { username, password } = req.body; |
| 60 | +// const user = await User.findOne({ where: { username } }); |
| 61 | +// const badInput = { |
| 62 | +// log: 'Incorrect username/password combination.', |
| 63 | +// status: 400, |
| 64 | +// message: { err: 'Incorrect username/password combination.' }, |
| 65 | +// }; |
| 66 | +// if (!user) return next(badInput); |
| 67 | +// const matched = await bcrypt.compare(password, user.password); |
| 68 | +// if (!matched) return next(badInput); |
| 69 | +// return next(); |
| 70 | +// } catch (err) { |
| 71 | +// return next({ |
| 72 | +// log: 'Error verifying user', |
| 73 | +// status: 400, |
| 74 | +// message: { err: 'Error verifying user', err }, |
| 75 | +// }); |
| 76 | +// } |
| 77 | +// } |
| 78 | +// }; |
101 | 79 |
|
102 |
| -module.exports = authController; |
| 80 | +// authController.addBrokers = async (req, res, next) => { |
| 81 | +// // console.log('inside addBrokers'); |
| 82 | +// // try { |
| 83 | +// // const { idsArray, username } = req.body; |
| 84 | +// // console.log('req.body in addBrokers... ', req.body); |
| 85 | +// // const queryString = ` |
| 86 | +// // UPDATE users |
| 87 | +// // SET broker_ids = $1 |
| 88 | +// // WHERE username = $2 |
| 89 | +// // `; |
| 90 | +// // let inserted = await db.query(queryString, [idsArray, username]); |
| 91 | +// // console.log('inserted.. ', inserted); |
| 92 | +// // return next(); |
| 93 | +// // } catch (err) { |
| 94 | +// // return next({ |
| 95 | +// // log: 'Error inside add Brokers.', |
| 96 | +// // status: 401, |
| 97 | +// // message: { err: 'Unable to add brokers to database table users.', err }, |
| 98 | +// // }); |
| 99 | +// // } |
| 100 | +// return next(); |
| 101 | +// }; |
| 102 | +export default authController; |
| 103 | +// module.exports = authController; |
0 commit comments