2018-09-30 14:49:07 -04:00
|
|
|
/**
|
|
|
|
* User data access
|
|
|
|
*
|
|
|
|
*/
|
2019-09-10 10:38:01 -04:00
|
|
|
import { errors } from 'pg-promise';
|
|
|
|
|
2019-08-14 05:54:13 -04:00
|
|
|
import db from '../../db';
|
2019-09-11 11:28:05 -04:00
|
|
|
import { validateUsername, ValidationError, validatePassword } from '../validation';
|
2019-10-21 10:19:35 -04:00
|
|
|
import { promisify } from 'util';
|
2018-09-09 17:22:44 -04:00
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
|
|
|
|
async function createUser(user) {
|
2019-09-11 11:28:05 -04:00
|
|
|
try {
|
|
|
|
validateUsername(user.username);
|
|
|
|
validatePassword(user.password);
|
|
|
|
} catch(err) {
|
|
|
|
if (err instanceof ValidationError) {
|
2019-10-21 10:19:35 -04:00
|
|
|
throw { error: err.message };
|
2019-09-11 11:28:05 -04:00
|
|
|
} else throw err;
|
2018-09-13 11:58:05 -04:00
|
|
|
}
|
2019-09-11 11:28:05 -04:00
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
try {
|
|
|
|
return await 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
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch(error) {
|
|
|
|
console.error('Error:', error);
|
2018-09-09 17:22:44 -04:00
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
if (error.detail.includes('already exists')) {
|
|
|
|
if (error.detail.includes('username')) {
|
2019-02-24 14:28:11 -05:00
|
|
|
return { error: 'Username already registered' };
|
2019-10-21 10:19:35 -04:00
|
|
|
} else if (error.detail.includes('email')) {
|
2019-02-24 14:28:11 -05:00
|
|
|
return { error: 'Email already registered' };
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-21 10:19:35 -04:00
|
|
|
return { error: 'Database error' };
|
|
|
|
}
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
async function authUser(username: string, password: string) {
|
|
|
|
try {
|
|
|
|
const user = await db.one(
|
|
|
|
`SELECT
|
|
|
|
user_id,
|
|
|
|
(
|
|
|
|
pass = crypt($2, pass)
|
|
|
|
) AS auth_ok
|
|
|
|
FROM users
|
|
|
|
WHERE
|
|
|
|
username = $1
|
|
|
|
`, [
|
|
|
|
username,
|
|
|
|
password
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
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-10-21 10:19:35 -04:00
|
|
|
} catch(err) {
|
2019-09-10 10:38:01 -04:00
|
|
|
if (err instanceof errors.QueryResultError) {
|
|
|
|
console.error(`Authentication failed for user ${username}`);
|
|
|
|
return { error: 'Username or password not recognised' };
|
|
|
|
}
|
|
|
|
console.error('Error:', err);
|
|
|
|
return { error: 'Database error' };
|
2019-10-21 10:19:35 -04:00
|
|
|
}
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
async function getUserById(id: string) {
|
|
|
|
try {
|
|
|
|
return await db.one(
|
|
|
|
`SELECT
|
|
|
|
username, email, registered, api_key
|
|
|
|
FROM
|
|
|
|
users
|
|
|
|
WHERE
|
|
|
|
user_id = $1
|
|
|
|
`, [
|
|
|
|
id
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch(error) {
|
2018-09-09 17:22:44 -04:00
|
|
|
console.error('Error:', error)
|
|
|
|
return undefined;
|
2019-10-21 10:19:35 -04:00
|
|
|
}
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
async function getUserByEmail(email: string) {
|
|
|
|
try {
|
|
|
|
return db.one(
|
|
|
|
`SELECT
|
|
|
|
user_id, username, email
|
|
|
|
FROM
|
|
|
|
users
|
|
|
|
WHERE
|
|
|
|
email = $1
|
|
|
|
`, [email]
|
|
|
|
);
|
|
|
|
} catch(error) {
|
2019-08-21 09:46:14 -04:00
|
|
|
console.error('Error:', error);
|
|
|
|
return undefined;
|
2019-10-21 10:19:35 -04:00
|
|
|
}
|
2019-08-21 09:46:14 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
async function getNewUserAPIKey(id: string) {
|
|
|
|
try{
|
|
|
|
return db.one(
|
|
|
|
`UPDATE
|
|
|
|
users
|
|
|
|
SET
|
|
|
|
api_key = gen_random_uuid()
|
|
|
|
WHERE
|
|
|
|
user_id = $1
|
|
|
|
RETURNING
|
|
|
|
api_key
|
|
|
|
`, [
|
|
|
|
id
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch(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.' };
|
2019-10-21 10:19:35 -04:00
|
|
|
}
|
2018-10-20 07:20:10 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
async function authAPIUser(key: string) {
|
|
|
|
try {
|
|
|
|
return await db.one(
|
|
|
|
`SELECT
|
|
|
|
user_id
|
|
|
|
FROM
|
|
|
|
users
|
|
|
|
WHERE
|
|
|
|
api_key = $1
|
|
|
|
`, [
|
|
|
|
key
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch(error) {
|
2018-10-20 07:59:17 -04:00
|
|
|
console.error('Error:', error)
|
|
|
|
return undefined;
|
2019-10-21 10:19:35 -04:00
|
|
|
}
|
2018-10-20 07:59:17 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
async function deleteUser(id: string) {
|
|
|
|
try {
|
|
|
|
return await db.none(
|
|
|
|
`UPDATE users
|
|
|
|
SET
|
|
|
|
email = null,
|
|
|
|
pass = null,
|
|
|
|
api_key = null,
|
|
|
|
username = concat('deleted_', cast(user_id as char(13))),
|
|
|
|
is_deleted = true,
|
|
|
|
deleted_on = now() at time zone 'utc'
|
|
|
|
WHERE user_id = $1
|
|
|
|
`, [id]
|
|
|
|
);
|
|
|
|
} catch(error) {
|
2019-08-15 11:12:01 -04:00
|
|
|
console.error('Error:', error);
|
|
|
|
return {error: 'Database error'};
|
2019-10-21 10:19:35 -04:00
|
|
|
}
|
2019-08-15 11:12:01 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:19:35 -04:00
|
|
|
function logout(session: Express.Session): Promise<void> {
|
|
|
|
session.user_id = undefined;
|
|
|
|
|
|
|
|
return promisify(session.destroy.bind(session))();
|
2019-08-19 09:31:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
getUserById,
|
2019-08-21 09:46:14 -04:00
|
|
|
getUserByEmail,
|
2019-08-19 09:31:35 -04:00
|
|
|
createUser,
|
|
|
|
authUser,
|
|
|
|
getNewUserAPIKey,
|
|
|
|
authAPIUser,
|
|
|
|
deleteUser,
|
|
|
|
logout
|
|
|
|
};
|