Add function to create user

main
sudoer777 2021-11-24 18:13:33 -07:00
parent c5f05eebff
commit 7520580d66
3 changed files with 34 additions and 21 deletions

2
app.js
View File

@ -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

View File

@ -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]);
database.executeQuery(query, [email])
.then(result => {
if(result.length > 0) {
const first = result[0];
bcrypt.compare(password, first[2], function(err, res) {
if(res) {
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)
}
});
}));
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)]);
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]);
}

View File

@ -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);