Add function to create user
parent
c5f05eebff
commit
7520580d66
2
app.js
2
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
|
||||
|
|
|
@ -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]);
|
||||
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]);
|
||||
}
|
|
@ -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);
|
||||
|
|
Reference in New Issue