colouring-montreal/app/src/user.js

90 lines
2.1 KiB
JavaScript
Raw Normal View History

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