mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
start porting register local user handler, several bug fixes
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
"missingAuthHeaders": "Missing authentication headers.",
|
"missingAuthHeaders": "Missing authentication headers.",
|
||||||
"missingUsernameEmail": "Missing username or email.",
|
"missingUsernameEmail": "Missing username or email.",
|
||||||
"missingPassword": "Missing password.",
|
"missingPassword": "Missing password.",
|
||||||
|
"invalidEmail": "Invalid email address.",
|
||||||
|
"emailTaken": "Email already taken.",
|
||||||
|
"usernameTaken": "Username already taken.",
|
||||||
|
"passwordConfirmationMatch": "Password confirmation doesn't match password.",
|
||||||
"invalidLoginCredentials": "Incorrect username / email and / or password.",
|
"invalidLoginCredentials": "Incorrect username / email and / or password.",
|
||||||
"invalidCredentials": "User not found with given auth credentials.",
|
"invalidCredentials": "User not found with given auth credentials.",
|
||||||
"accountSuspended": "Account has been suspended, please contact leslie@habitica.com with your UUID \"<%= userId %>\" for assistance."
|
"accountSuspended": "Account has been suspended, please contact leslie@habitica.com with your UUID \"<%= userId %>\" for assistance."
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ gulp.task('lint:server', () => {
|
|||||||
.src([
|
.src([
|
||||||
'./website/src/**/api-v3/**/*.js',
|
'./website/src/**/api-v3/**/*.js',
|
||||||
'./website/src/models/user.js',
|
'./website/src/models/user.js',
|
||||||
|
'./website/src/models/emailUnsubscription.js',
|
||||||
'./website/src/server.js'
|
'./website/src/server.js'
|
||||||
])
|
])
|
||||||
.pipe(eslint())
|
.pipe(eslint())
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import {
|
|||||||
} from '../../libs/api-v3/errors';
|
} from '../../libs/api-v3/errors';
|
||||||
import passwordUtils from '../../libs/api-v3/password';
|
import passwordUtils from '../../libs/api-v3/password';
|
||||||
import User from '../../models/user';
|
import User from '../../models/user';
|
||||||
|
import EmailUnsubscription from '../../models/emailUnsubscription';
|
||||||
|
import Q from 'q';
|
||||||
|
import { sendTxn as sendTxnEmail } from '../../libs/api-v3/email';
|
||||||
let api = {};
|
let api = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,14 +20,115 @@ let api = {};
|
|||||||
* @apiParam {String} password Password for the new user account
|
* @apiParam {String} password Password for the new user account
|
||||||
* @apiParam {String} passwordConfirmation Password confirmation
|
* @apiParam {String} passwordConfirmation Password confirmation
|
||||||
*
|
*
|
||||||
* @apiSuccess {Object} user The user public fields
|
* @apiSuccess {Object} user The user profile
|
||||||
*
|
|
||||||
*
|
*
|
||||||
* @apiUse NotAuthorized
|
* @apiUse NotAuthorized
|
||||||
*/
|
*/
|
||||||
api.registerLocal = {
|
api.registerLocal = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/user/register/local',
|
url: '/user/register/local',
|
||||||
|
handler (req, res, next) {
|
||||||
|
req.checkBody({
|
||||||
|
username: {
|
||||||
|
notEmpty: true,
|
||||||
|
errorMessage: req.t('missingEmail'),
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
notEmpty: true,
|
||||||
|
isEmail: true,
|
||||||
|
errorMessage: req.t('invalidEmail'),
|
||||||
|
},
|
||||||
|
password: {
|
||||||
|
notEmpty: true,
|
||||||
|
errorMessage: req.t('missingPassword'),
|
||||||
|
},
|
||||||
|
passwordConfirmation: {
|
||||||
|
notEmpty: true,
|
||||||
|
equals: {
|
||||||
|
options: [req.body.password],
|
||||||
|
},
|
||||||
|
errorMessage: req.t('passwordConfirmationMatch'),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
let validationErrors = req.validationErrors();
|
||||||
|
|
||||||
|
if (validationErrors) return next(validationErrors);
|
||||||
|
|
||||||
|
req.sanitizeBody('username').trim();
|
||||||
|
req.sanitizeBody('email').trim();
|
||||||
|
req.sanitizeBody('password').trim();
|
||||||
|
req.sanitizeBody('passwordConfirmation').trim();
|
||||||
|
|
||||||
|
let email = req.body.email.toLowerCase();
|
||||||
|
let username = req.body.username;
|
||||||
|
// Get the lowercase version of username to check that we do not have duplicates
|
||||||
|
// So we can search for it in the database and then reject the choosen username if 1 or more results are found
|
||||||
|
let lowerCaseUsername = username.toLowerCase();
|
||||||
|
|
||||||
|
Q.all([
|
||||||
|
// Search for duplicates using lowercase version of username
|
||||||
|
User.findOne({$or: [
|
||||||
|
{'auth.local.email': email},
|
||||||
|
{'auth.local.lowerCaseUsername': lowerCaseUsername},
|
||||||
|
]}, {'auth.local': 1})
|
||||||
|
.exec(),
|
||||||
|
|
||||||
|
// If the request is made by an authenticated Facebook user, find it
|
||||||
|
// TODO move to a separate route
|
||||||
|
// TODO automatically merge?
|
||||||
|
/* User.findOne({
|
||||||
|
_id: req.headers['x-api-user'],
|
||||||
|
apiToken: req.headers['x-api-key']
|
||||||
|
}, {auth:1})
|
||||||
|
.exec(); */
|
||||||
|
])
|
||||||
|
.then((results) => {
|
||||||
|
if (results[0]) {
|
||||||
|
if (email === results[0].auth.local.email) return next(new NotAuthorized(req.t('emailTaken')));
|
||||||
|
// Check that the lowercase username isn't already used
|
||||||
|
if (lowerCaseUsername === results[0].auth.local.lowerCaseUsername) return next(new NotAuthorized(req.t('usernameTaken')));
|
||||||
|
}
|
||||||
|
|
||||||
|
let salt = passwordUtils.makeSalt();
|
||||||
|
let newUser = new User({
|
||||||
|
auth: {
|
||||||
|
local: {
|
||||||
|
username,
|
||||||
|
lowerCaseUsername, // Store the lowercase version of the username
|
||||||
|
email, // Store email as lowercase
|
||||||
|
salt,
|
||||||
|
hashed_password: passwordUtils.encrypt(req.body.password, salt), // eslint-disable-line camelcase
|
||||||
|
},
|
||||||
|
},
|
||||||
|
preferences: {
|
||||||
|
language: req.language,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
newUser.registeredThrough = req.headers['x-client']; // TODO is this saved somewhere?
|
||||||
|
|
||||||
|
res.analytics.track('register', {
|
||||||
|
category: 'acquisition',
|
||||||
|
type: 'local',
|
||||||
|
gaLabel: 'local',
|
||||||
|
uuid: newUser._id,
|
||||||
|
});
|
||||||
|
|
||||||
|
return newUser.save();
|
||||||
|
})
|
||||||
|
.then((savedUser) => {
|
||||||
|
res.status(201).json(savedUser);
|
||||||
|
|
||||||
|
// Clean previous email preferences
|
||||||
|
EmailUnsubscription
|
||||||
|
.remove({email: savedUser.auth.local.email})
|
||||||
|
.then(() => {
|
||||||
|
sendTxnEmail(savedUser, 'welcome');
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(next);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ fs
|
|||||||
let controller = require(CONTROLLERS_PATH + fileName); // eslint-disable-line global-require
|
let controller = require(CONTROLLERS_PATH + fileName); // eslint-disable-line global-require
|
||||||
|
|
||||||
_.each(controller, (action) => {
|
_.each(controller, (action) => {
|
||||||
let {method, url, middlewares, handler} = action;
|
let {method, url, middlewares = [], handler} = action;
|
||||||
|
|
||||||
method = method.toLowerCase();
|
method = method.toLowerCase();
|
||||||
router[method](url, ...middlewares, handler);
|
router[method](url, ...middlewares, handler);
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ function _getFromUser (user, req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _attachTranslateFunction (req, next) {
|
function _attachTranslateFunction (req, next) {
|
||||||
|
// TODO attach to res?
|
||||||
req.t = function reqTranslation () {
|
req.t = function reqTranslation () {
|
||||||
return i18n.t(...arguments, req.language);
|
return i18n.t(...arguments, req.language);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
var mongoose = require("mongoose");
|
import mongoose from 'mongoose';
|
||||||
var shared = require('../../../common');
|
import common from '../../../common';
|
||||||
|
import validator from 'validator';
|
||||||
|
|
||||||
// A collection used to store mailing list unsubscription for non registered email addresses
|
// A collection used to store mailing list unsubscription for non registered email addresses
|
||||||
var EmailUnsubscriptionSchema = new mongoose.Schema({
|
export let schema = new mongoose.Schema({
|
||||||
_id: {
|
_id: {
|
||||||
type: String,
|
type: String,
|
||||||
'default': shared.uuid
|
default: common.uuid,
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
trim: true,
|
||||||
|
lowercase: true, // TODO migrate existing to lowerCase
|
||||||
|
validator: [validator.isEmail, 'Invalid email.'],
|
||||||
},
|
},
|
||||||
email: String
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports.schema = EmailUnsubscriptionSchema;
|
export let model = mongoose.model('EmailUnsubscription', schema);
|
||||||
module.exports.model = mongoose.model('EmailUnsubscription', EmailUnsubscriptionSchema);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user