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('/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
|
||||||
|
|
|
@ -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])
|
||||||
if(result.length > 0) {
|
.then(result => {
|
||||||
const first = result[0];
|
if(result.length > 0) {
|
||||||
bcrypt.compare(password, first[2], function(err, res) {
|
const first = result[0];
|
||||||
if(res) {
|
const matches = bcrypt.compareSync(password, first[2]);
|
||||||
cb(null, { id: first[0], email: first[1], admin: first[3] })
|
if(matches) {
|
||||||
}
|
cb(null, { id: first[0], email: first[1], admin: first[3] })
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
|
{
|
||||||
|
cb(null, false)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
cb(null, false)
|
cb(null, false)
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
} else {
|
|
||||||
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]);
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
Reference in New Issue