diff --git a/common/models/user.js b/common/models/user.js index d0dff0432f..3fef044688 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -14,8 +14,8 @@ import { isEmail } from 'validator'; import path from 'path'; import loopback from 'loopback'; import _ from 'lodash'; -import { ObjectId } from 'mongodb'; import jwt from 'jsonwebtoken'; +import generate from 'nanoid/generate'; import { fixCompletedChallengeItem } from '../utils'; import { themes } from '../utils/themes'; @@ -34,6 +34,8 @@ import { const log = debugFactory('fcc:models:user'); const BROWNIEPOINTS_TIMEOUT = [1, 'hour']; +const nanoidCharSet = + '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; const createEmailError = redirectTo => wrapHandledError( new Error('email format is invalid'), @@ -237,7 +239,7 @@ module.exports = function(User) { user.externalId = uuid(); } if (!user.unsubscribeId) { - user.unsubscribeId = new ObjectId(); + user.unsubscribeId = generate(nanoidCharSet, 20); } if (!user.progressTimestamps) { @@ -296,7 +298,7 @@ module.exports = function(User) { } if (!user.unsubscribeId) { - user.unsubscribeId = new ObjectId(); + user.unsubscribeId = generate(nanoidCharSet, 20); } }) .ignoreElements(); diff --git a/package-lock.json b/package-lock.json index 3b347c43d1..9e4b323481 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5901,7 +5901,7 @@ }, "event-stream": { "version": "3.3.4", - "resolved": "https://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz", + "resolved": "http://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz", "integrity": "sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=", "dev": true, "requires": { @@ -12719,6 +12719,11 @@ "dev": true, "optional": true }, + "nanoid": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-1.1.0.tgz", + "integrity": "sha512-iOCqgXieGrk8/wDt1n9rZS2KB1dYVssemY0NTWjfzVr+1t1gAmdTp1u2+YHppKro3Bk5S+Gs+xmYCfpuXauYXQ==" + }, "nanomatch": { "version": "1.2.6", "resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.6.tgz", @@ -16834,7 +16839,7 @@ }, "should-equal": { "version": "0.3.1", - "resolved": "https://registry.npmjs.org/should-equal/-/should-equal-0.3.1.tgz", + "resolved": "http://registry.npmjs.org/should-equal/-/should-equal-0.3.1.tgz", "integrity": "sha1-vY6pemdI45+tR2o75v1y68LnK/A=", "dev": true, "requires": { diff --git a/package.json b/package.json index de0267d803..08ac9e04ae 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "mongodb": "^3.0.4", "morgan": "^1.6.1", "mousetrap": "~1.6.0", + "nanoid": "^1.1.0", "node-emoji": "^1.4.1", "nodemailer": "^2.1.0", "nodemailer-ses-transport": "^1.5.1", diff --git a/server/boot/randomAPIs.js b/server/boot/randomAPIs.js index 061482f35a..9c4aa7a01c 100644 --- a/server/boot/randomAPIs.js +++ b/server/boot/randomAPIs.js @@ -1,5 +1,4 @@ import request from 'request'; -import { ObjectId } from 'mongodb'; import constantStrings from '../utils/constantStrings.json'; import testimonials from '../resources/testimonials.json'; @@ -162,7 +161,7 @@ module.exports = function(app) { req.flash('info', { msg: 'We\'ve successfully updated your Email preferences.' }); - return res.redirect('/unsubscribed/'); + return res.redirect('/unsubscribed'); }) .catch(next); }); @@ -176,13 +175,11 @@ module.exports = function(app) { msg: 'We could not find an account to unsubscribe' }); return res.redirect('/'); - } const [ user ] = users; return new Promise((resolve, reject) => user.updateAttributes({ - sendQuincyEmail: false, - unsubscribeId: unsubscribeId + sendQuincyEmail: false }, (err) => { if (err) { reject(err); @@ -194,7 +191,7 @@ module.exports = function(app) { req.flash('success', { msg: 'We\'ve successfully updated your email preferences.' }); - return res.redirect(`/unsubscribed/${queryId}`); + return res.redirect(`/unsubscribed/${unsubscribeId}`); }) .catch(next); }); @@ -215,16 +212,8 @@ module.exports = function(app) { } function resubscribe(req, res, next) { - const { unsubscribeId: queryId } = req.params; - return User.find({ - where: { - or: [ - { unsubscribeId: queryId }, - { unsubscribeId: ObjectId(queryId).toString() }, - { unsubscribeId: ObjectId(queryId) } - ] - } - }, + const { unsubscribeId } = req.params; + return User.find({ where: { unsubscribeId } }, (err, users) => { if (err || !users.length) { req.flash('info', {