retropilot-server/controllers/users.js

110 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-01-12 08:02:30 -07:00
import crypto from 'crypto';
2022-01-21 16:36:48 -07:00
import log4js from 'log4js';
2022-01-12 08:02:30 -07:00
import orm from '../models/index.model';
2021-10-25 15:56:40 -06:00
2022-01-21 16:36:48 -07:00
const logger = log4js.getLogger('default');
export async function getAccountFromId(id) {
2022-01-08 17:35:40 -07:00
return orm.models.accounts.findByPk(id);
2021-08-14 16:11:32 -06:00
}
2022-01-21 16:36:48 -07:00
export async function getAccountFromEmail(email) {
2022-01-09 19:49:24 -07:00
if (!email) return null;
const account = orm.models.accounts.findOne({ where: { email } });
if (account.dataValues) return account.dataValues;
return null;
}
2022-03-02 20:37:16 -07:00
export async function createBaseAccount() {
await orm.models.accounts.create({
id: 0,
email: 'dummy@retropilot.org',
password: '123123',
created: Date.now(),
last_ping: Date.now(),
email_verify_token: 'notokenplease',
});
return { success: true, status: 200 };
}
2022-03-02 19:18:07 -07:00
export async function _dirtyCreateAccount(email, password, created, admin) {
2022-03-02 19:29:13 -07:00
logger.log('creating acount: ', email, password, created, admin);
2022-01-09 19:49:24 -07:00
return orm.models.accounts.create({
2022-03-02 19:18:07 -07:00
email, password, created, admin,
2022-01-09 19:49:24 -07:00
});
}
2022-01-21 16:36:48 -07:00
export async function createAccount(email, password) {
2022-01-08 17:35:40 -07:00
if (!email || !password) {
return { success: false, status: 400, data: { missingData: true } };
}
if (!process.env.ALLOW_REGISTRATION) {
2022-01-07 18:35:55 -07:00
return { success: false, status: 403, data: { registerEnabled: false } };
}
2022-01-08 17:35:40 -07:00
const emailToken = crypto.createHmac('sha256', process.env.APP_SALT).update(email.trim()).digest('hex');
password = crypto.createHash('sha256').update(password + process.env.APP_SALT).digest('hex');
2022-01-07 18:35:55 -07:00
2022-01-08 17:35:40 -07:00
const account = await orm.models.accounts.findOne({ where: { email } });
2022-01-07 18:35:55 -07:00
if (account != null && account.dataValues != null) {
return { success: true, status: 409, data: { alreadyRegistered: true } };
}
2022-01-08 17:35:40 -07:00
await orm.models.accounts.create({
2022-01-07 18:35:55 -07:00
email,
password,
created: Date.now(),
last_ping: Date.now(),
2022-01-08 13:43:57 -07:00
email_verify_token: emailToken,
2022-01-07 18:35:55 -07:00
});
2022-01-08 17:35:40 -07:00
const didAccountRegister = await orm.models.accounts.findOne({ where: { email } });
2022-01-07 18:35:55 -07:00
if (didAccountRegister != null && didAccountRegister.dataValues != null) {
return { success: true, status: 200 };
}
2022-01-08 17:35:40 -07:00
// TODO: better error
return { success: false, status: 500, data: {} };
}
2022-01-21 16:36:48 -07:00
export async function verifyEmailToken(token) {
2022-01-08 17:35:40 -07:00
if (!token) {
return { success: false, status: 400, data: { missingToken: true } };
}
const account = await orm.models.accounts.findOne(
2022-01-08 13:43:57 -07:00
{ where: { email_verify_token: token } },
2022-01-07 18:35:55 -07:00
);
2022-01-08 17:35:40 -07:00
if (account === null) {
return { success: false, status: 404, data: { badToken: true } };
}
2022-01-07 18:35:55 -07:00
if (account.verified === 1) {
2022-01-08 19:22:44 -07:00
return { success: true, status: 409, data: { alreadyVerified: true } };
2022-01-07 18:35:55 -07:00
}
2022-01-08 17:35:40 -07:00
await orm.models.accounts.update(
2022-01-08 19:22:44 -07:00
{ verified: true },
{ where: { id: account.id } },
2022-01-07 18:35:55 -07:00
);
2022-01-07 18:35:55 -07:00
return { success: true, status: 200, data: { successfullyVerified: true } };
}
2022-01-21 16:36:48 -07:00
export async function getAllUsers() {
2022-01-08 17:35:40 -07:00
return orm.models.accounts.findAll({ attributes: ['id', 'last_ping', 'created', 'admin', 'banned'] });
}
2022-01-12 08:02:30 -07:00
export default {
2022-01-07 18:35:55 -07:00
createAccount,
2022-03-02 20:37:16 -07:00
createBaseAccount,
2022-01-07 18:35:55 -07:00
verifyEmailToken,
getAccountFromId,
2022-01-08 13:43:57 -07:00
getAllUsers,
2022-01-09 19:49:24 -07:00
getAccountFromEmail,
_dirtyCreateAccount,
2022-01-07 18:35:55 -07:00
};