Update user db access

This commit is contained in:
Tom Russell 2018-09-30 19:49:07 +01:00
parent d0755e61de
commit e3c02612cd

View File

@ -1,4 +1,8 @@
import { query } from './db'; /**
* User data access
*
*/
import db from './db';
function createUser(user) { function createUser(user) {
@ -8,7 +12,7 @@ function createUser(user) {
if (user.password.length > 70) { if (user.password.length > 70) {
return Promise.reject({error: 'Password must be at most 70 characters'}) return Promise.reject({error: 'Password must be at most 70 characters'})
} }
return query( return db.one(
`INSERT `INSERT
INTO users ( INTO users (
user_id, user_id,
@ -26,9 +30,7 @@ function createUser(user) {
user.email, user.email,
user.password user.password
] ]
).then(function(data){ ).catch(function(error){
return data.rows[0];
}).catch(function(error){
console.error('Error:', error) console.error('Error:', error)
if (error.detail.indexOf('already exists') !== -1){ if (error.detail.indexOf('already exists') !== -1){
@ -43,7 +45,7 @@ function createUser(user) {
} }
function authUser(username, password) { function authUser(username, password) {
return query( return db.one(
`SELECT `SELECT
user_id, user_id,
( (
@ -56,8 +58,7 @@ function authUser(username, password) {
username, username,
password password
] ]
).then(function(data){ ).then(function(user){
const user = data.rows[0];
if (user && user.auth_ok) { if (user && user.auth_ok) {
return {user_id: user.user_id} return {user_id: user.user_id}
} else { } else {
@ -70,7 +71,7 @@ function authUser(username, password) {
} }
function getUserById(user_id) { function getUserById(user_id) {
return query( return db.one(
`SELECT `SELECT
username, email, registered username, email, registered
FROM users FROM users
@ -79,9 +80,7 @@ function getUserById(user_id) {
`, [ `, [
user_id user_id
] ]
).then(function(data){ ).catch(function(error){
return data.rows[0];
}).catch(function(error){
console.error('Error:', error) console.error('Error:', error)
return undefined; return undefined;
}); });