mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 21:27:23 +01:00
start upgrading eslint
This commit is contained in:
@@ -12,7 +12,7 @@ import {
|
||||
} from '../../libs/errors';
|
||||
import * as passwordUtils from '../../libs/password';
|
||||
import { sendTxn as sendTxnEmail } from '../../libs/email';
|
||||
import { validatePasswordResetCodeAndFindUser, convertToBcrypt} from '../../libs/password';
|
||||
import { validatePasswordResetCodeAndFindUser, convertToBcrypt } from '../../libs/password';
|
||||
import { encrypt } from '../../libs/encryption';
|
||||
import {
|
||||
loginRes,
|
||||
@@ -20,12 +20,12 @@ import {
|
||||
loginSocial,
|
||||
registerLocal,
|
||||
} from '../../libs/auth';
|
||||
import {verifyUsername} from '../../libs/user/validation';
|
||||
import { verifyUsername } from '../../libs/user/validation';
|
||||
|
||||
const BASE_URL = nconf.get('BASE_URL');
|
||||
const TECH_ASSISTANCE_EMAIL = nconf.get('EMAILS_TECH_ASSISTANCE_EMAIL');
|
||||
|
||||
let api = {};
|
||||
const api = {};
|
||||
|
||||
/**
|
||||
* @api {post} /api/v3/user/auth/local/register Register
|
||||
@@ -79,24 +79,24 @@ api.loginLocal = {
|
||||
errorMessage: res.t('missingPassword'),
|
||||
},
|
||||
});
|
||||
let validationErrors = req.validationErrors();
|
||||
const validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
req.sanitizeBody('username').trim();
|
||||
req.sanitizeBody('password').trim();
|
||||
|
||||
let login;
|
||||
let username = req.body.username;
|
||||
let password = req.body.password;
|
||||
const { username } = req.body;
|
||||
const { password } = req.body;
|
||||
|
||||
if (validator.isEmail(String(username))) {
|
||||
login = {'auth.local.email': username.toLowerCase()}; // Emails are stored lowercase
|
||||
login = { 'auth.local.email': username.toLowerCase() }; // Emails are stored lowercase
|
||||
} else {
|
||||
login = {'auth.local.username': username};
|
||||
login = { 'auth.local.username': username };
|
||||
}
|
||||
|
||||
// load the entire user because we may have to save it to convert the password to bcrypt
|
||||
let user = await User.findOne(login).exec();
|
||||
const user = await User.findOne(login).exec();
|
||||
|
||||
// if user is using social login, then user will not have a hashed_password stored
|
||||
if (!user || !user.auth.local.hashed_password) throw new NotAuthorized(res.t('invalidLoginCredentialsLong'));
|
||||
@@ -150,17 +150,17 @@ api.loginSocial = {
|
||||
* @apiParam (Body) {String} username The new username
|
||||
|
||||
* @apiSuccess {String} data.username The new username
|
||||
**/
|
||||
* */
|
||||
api.updateUsername = {
|
||||
method: 'PUT',
|
||||
middlewares: [authWithHeaders()],
|
||||
url: '/user/auth/update-username',
|
||||
async handler (req, res) {
|
||||
const user = res.locals.user;
|
||||
const { user } = res.locals;
|
||||
|
||||
req.checkBody({
|
||||
username: {
|
||||
notEmpty: {errorMessage: res.t('missingUsername')},
|
||||
notEmpty: { errorMessage: res.t('missingUsername') },
|
||||
},
|
||||
});
|
||||
|
||||
@@ -172,13 +172,13 @@ api.updateUsername = {
|
||||
const issues = verifyUsername(newUsername, res);
|
||||
if (issues.length > 0) throw new BadRequest(issues.join(' '));
|
||||
|
||||
const password = req.body.password;
|
||||
const { password } = req.body;
|
||||
if (password !== undefined) {
|
||||
let isValidPassword = await passwordUtils.compare(user, password);
|
||||
const isValidPassword = await passwordUtils.compare(user, password);
|
||||
if (!isValidPassword) throw new NotAuthorized(res.t('wrongPassword'));
|
||||
}
|
||||
|
||||
const existingUser = await User.findOne({ 'auth.local.lowerCaseUsername': newUsername.toLowerCase() }, {auth: 1}).exec();
|
||||
const existingUser = await User.findOne({ 'auth.local.lowerCaseUsername': newUsername.toLowerCase() }, { auth: 1 }).exec();
|
||||
if (existingUser !== undefined && existingUser !== null && existingUser._id !== user._id) {
|
||||
throw new BadRequest(res.t('usernameTaken'));
|
||||
}
|
||||
@@ -224,39 +224,39 @@ api.updateUsername = {
|
||||
* @apiParam (Body) {String} confirmPassword New password confirmation
|
||||
*
|
||||
* @apiSuccess {Object} data An empty object
|
||||
**/
|
||||
* */
|
||||
api.updatePassword = {
|
||||
method: 'PUT',
|
||||
middlewares: [authWithHeaders()],
|
||||
url: '/user/auth/update-password',
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
const { user } = res.locals;
|
||||
|
||||
if (!user.auth.local.hashed_password) throw new BadRequest(res.t('userHasNoLocalRegistration'));
|
||||
|
||||
req.checkBody({
|
||||
password: {
|
||||
notEmpty: {errorMessage: res.t('missingPassword')},
|
||||
notEmpty: { errorMessage: res.t('missingPassword') },
|
||||
},
|
||||
newPassword: {
|
||||
notEmpty: {errorMessage: res.t('missingNewPassword')},
|
||||
notEmpty: { errorMessage: res.t('missingNewPassword') },
|
||||
},
|
||||
confirmPassword: {
|
||||
notEmpty: {errorMessage: res.t('missingNewPassword')},
|
||||
notEmpty: { errorMessage: res.t('missingNewPassword') },
|
||||
},
|
||||
});
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
const validationErrors = req.validationErrors();
|
||||
|
||||
if (validationErrors) {
|
||||
throw validationErrors;
|
||||
}
|
||||
|
||||
let oldPassword = req.body.password;
|
||||
let isValidPassword = await passwordUtils.compare(user, oldPassword);
|
||||
const oldPassword = req.body.password;
|
||||
const isValidPassword = await passwordUtils.compare(user, oldPassword);
|
||||
if (!isValidPassword) throw new NotAuthorized(res.t('wrongPassword'));
|
||||
|
||||
let newPassword = req.body.newPassword;
|
||||
const { newPassword } = req.body;
|
||||
if (newPassword !== req.body.confirmPassword) throw new NotAuthorized(res.t('passwordConfirmationMatch'));
|
||||
|
||||
// set new password and make sure it's using bcrypt for hashing
|
||||
@@ -276,7 +276,7 @@ api.updatePassword = {
|
||||
* @apiParam (Body) {String} email The email address of the user
|
||||
*
|
||||
* @apiSuccess {String} message The localized success message
|
||||
**/
|
||||
* */
|
||||
api.resetPassword = {
|
||||
method: 'POST',
|
||||
middlewares: [],
|
||||
@@ -284,14 +284,14 @@ api.resetPassword = {
|
||||
async handler (req, res) {
|
||||
req.checkBody({
|
||||
email: {
|
||||
notEmpty: {errorMessage: res.t('missingEmail')},
|
||||
notEmpty: { errorMessage: res.t('missingEmail') },
|
||||
},
|
||||
});
|
||||
let validationErrors = req.validationErrors();
|
||||
const validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
let email = req.body.email.toLowerCase();
|
||||
let user = await User.findOne({ 'auth.local.email': email }).exec();
|
||||
const email = req.body.email.toLowerCase();
|
||||
const user = await User.findOne({ 'auth.local.email': email }).exec();
|
||||
|
||||
if (user) {
|
||||
// create an encrypted link to be used to reset the password
|
||||
@@ -299,12 +299,12 @@ api.resetPassword = {
|
||||
userId: user._id,
|
||||
expiresAt: moment().add({ hours: 24 }),
|
||||
}));
|
||||
let link = `${BASE_URL}/static/user/auth/local/reset-password-set-new-one?code=${passwordResetCode}`;
|
||||
const link = `${BASE_URL}/static/user/auth/local/reset-password-set-new-one?code=${passwordResetCode}`;
|
||||
|
||||
user.auth.local.passwordResetCode = passwordResetCode;
|
||||
|
||||
sendTxnEmail(user, 'reset-password', [
|
||||
{name: 'PASSWORD_RESET_LINK', content: link},
|
||||
{ name: 'PASSWORD_RESET_LINK', content: link },
|
||||
]);
|
||||
|
||||
await user.save();
|
||||
@@ -330,23 +330,23 @@ api.updateEmail = {
|
||||
middlewares: [authWithHeaders()],
|
||||
url: '/user/auth/update-email',
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
const { user } = res.locals;
|
||||
|
||||
if (!user.auth.local.email) throw new BadRequest(res.t('userHasNoLocalRegistration'));
|
||||
|
||||
req.checkBody('newEmail', res.t('newEmailRequired')).notEmpty().isEmail();
|
||||
req.checkBody('password', res.t('missingPassword')).notEmpty();
|
||||
let validationErrors = req.validationErrors();
|
||||
const validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
let emailAlreadyInUse = await User.findOne({
|
||||
const emailAlreadyInUse = await User.findOne({
|
||||
'auth.local.email': req.body.newEmail.toLowerCase(),
|
||||
}).select({_id: 1}).lean().exec();
|
||||
}).select({ _id: 1 }).lean().exec();
|
||||
|
||||
if (emailAlreadyInUse) throw new NotAuthorized(res.t('cannotFulfillReq', { techAssistanceEmail: TECH_ASSISTANCE_EMAIL }));
|
||||
|
||||
let password = req.body.password;
|
||||
let isValidPassword = await passwordUtils.compare(user, password);
|
||||
const { password } = req.body;
|
||||
const isValidPassword = await passwordUtils.compare(user, password);
|
||||
if (!isValidPassword) throw new NotAuthorized(res.t('wrongPassword'));
|
||||
|
||||
// if password is using old sha1 encryption, change it
|
||||
@@ -377,18 +377,18 @@ api.resetPasswordSetNewOne = {
|
||||
method: 'POST',
|
||||
url: '/user/auth/reset-password-set-new-one',
|
||||
async handler (req, res) {
|
||||
let user = await validatePasswordResetCodeAndFindUser(req.body.code);
|
||||
let isValidCode = Boolean(user);
|
||||
const user = await validatePasswordResetCodeAndFindUser(req.body.code);
|
||||
const isValidCode = Boolean(user);
|
||||
|
||||
if (!isValidCode) throw new NotAuthorized(res.t('invalidPasswordResetCode'));
|
||||
|
||||
req.checkBody('newPassword', res.t('missingNewPassword')).notEmpty();
|
||||
req.checkBody('confirmPassword', res.t('missingNewPassword')).notEmpty();
|
||||
let validationErrors = req.validationErrors();
|
||||
const validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
let newPassword = req.body.newPassword;
|
||||
let confirmPassword = req.body.confirmPassword;
|
||||
const { newPassword } = req.body;
|
||||
const { confirmPassword } = req.body;
|
||||
|
||||
if (newPassword !== confirmPassword) {
|
||||
throw new BadRequest(res.t('passwordConfirmationMatch'));
|
||||
@@ -416,17 +416,15 @@ api.deleteSocial = {
|
||||
url: '/user/auth/social/:network',
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
let network = req.params.network;
|
||||
let isSupportedNetwork = common.constants.SUPPORTED_SOCIAL_NETWORKS.find(supportedNetwork => {
|
||||
return supportedNetwork.key === network;
|
||||
});
|
||||
const { user } = res.locals;
|
||||
const { network } = req.params;
|
||||
const isSupportedNetwork = common.constants.SUPPORTED_SOCIAL_NETWORKS.find(supportedNetwork => supportedNetwork.key === network);
|
||||
if (!isSupportedNetwork) throw new BadRequest(res.t('unsupportedNetwork'));
|
||||
if (!hasBackupAuth(user, network)) throw new NotAuthorized(res.t('cantDetachSocial'));
|
||||
let unset = {
|
||||
const unset = {
|
||||
[`auth.${network}`]: 1,
|
||||
};
|
||||
await User.update({_id: user._id}, {$unset: unset}).exec();
|
||||
await User.update({ _id: user._id }, { $unset: unset }).exec();
|
||||
|
||||
res.respond(200, {});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user