2018-09-30 14:49:07 -04:00
|
|
|
/**
|
|
|
|
* User data access
|
|
|
|
*
|
|
|
|
*/
|
2019-02-24 07:17:59 -05:00
|
|
|
import db from '../db';
|
2018-09-09 17:22:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
function createUser(user) {
|
2018-09-13 11:58:05 -04:00
|
|
|
if (!user.password || user.password.length < 8) {
|
2019-02-24 14:28:11 -05:00
|
|
|
return Promise.reject({ error: 'Password must be at least 8 characters' })
|
2018-09-13 11:58:05 -04:00
|
|
|
}
|
|
|
|
if (user.password.length > 70) {
|
2019-02-24 14:28:11 -05:00
|
|
|
return Promise.reject({ error: 'Password must be at most 70 characters' })
|
2018-09-13 11:58:05 -04:00
|
|
|
}
|
2018-09-30 14:49:07 -04:00
|
|
|
return db.one(
|
2018-09-09 17:22:44 -04:00
|
|
|
`INSERT
|
|
|
|
INTO users (
|
|
|
|
user_id,
|
|
|
|
username,
|
|
|
|
email,
|
|
|
|
pass
|
|
|
|
) VALUES (
|
|
|
|
gen_random_uuid(),
|
|
|
|
$1,
|
|
|
|
$2,
|
|
|
|
crypt($3, gen_salt('bf'))
|
|
|
|
) RETURNING user_id
|
|
|
|
`, [
|
2019-02-24 14:28:11 -05:00
|
|
|
user.username,
|
|
|
|
user.email,
|
|
|
|
user.password
|
2018-09-09 17:22:44 -04:00
|
|
|
]
|
2019-02-24 14:28:11 -05:00
|
|
|
).catch(function (error) {
|
2018-09-09 17:22:44 -04:00
|
|
|
console.error('Error:', error)
|
|
|
|
|
2019-02-24 14:28:11 -05:00
|
|
|
if (error.detail.indexOf('already exists') !== -1) {
|
|
|
|
if (error.detail.indexOf('username') !== -1) {
|
|
|
|
return { error: 'Username already registered' };
|
2018-09-09 17:22:44 -04:00
|
|
|
} else if (error.detail.indexOf('email') !== -1) {
|
2019-02-24 14:28:11 -05:00
|
|
|
return { error: 'Email already registered' };
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
}
|
2019-02-24 14:28:11 -05:00
|
|
|
return { error: 'Database error' }
|
2018-09-09 17:22:44 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function authUser(username, password) {
|
2018-09-30 14:49:07 -04:00
|
|
|
return db.one(
|
2018-09-09 17:22:44 -04:00
|
|
|
`SELECT
|
|
|
|
user_id,
|
|
|
|
(
|
|
|
|
pass = crypt($2, pass)
|
|
|
|
) AS auth_ok
|
|
|
|
FROM users
|
|
|
|
WHERE
|
|
|
|
username = $1
|
|
|
|
`, [
|
2019-02-24 14:28:11 -05:00
|
|
|
username,
|
|
|
|
password
|
2018-09-09 17:22:44 -04:00
|
|
|
]
|
2019-02-24 14:28:11 -05:00
|
|
|
).then(function (user) {
|
2018-09-13 11:58:05 -04:00
|
|
|
if (user && user.auth_ok) {
|
2019-02-24 14:28:11 -05:00
|
|
|
return { user_id: user.user_id }
|
2018-09-09 17:22:44 -04:00
|
|
|
} else {
|
2019-02-24 14:28:11 -05:00
|
|
|
return { error: 'Username or password not recognised' }
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
2019-02-24 14:28:11 -05:00
|
|
|
}).catch(function (err) {
|
2018-09-09 17:22:44 -04:00
|
|
|
console.error(err);
|
2019-02-24 14:28:11 -05:00
|
|
|
return { error: 'Username or password not recognised' };
|
2018-09-09 17:22:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUserById(user_id) {
|
2018-09-30 14:49:07 -04:00
|
|
|
return db.one(
|
2018-09-09 17:22:44 -04:00
|
|
|
`SELECT
|
2018-10-20 07:20:10 -04:00
|
|
|
username, email, registered, api_key
|
2018-10-20 07:59:17 -04:00
|
|
|
FROM
|
|
|
|
users
|
2018-09-09 17:22:44 -04:00
|
|
|
WHERE
|
2018-10-20 07:59:17 -04:00
|
|
|
user_id = $1
|
2018-09-09 17:22:44 -04:00
|
|
|
`, [
|
|
|
|
user_id
|
|
|
|
]
|
2019-02-24 14:28:11 -05:00
|
|
|
).catch(function (error) {
|
2018-09-09 17:22:44 -04:00
|
|
|
console.error('Error:', error)
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:20:10 -04:00
|
|
|
function getNewUserAPIKey(user_id) {
|
|
|
|
return db.one(
|
|
|
|
`UPDATE
|
|
|
|
users
|
|
|
|
SET
|
|
|
|
api_key = gen_random_uuid()
|
|
|
|
WHERE
|
|
|
|
user_id = $1
|
|
|
|
RETURNING
|
|
|
|
api_key
|
|
|
|
`, [
|
|
|
|
user_id
|
|
|
|
]
|
2019-02-24 14:28:11 -05:00
|
|
|
).catch(function (error) {
|
2018-10-20 07:20:10 -04:00
|
|
|
console.error('Error:', error)
|
2019-02-24 14:28:11 -05:00
|
|
|
return { error: 'Failed to generate new API key.' };
|
2018-10-20 07:20:10 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:59:17 -04:00
|
|
|
function authAPIUser(api_key) {
|
|
|
|
return db.one(
|
|
|
|
`SELECT
|
|
|
|
user_id
|
|
|
|
FROM
|
|
|
|
users
|
|
|
|
WHERE
|
|
|
|
api_key = $1
|
|
|
|
`, [
|
|
|
|
api_key
|
|
|
|
]
|
2019-02-24 14:28:11 -05:00
|
|
|
).catch(function (error) {
|
2018-10-20 07:59:17 -04:00
|
|
|
console.error('Error:', error)
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { getUserById, createUser, authUser, getNewUserAPIKey, authAPIUser }
|