mirror of
https://gitlab.silvrtree.co.uk/martind2000/obrandDBWork.git
synced 2025-01-10 21:55:08 +00:00
174 lines
4.9 KiB
JavaScript
174 lines
4.9 KiB
JavaScript
'use strict';
|
|
|
|
//var logger = require('log4js').getLogger();
|
|
var mdValidator = require('node-validator'), mdErrors = require('./md-errors');
|
|
var bcrypt = require('bcrypt-as-promised');
|
|
var newId = require('uuid-pure').newId;
|
|
|
|
|
|
//var db = require('./db-connector').dbConnection;
|
|
|
|
module.exports = function(db) {
|
|
var module = {};
|
|
|
|
module.connectPGP= function() {
|
|
return new Promise(function(resolve, reject) {
|
|
db.query('select $1::int as number', [1])
|
|
.then((data)=>{
|
|
console.log(data);
|
|
resolve(data);
|
|
})
|
|
.catch((error)=>{
|
|
reject(error);
|
|
});
|
|
});
|
|
};
|
|
module.sqlInsertAccount= function(data) {
|
|
let _data = data;
|
|
_data.uid = newId();
|
|
return new Promise(function(resolve, reject) {
|
|
db.func('insert_user',[_data.username, _data.email, _data.hash, _data.uid])
|
|
.then(()=> {
|
|
return resolve('ok');
|
|
})
|
|
.catch((err)=> {
|
|
return reject(err);
|
|
});
|
|
});
|
|
};
|
|
module.sqlGetAccount= function(email) {
|
|
return new Promise(function(resolve, reject) {
|
|
db.oneOrNone("select * from logins where email=$1;",[email])
|
|
.then(function(d) {
|
|
return resolve(d);
|
|
})
|
|
.catch((err)=> {
|
|
return reject(err);
|
|
});
|
|
});
|
|
};
|
|
module.sqlGetAccountDetails= function(uid) {
|
|
return new Promise(function(resolve, reject) {
|
|
db.one("select * from logins where uid=$1;",[uid])
|
|
.then(function(d) {
|
|
return resolve(d);
|
|
})
|
|
.catch((err)=> {
|
|
return reject(err);
|
|
});
|
|
});
|
|
};
|
|
module.sqlUpsertProfile= function(data) {
|
|
var propArray=[data.uid ,data.forename, data.surname, data.gender, data.dob, data.bio];
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
db.func('upsert_profile',propArray)
|
|
.then(()=> {
|
|
return resolve(true);
|
|
})
|
|
.catch((err)=> {
|
|
return reject(err);
|
|
});
|
|
});
|
|
};
|
|
module.sqlGetProfile= function(uid) {
|
|
return new Promise(function(resolve, reject) {
|
|
db.oneOrNone("select * from profile where uid=$1;",[uid])
|
|
.then(function(d) {
|
|
return resolve(d);
|
|
})
|
|
.catch((err)=> {
|
|
return reject(err);
|
|
});
|
|
});
|
|
};
|
|
|
|
module.addNewAccount= function(data) {
|
|
var self = this;
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let _data = {};
|
|
_data.username = data.username.trim();
|
|
_data.password = data.password.trim();
|
|
_data.email = data.email.trim();
|
|
|
|
if (Object.keys(data).length === 3) {
|
|
|
|
if (_data.username.length === 0 || _data.password.length === 0 || _data.email.length === 0)
|
|
{
|
|
return reject(mdErrors.error(1000));
|
|
}
|
|
|
|
if (mdValidator.Email(data.email) === false)
|
|
{
|
|
return reject(mdErrors.error(1001));
|
|
} else
|
|
{
|
|
// It should be possible to insert the user now.
|
|
bcrypt.hash(data.password,10).then((d) => {
|
|
_data.hash = d;
|
|
self.sqlInsertAccount(_data)
|
|
.then(()=>{
|
|
return resolve({reply:'user added'});
|
|
})
|
|
.catch((err)=>{
|
|
return reject(err);
|
|
});
|
|
});
|
|
|
|
}
|
|
} else
|
|
{
|
|
// error - required details missing
|
|
return reject(mdErrors.error(1002));
|
|
|
|
}
|
|
});
|
|
};
|
|
module.findAccount= function(data) {
|
|
|
|
var _data = data;
|
|
return new Promise((resolve, reject) => {
|
|
this.sqlGetAccount(_data.email)
|
|
.then((row) => {
|
|
if (row === null) {
|
|
return reject(mdErrors.error(1004));
|
|
}
|
|
// check password against hash held in db
|
|
bcrypt.compare(_data.password, row.password_hash)
|
|
.then(function() {
|
|
let loginDetails = {
|
|
id: row.id,
|
|
username: row.username,
|
|
email: row.email,
|
|
uid: row.uid
|
|
};
|
|
return resolve(loginDetails);
|
|
})
|
|
// failure, reject
|
|
.catch(function() {
|
|
return reject(mdErrors.error(1004));
|
|
});
|
|
|
|
})
|
|
.catch(function(err){
|
|
return reject(err);
|
|
});
|
|
});
|
|
};
|
|
module.addInsertProfile= function(data) {
|
|
return new Promise((resolve, reject) => {
|
|
this.sqlUpsertProfile(data)
|
|
.then((d)=>{
|
|
return resolve(d);
|
|
})
|
|
.catch((err)=>{
|
|
return reject(err);
|
|
});
|
|
});
|
|
};
|
|
|
|
return module;
|
|
};
|
|
|