pixelplanet/src/routes/api/auth/register.js

118 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2020-01-02 16:58:06 +00:00
import Sequelize from 'sequelize';
import logger from '../../../core/logger';
2022-06-19 21:19:10 +00:00
import { RegUser } from '../../../data/sql';
2022-09-09 22:35:28 +00:00
import mailProvider from '../../../core/MailProvider';
2020-01-02 16:58:06 +00:00
import getMe from '../../../core/me';
import { getIPFromRequest, getHostFromRequest } from '../../../utils/ip';
2022-09-17 15:52:59 +00:00
import { checkIfMailDisposable } from '../../../core/isAllowed';
2020-01-06 11:29:33 +00:00
import {
validateEMail,
validateName,
validatePassword,
} from '../../../utils/validation';
import {
checkCaptchaSolution,
} from '../../../data/redis/captcha';
2020-01-02 16:58:06 +00:00
async function validate(email, name, password, captcha, captchaid, t, gettext) {
2020-01-02 16:58:06 +00:00
const errors = [];
2021-01-30 21:36:53 +00:00
const emailerror = gettext(validateEMail(email));
2022-09-17 15:52:59 +00:00
if (emailerror) {
errors.push(emailerror);
} else if (await checkIfMailDisposable(email)) {
2022-09-17 00:33:15 +00:00
errors.push(t`This email provider is not allowed`);
}
2020-01-02 16:58:06 +00:00
const nameerror = validateName(name);
if (nameerror) errors.push(nameerror);
2021-01-30 12:32:46 +00:00
const passworderror = gettext(validatePassword(password));
2020-01-02 16:58:06 +00:00
if (passworderror) errors.push(passworderror);
if (!captcha || !captchaid) errors.push(t`No Captcha given`);
2020-01-02 16:58:06 +00:00
let reguser = await RegUser.findOne({ where: { email } });
2021-01-30 21:36:53 +00:00
if (reguser) errors.push(t`E-Mail already in use.`);
2020-01-02 16:58:06 +00:00
reguser = await RegUser.findOne({ where: { name } });
2021-01-30 21:36:53 +00:00
if (reguser) errors.push(t`Username already in use.`);
2020-01-02 16:58:06 +00:00
return errors;
}
export default async (req, res) => {
const {
email, name, password, captcha, captchaid,
} = req.body;
2021-01-30 12:32:46 +00:00
const { t, gettext } = req.ttag;
const errors = await validate(
email, name, password, captcha, captchaid, t, gettext,
);
const ip = getIPFromRequest(req);
if (!errors.length) {
const captchaPass = await checkCaptchaSolution(
captcha, ip, true, captchaid,
);
switch (captchaPass) {
case 0:
break;
case 1:
errors.push(t`You took too long, try again.`);
break;
case 2:
errors.push(t`You failed your captcha`);
break;
default:
errors.push(t`Unknown Captcha Error`);
break;
}
}
2020-01-02 16:58:06 +00:00
if (errors.length > 0) {
res.status(400);
res.json({
errors,
});
return;
}
const newuser = await RegUser.create({
email,
name,
password,
verificationReqAt: Sequelize.literal('CURRENT_TIMESTAMP'),
lastLogIn: Sequelize.literal('CURRENT_TIMESTAMP'),
});
if (!newuser) {
res.status(500);
res.json({
2021-01-30 12:32:46 +00:00
errors: [t`Failed to create new user :(`],
2020-01-02 16:58:06 +00:00
});
return;
}
logger.info(`Created new user ${name} ${email} ${ip}`);
2021-01-30 12:32:46 +00:00
const { user, lang } = req;
2020-11-07 15:37:52 +00:00
user.setRegUser(newuser);
2021-01-30 12:32:46 +00:00
const me = await getMe(user, lang);
2020-01-02 16:58:06 +00:00
await req.logIn(user, (err) => {
if (err) {
2022-06-20 22:35:01 +00:00
logger.warn(`Login after register error: ${err.message}`);
2020-01-02 16:58:06 +00:00
res.status(500);
res.json({
2021-01-30 12:32:46 +00:00
errors: [t`Failed to establish session after register :(`],
2020-01-02 16:58:06 +00:00
});
return;
}
const host = getHostFromRequest(req);
2021-01-30 12:32:46 +00:00
mailProvider.sendVerifyMail(email, name, host, lang);
2020-01-02 16:58:06 +00:00
res.status(200);
res.json({
success: true,
me,
});
});
};