Add function to create user

This commit is contained in:
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('/users', usersRouter);
app.use('/data', dataRouter); app.use('/data', dataRouter);
app.use('/manage', manageRouter); app.use('/manage', manageRouter);
app.user('/auth', authRouter); app.use('/auth', authRouter);
// catch 404 and forward to error handler // catch 404 and forward to error handler

View file

@ -1,26 +1,28 @@
const database = require('./../database'); const database = require('./../database');
const passport = require('passport'); const passport = require('passport');
const passportLocal = require('passport-local'); const passportLocal = require('passport-local');
const bcrypt = require('bcrypt');
passport.use(new passportLocal.Strategy((email, password, cb) => { passport.use(new passportLocal.Strategy((email, password, cb) => {
query = `SELECT id, email, password, admin query = `SELECT user_id, email, password, admin
FROM accounts.users FROM accounts.users
WHERE email = $1`; WHERE email = $1`;
const result = database.executeQuery(query, [email]); database.executeQuery(query, [email])
.then(result => {
if(result.length > 0) { if(result.length > 0) {
const first = result[0]; const first = result[0];
bcrypt.compare(password, first[2], function(err, res) { const matches = bcrypt.compareSync(password, first[2]);
if(res) { if(matches) {
cb(null, { id: first[0], email: first[1], admin: first[3] }) cb(null, { id: first[0], email: first[1], admin: first[3] })
} }
else else
{ {
cb(null, false) cb(null, false)
} }
})
} else { } else {
cb(null, false) cb(null, false)
} }
});
})); }));
passport.serializeUser((user, done) => { passport.serializeUser((user, done) => {
@ -28,10 +30,22 @@ passport.serializeUser((user, done) => {
}) })
passport.deserializeUser((id, cb) => { passport.deserializeUser((id, cb) => {
query = `SELECT id, email, admin query = `SELECT user_id, email, admin
FROM accounts.users FROM accounts.users
WHERE id = $1`; WHERE id = $1`;
const result = database.executeQuery(query, [parseInt(id, 10)]); database.executeQuery(query, [parseInt(id, 10)])
.then(result => {
cb(null, result[0]); 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() { async function checkForDatabaseInitialization() {
const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`; const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`;
let result = await executeQuery(query); let result = await executeQuery(query);