diff --git a/app.js b/app.js index f657970..f8cb47b 100644 --- a/app.js +++ b/app.js @@ -48,7 +48,7 @@ app.use('/', indexRouter); app.use('/users', usersRouter); app.use('/data', dataRouter); app.use('/manage', manageRouter); -app.user('/auth', authRouter); +app.use('/auth', authRouter); // catch 404 and forward to error handler diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js index 67d277a..d65de3c 100644 --- a/database/accounts/accounts.js +++ b/database/accounts/accounts.js @@ -1,26 +1,28 @@ const database = require('./../database'); const passport = require('passport'); const passportLocal = require('passport-local'); +const bcrypt = require('bcrypt'); passport.use(new passportLocal.Strategy((email, password, cb) => { - query = `SELECT id, email, password, admin + query = `SELECT user_id, email, password, admin FROM accounts.users WHERE email = $1`; - const result = database.executeQuery(query, [email]); - if(result.length > 0) { - const first = result[0]; - bcrypt.compare(password, first[2], function(err, res) { - if(res) { - cb(null, { id: first[0], email: first[1], admin: first[3] }) - } - else - { + database.executeQuery(query, [email]) + .then(result => { + if(result.length > 0) { + const first = result[0]; + const matches = bcrypt.compareSync(password, first[2]); + if(matches) { + cb(null, { id: first[0], email: first[1], admin: first[3] }) + } + else + { + cb(null, false) + } + } else { cb(null, false) } - }) - } else { - cb(null, false) - } + }); })); passport.serializeUser((user, done) => { @@ -28,10 +30,22 @@ passport.serializeUser((user, done) => { }) passport.deserializeUser((id, cb) => { - query = `SELECT id, email, admin + query = `SELECT user_id, email, admin FROM accounts.users WHERE id = $1`; - const result = database.executeQuery(query, [parseInt(id, 10)]); - - cb(null, result[0]); -}); \ No newline at end of file + database.executeQuery(query, [parseInt(id, 10)]) + .then(result => { + cb(null, result[0]); + }); +}); + + + +async function createUser(email, password) { + const salt = bcrypt.genSaltSync(); + const hash = bcrypt.hashSync(password, salt); + + const query = `INSERT INTO accounts.users(email, password) + VALUES($1, $2)`; + await database.executeQuery(query, [email, hash]); +} \ No newline at end of file diff --git a/database/database.js b/database/database.js index f166980..1d5bc56 100644 --- a/database/database.js +++ b/database/database.js @@ -27,7 +27,6 @@ async function Initialize() { - async function checkForDatabaseInitialization() { const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`; let result = await executeQuery(query);