#116 Switched all then/catch chains with async/await
parent
b06e72f40c
commit
cd687e688d
|
@ -19,20 +19,15 @@ module.exports = {
|
|||
verify: () => {
|
||||
debug(`Verifying SMTP connection...`)
|
||||
|
||||
return new Promise( (resolve, reject) => {
|
||||
transporter.verify()
|
||||
.then( (success) => {
|
||||
if (success) {
|
||||
return new Promise( async (resolve, reject) => {
|
||||
try {
|
||||
if (await transporter.verify()) {
|
||||
console.log(` Nodemailer connected to ${env.mailserver}:${env.mailport} as ${env.mailauth.user}`)
|
||||
resolve()
|
||||
} else reject(new Error(
|
||||
} else reject( new Error(
|
||||
`Nodemailer failed to connect to SMTP server at smtp:/\/${env.mailauth.user}:${env.mailauth.pass}@${env.mailserver}:${env.mailport}`
|
||||
))
|
||||
resolve()
|
||||
}).catch( (err) => {
|
||||
console.log(err.stack)
|
||||
reject()
|
||||
})
|
||||
) )
|
||||
} catch (err) { reject(err) }
|
||||
})
|
||||
|
||||
},
|
||||
|
|
|
@ -56,16 +56,15 @@ userSchema.methods.createEmailToken = function () {
|
|||
let user = this
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
crypto.randomBytes(16, (err, buf) => {
|
||||
crypto.randomBytes(16, async (err, buf) => {
|
||||
if (err) return reject(err)
|
||||
if (buf) {
|
||||
debug(`Buffer ${buf.toString('hex')} created`)
|
||||
user.emailToken = buf.toString('hex')
|
||||
user.save()
|
||||
.then(() => {
|
||||
try {
|
||||
await user.save()
|
||||
resolve(user.emailToken)
|
||||
})
|
||||
.catch(reject)
|
||||
} catch (err) { reject(err) }
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -77,35 +76,33 @@ userSchema.methods.createPassToken = function () {
|
|||
let user = this
|
||||
debug(`user.createPassToken() called for ${user.id}`)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise( async (resolve, reject) => {
|
||||
|
||||
// Reuse old token, resetting clock
|
||||
if (user.auth.passTokenExpires >= Date.now()) {
|
||||
debug(`Reusing old password token...`)
|
||||
user.auth.passTokenExpires = Date.now() + 3600000 // 1 hour
|
||||
user.save()
|
||||
.then(() => {
|
||||
resolve(user.auth.passToken, user.auth.passTokenExpires)
|
||||
})
|
||||
.catch(reject)
|
||||
try {
|
||||
await user.save()
|
||||
resolve([user.auth.passToken, user.auth.passTokenExpires])
|
||||
} catch (err) { reject(err) }
|
||||
|
||||
// Create new token
|
||||
} else {
|
||||
debug(`Creating new password token...`)
|
||||
crypto.randomBytes(16, (err, buf) => {
|
||||
crypto.randomBytes(16, async (err, buf) => {
|
||||
if (err) return reject(err)
|
||||
if (buf) {
|
||||
user.auth.passToken = buf.toString('hex')
|
||||
user.auth.passTokenExpires = Date.now() + 3600000 // 1 hour
|
||||
user.save()
|
||||
.then(() => {
|
||||
debug('successfully saved user in createPassToken')
|
||||
resolve(user.auth.passToken, user.auth.passTokenExpires)
|
||||
})
|
||||
.catch((err) => {
|
||||
debug('error saving user in createPassToken')
|
||||
try {
|
||||
await user.save()
|
||||
debug('Successfully saved user in createPassToken')
|
||||
resolve([user.auth.passToken, user.auth.passTokenExpires])
|
||||
} catch (err) {
|
||||
debug('Error saving user in createPassToken')
|
||||
reject(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -127,12 +124,13 @@ userSchema.methods.generateHashedPassword = function (password) {
|
|||
// Generate hash
|
||||
bcrypt.genSalt(8, (err, salt) => {
|
||||
if (err) return reject(err)
|
||||
bcrypt.hash(password, salt, (err, hash) => {
|
||||
bcrypt.hash(password, salt, async (err, hash) => {
|
||||
if (err) return reject(err)
|
||||
this.auth.password = hash
|
||||
this.save()
|
||||
.then(resolve)
|
||||
.catch(reject)
|
||||
try {
|
||||
await this.save()
|
||||
resolve()
|
||||
} catch (err) { reject() }
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -143,9 +141,7 @@ userSchema.methods.generateHashedPassword = function (password) {
|
|||
userSchema.methods.validPassword = function (password) {
|
||||
let user = this
|
||||
debug(`user.validPassword() called for ${user.id}`)
|
||||
|
||||
return bcrypt.compare(password, user.auth.password)
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -13,7 +13,7 @@ const mw = require('./middleware.js')
|
|||
const User = require('./models.js').user
|
||||
|
||||
module.exports = (passport) => {
|
||||
|
||||
|
||||
// Serialize/deserialize users
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user.id)
|
||||
|
@ -30,11 +30,11 @@ module.exports = (passport) => {
|
|||
usernameField: 'email',
|
||||
passwordField: 'password',
|
||||
passReqToCallback: true
|
||||
}, (req, email, password, done) => {
|
||||
}, async (req, email, password, done) => {
|
||||
debug(`Perfoming local login for ${email}`)
|
||||
User.findOne({'email': email})
|
||||
.then((user) => {
|
||||
|
||||
try {
|
||||
let user = await User.findOne({'email': email})
|
||||
|
||||
// No user with that email
|
||||
if (!user) {
|
||||
debug(`No user with that email`)
|
||||
|
@ -44,39 +44,33 @@ module.exports = (passport) => {
|
|||
// User exists
|
||||
} else {
|
||||
debug(`User exists. Checking password...`)
|
||||
|
||||
|
||||
// Check password
|
||||
user.validPassword(password)
|
||||
.then( (res) => {
|
||||
let res = await user.validPassword(password)
|
||||
|
||||
// Password incorrect
|
||||
if (!res) {
|
||||
debug(`Incorrect password`)
|
||||
req.session.next = undefined
|
||||
return done(null, false, req.flash('warning', 'Incorrect email or password.'))
|
||||
// Password incorrect
|
||||
if (!res) {
|
||||
debug(`Incorrect password`)
|
||||
req.session.next = undefined
|
||||
return done(null, false, req.flash('warning', 'Incorrect email or password.'))
|
||||
|
||||
// Successful login
|
||||
} else {
|
||||
user.lastLogin = Date.now()
|
||||
user.save()
|
||||
return done(null, user)
|
||||
}
|
||||
|
||||
// Successful login
|
||||
} else {
|
||||
user.lastLogin = Date.now()
|
||||
user.save()
|
||||
return done(null, user)
|
||||
}
|
||||
|
||||
})
|
||||
.catch( (err) => {
|
||||
return done(err)
|
||||
})
|
||||
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
return done(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
))
|
||||
|
||||
// Social login
|
||||
function socialLogin(req, service, profileId, done) {
|
||||
async function socialLogin(req, service, profileId, done) {
|
||||
debug(`socialLogin() called for ${service} account ${profileId}`)
|
||||
let query = {}
|
||||
query['auth.' + service] = profileId
|
||||
|
@ -84,31 +78,32 @@ module.exports = (passport) => {
|
|||
// Intent to log in
|
||||
if (!req.user) {
|
||||
debug(`Searching for user with query ${query}...`)
|
||||
User.findOne(query)
|
||||
.then((user) => {
|
||||
try {
|
||||
let user = await User.findOne(query)
|
||||
|
||||
// Can't find user
|
||||
if (!user) {
|
||||
// Lazy update from old googleId field
|
||||
if (service === 'google') {
|
||||
User.findOne({ 'googleID': parseInt(profileId, 10) })
|
||||
.then((user) => {
|
||||
try {
|
||||
let user = await User.findOne({ 'googleID': parseInt(profileId, 10) })
|
||||
|
||||
// User exists with old schema
|
||||
if (user) {
|
||||
debug(`User ${user.id} exists with old schema. Lazily updating...`)
|
||||
user.auth.google = profileId
|
||||
user.googleId = undefined
|
||||
user.save()
|
||||
.then(() => {
|
||||
try {
|
||||
await user.save()
|
||||
debug(`Lazily updated ${user.id}...`)
|
||||
req.session.flashType = 'success'
|
||||
req.session.flashMessage = 'You have been logged in. '
|
||||
return done(null, user)
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug(`Failed to save user that exists with old googleId schema!`)
|
||||
mw.throwErr(err, req)
|
||||
return done(err)
|
||||
})
|
||||
}
|
||||
|
||||
// No such user
|
||||
} else {
|
||||
|
@ -116,12 +111,11 @@ module.exports = (passport) => {
|
|||
req.flash('warning', `There's no user for that ${service} account. `)
|
||||
return done()
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug(`Failed to search for user with old googleID of ${profileId}. `)
|
||||
mw.throwErr(err, req)
|
||||
return done(err)
|
||||
})
|
||||
}
|
||||
|
||||
// No googleId either
|
||||
} else {
|
||||
|
@ -137,12 +131,11 @@ module.exports = (passport) => {
|
|||
req.session.flashMessage = 'You have been logged in.'
|
||||
return done(null, user)
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug(`Failed to find user with query: ${query}`)
|
||||
mw.throwErr(err, req)
|
||||
return done(err)
|
||||
})
|
||||
}
|
||||
|
||||
// Intent to connect account
|
||||
} else {
|
||||
|
@ -150,8 +143,9 @@ module.exports = (passport) => {
|
|||
|
||||
// Check for unique profileId
|
||||
debug(`Checking for unique account with query ${query}...`)
|
||||
User.findOne(query)
|
||||
.then((existingUser) => {
|
||||
try {
|
||||
let user = await User.findOne(query)
|
||||
|
||||
// Social account already in use
|
||||
if (existingUser) {
|
||||
debug(`${service} account already in use with user ${existingUser.id}`)
|
||||
|
@ -163,24 +157,22 @@ module.exports = (passport) => {
|
|||
} else {
|
||||
debug(`${service} account (${profileId}) is unique; Connecting to ${req.user.id}...`)
|
||||
req.user.auth[service] = profileId
|
||||
req.user.save()
|
||||
.then(() => {
|
||||
try {
|
||||
await req.user.save()
|
||||
debug(`Successfully connected ${service} account to ${req.user.id}`)
|
||||
req.session.flashType = 'success'
|
||||
req.session.flashMessage = `${mw.capitalize(service)} account connected. `
|
||||
return done(null, req.user)
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug(`Failed to connect ${service} account to ${req.user.id}!`)
|
||||
return done(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug(`Failed to check for unique ${service} profileId of ${profileId}!`)
|
||||
mw.throwErr(err, req)
|
||||
return done(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,31 +5,29 @@ const mw = require('../middleware.js')
|
|||
const debug = require('debug')('tracman-routes-admin')
|
||||
const User = require('../models.js').user
|
||||
|
||||
router.get('/', mw.ensureAdmin, (req, res) => {
|
||||
User.find({}).sort({lastLogin: -1})
|
||||
.then((found) => {
|
||||
router.get('/', mw.ensureAdmin, async (req, res) => {
|
||||
try {
|
||||
let found = await User.find({}).sort({lastLogin: -1})
|
||||
res.render('admin', {
|
||||
active: 'admin',
|
||||
noFooter: '1',
|
||||
users: found,
|
||||
total: found.length
|
||||
})
|
||||
})
|
||||
.catch((err) => { mw.throwErr(err, req) })
|
||||
} catch (err) { mw.throwErr(err, req) }
|
||||
})
|
||||
|
||||
router.get('/delete/:usrid', mw.ensureAdmin, (req, res, next) => {
|
||||
router.get('/delete/:usrid', mw.ensureAdmin, async (req, res, next) => {
|
||||
debug(`/delete/${req.params.usrid} called`)
|
||||
|
||||
User.findOneAndRemove({'_id': req.params.usrid})
|
||||
.then((user) => {
|
||||
try {
|
||||
await User.findOneAndRemove({'_id': req.params.usrid})
|
||||
req.flash('success', `<i>${req.params.usrid}</i> deleted.`)
|
||||
res.redirect('/admin')
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/admin')
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
|
|
@ -53,81 +53,81 @@ module.exports = (app, passport) => {
|
|||
.get((req, res) => {
|
||||
res.redirect('/login#signup')
|
||||
})
|
||||
.post((req, res, next) => {
|
||||
.post( async (req, res, next) => {
|
||||
|
||||
// Send token and alert user
|
||||
function sendToken(user) {
|
||||
async function sendToken(user) {
|
||||
debug(`sendToken() called for user ${user.id}`)
|
||||
|
||||
// Create a new password token
|
||||
user.createPassToken()
|
||||
.then( (token, expires) => {
|
||||
debug(`Created password token for user ${user.id} successfully`)
|
||||
try {
|
||||
let [token, expires] = await user.createPassToken()
|
||||
console.log(token,expires)
|
||||
debug(`Created password token for user ${user.id} successfully`)
|
||||
|
||||
// Figure out expiration time
|
||||
let expirationTimeString = (req.query.tz)
|
||||
? moment(expires).utcOffset(req.query.tz).toDate().toLocaleTimeString(req.acceptsLanguages[0])
|
||||
: moment(expires).toDate().toLocaleTimeString(req.acceptsLanguages[0]) + ' UTC'
|
||||
// Figure out expiration time string
|
||||
debug(`Determining expiration time string for ${expires}...`)
|
||||
let expiration_time_string = (req.query.tz)
|
||||
? moment(expires).utcOffset(req.query.tz).toDate().toLocaleTimeString(req.acceptsLanguages[0])
|
||||
: moment(expires).toDate().toLocaleTimeString(req.acceptsLanguages[0]) + ' UTC'
|
||||
|
||||
// Email the instructions to continue
|
||||
debug(`Emailing new user ${user.id} at ${user.email} instructions to create a password...`)
|
||||
mail.send({
|
||||
// Email the instructions to continue
|
||||
debug(`Emailing new user ${user.id} at ${user.email} instructions to create a password...`)
|
||||
try {
|
||||
await mail.send({
|
||||
from: mail.noReply,
|
||||
to: `<${user.email}>`,
|
||||
subject: 'Complete your Tracman registration',
|
||||
text: mail.text(
|
||||
`Welcome to Tracman! \n\nTo complete your registration, follow \
|
||||
this link and set your password:\n${env.url}/settings/password/${token}\n\n\
|
||||
This link will expire at ${expirationTimeString}. `
|
||||
This link will expire at ${expiration_time_string}. `
|
||||
),
|
||||
html: mail.html(
|
||||
`<p>Welcome to Tracman! </p><p>To complete your registration, \
|
||||
follow this link and set your password:\
|
||||
<br><a href="${env.url}/settings/password/${token}">\
|
||||
${env.url}/settings/password/${token}</a></p>\
|
||||
<p>This link will expire at ${expirationTimeString}. </p>`
|
||||
<p>This link will expire at ${expiration_time_string}. </p>`
|
||||
)
|
||||
})
|
||||
.then(() => {
|
||||
debug(`Successfully emailed new user ${user.id} instructions to continue`)
|
||||
req.flash('success',
|
||||
`An email has been sent to <u>${user.email}</u>. Check your \
|
||||
inbox and follow the link to complete your registration. (Your \
|
||||
registration link will expire in one hour). `
|
||||
)
|
||||
res.redirect('/login')
|
||||
})
|
||||
.catch((err) => { switch (err.responseCode) {
|
||||
debug(`Successfully emailed new user ${user.id} instructions to continue`)
|
||||
req.flash('success',
|
||||
`An email has been sent to <u>${user.email}</u>. Check your \
|
||||
inbox and follow the link to complete your registration. (Your \
|
||||
registration link will expire in one hour). `
|
||||
)
|
||||
res.redirect('/login')
|
||||
} catch (err) { switch (err.responseCode) {
|
||||
|
||||
// Mailbox doesn't exist
|
||||
case 550: {
|
||||
debug(`Failed to email new user ${user.id} instructions to create a password because the mailbox for ${user.email} wasn't found. `)
|
||||
// Mailbox doesn't exist
|
||||
case 550: {
|
||||
debug(`Failed to email new user ${user.id} instructions to create a password because the mailbox for ${user.email} wasn't found. `)
|
||||
|
||||
// Remove user
|
||||
user.remove().catch( (err) => {
|
||||
console.error(`Failed to remove new user ${user.id}, with a nonexistant email of ${user.email}:\n`,err.stack)
|
||||
})
|
||||
// Remove user
|
||||
user.remove().catch( (err) => {
|
||||
console.error(`Failed to remove new user ${user.id}, with a nonexistant email of ${user.email}:\n`,err.stack)
|
||||
})
|
||||
|
||||
// Redirect back
|
||||
req.flash('danger', `Mailbox for <u>${user.email}</u> not found. Did you enter that correctly?`)
|
||||
res.redirect('/login#signup')
|
||||
// Redirect back
|
||||
req.flash('danger', `Mailbox for <u>${user.email}</u> not found. Did you enter that correctly?`)
|
||||
res.redirect('/login#signup')
|
||||
|
||||
break
|
||||
break
|
||||
|
||||
// Other error
|
||||
} default: {
|
||||
debug(`Failed to email new user ${user.id} instructions to create a password!`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login#signup')
|
||||
}
|
||||
// Other error
|
||||
} default: {
|
||||
debug(`Failed to email new user ${user.id} instructions to create a password!`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login#signup')
|
||||
}
|
||||
|
||||
} })
|
||||
})
|
||||
.catch( (err) => {
|
||||
} }
|
||||
} catch (err) {
|
||||
debug(`Error creating password token for user ${user.id}!`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login#signup')
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -135,9 +135,9 @@ module.exports = (app, passport) => {
|
|||
req.checkBody('email', 'Please enter a valid email address.').isEmail()
|
||||
|
||||
// Check if somebody already has that email
|
||||
debug(`Searching for user with email ${req.body.email}...`)
|
||||
User.findOne({'email': req.body.email})
|
||||
.then((user) => {
|
||||
try {
|
||||
debug(`Searching for user with email ${req.body.email}...`)
|
||||
let user = await User.findOne({'email': req.body.email})
|
||||
|
||||
// User already exists
|
||||
if (user && user.auth.password) {
|
||||
|
@ -171,10 +171,11 @@ module.exports = (app, passport) => {
|
|||
const slug = new Promise((resolve, reject) => {
|
||||
debug(`Creating new slug for user...`);
|
||||
|
||||
(function checkSlug (s, cb) {
|
||||
debug(`Checking to see if slug ${s} is taken...`)
|
||||
User.findOne({slug: s})
|
||||
.then((existingUser) => {
|
||||
(async function checkSlug (s, cb) {
|
||||
try {
|
||||
debug(`Checking to see if slug ${s} is taken...`)
|
||||
let existingUser = await User.findOne({slug: s})
|
||||
|
||||
// Slug in use: generate a random one and retry
|
||||
if (existingUser) {
|
||||
debug(`Slug ${s} is taken; generating another...`)
|
||||
|
@ -194,12 +195,12 @@ module.exports = (app, passport) => {
|
|||
debug(`Slug ${s} is unique`)
|
||||
cb(s)
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug('Failed to create slug!')
|
||||
mw.throwErr(err, req)
|
||||
reject()
|
||||
})
|
||||
}
|
||||
|
||||
})(user.slug, (newSlug) => {
|
||||
debug(`Successfully created slug: ${newSlug}`)
|
||||
user.slug = newSlug
|
||||
|
@ -225,21 +226,20 @@ module.exports = (app, passport) => {
|
|||
})
|
||||
|
||||
// Save user and send the token by email
|
||||
Promise.all([slug, sk32])
|
||||
//.then(() => { user.save() })
|
||||
.then(() => { sendToken(user) })
|
||||
.catch((err) => {
|
||||
try {
|
||||
await Promise.all([slug, sk32])
|
||||
sendToken(user)
|
||||
} catch (err) {
|
||||
debug('Failed to save user after creating slug and sk32!')
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login#signup')
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug(`Failed to check if somebody already has the email ${req.body.email}`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login#signup')
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// Forgot password
|
||||
|
@ -258,32 +258,33 @@ module.exports = (app, passport) => {
|
|||
})
|
||||
|
||||
// Submitted forgot password form
|
||||
.post((req, res, next) => {
|
||||
.post( async (req, res, next) => {
|
||||
// Validate email
|
||||
req.checkBody('email', 'Please enter a valid email address.').isEmail()
|
||||
|
||||
// Check if somebody has that email
|
||||
User.findOne({'email': req.body.email})
|
||||
.then((user) => {
|
||||
|
||||
// No user with that email
|
||||
if (!user) {
|
||||
// Don't let on that no such user exists, to prevent dictionary attacks
|
||||
req.flash('success',
|
||||
`If an account exists with the email <u>${req.body.email}</u>, \
|
||||
an email has been sent there with a password reset link. `
|
||||
)
|
||||
res.redirect('/login')
|
||||
try {
|
||||
let user = await User.findOne({'email': req.body.email})
|
||||
|
||||
// User with that email does exist
|
||||
} else {
|
||||
// No user with that email
|
||||
if (!user) {
|
||||
// Don't let on that no such user exists, to prevent dictionary attacks
|
||||
req.flash('success',
|
||||
`If an account exists with the email <u>${req.body.email}</u>, \
|
||||
an email has been sent there with a password reset link. `
|
||||
)
|
||||
res.redirect('/login')
|
||||
|
||||
// Create reset token
|
||||
user.createPassToken()
|
||||
.then( (token) => {
|
||||
// User with that email does exist
|
||||
} else {
|
||||
|
||||
// Email reset link
|
||||
mail.send({
|
||||
// Create reset token
|
||||
try {
|
||||
let [token, expires] = await user.createPassToken()
|
||||
|
||||
// Email reset link
|
||||
try {
|
||||
await mail.send({
|
||||
from: mail.noReply,
|
||||
to: mail.to(user),
|
||||
subject: 'Reset your Tracman password',
|
||||
|
@ -301,29 +302,23 @@ module.exports = (app, passport) => {
|
|||
<p>If you didn't initiate this request, just ignore this email. </p>`
|
||||
)
|
||||
})
|
||||
.then(() => {
|
||||
req.flash(
|
||||
'success',
|
||||
`If an account exists with the email <u>${req.body.email}</u>, \
|
||||
an email has been sent there with a password reset link. `)
|
||||
res.redirect('/login')
|
||||
})
|
||||
.catch((err) => {
|
||||
debug(`Failed to send reset link to ${user.email}`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login')
|
||||
})
|
||||
})
|
||||
.catch( (err) => {
|
||||
return next(err)
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
debug(`Failed to check for if somebody has that email (in reset request)!`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login/forgot')
|
||||
})
|
||||
req.flash(
|
||||
'success',
|
||||
`If an account exists with the email <u>${req.body.email}</u>, \
|
||||
an email has been sent there with a password reset link. `)
|
||||
res.redirect('/login')
|
||||
} catch (err) {
|
||||
debug(`Failed to send reset link to ${user.email}`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login')
|
||||
}
|
||||
} catch (err) { return next(err) }
|
||||
}
|
||||
} catch (err) {
|
||||
debug(`Failed to check for if somebody has that email (in reset request)!`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/login/forgot')
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
@ -336,7 +331,7 @@ module.exports = (app, passport) => {
|
|||
// app.get('/login/app/twitter', passport.authenticate('twitter-token'), appLoginCallback);
|
||||
|
||||
// Social
|
||||
app.get('/login/:service', (req, res, next) => {
|
||||
app.get('/login/:service', async (req, res, next) => {
|
||||
let service = req.params.service
|
||||
let sendParams = (service === 'google') ? {scope: ['https://www.googleapis.com/auth/userinfo.profile']} : null
|
||||
|
||||
|
@ -366,17 +361,16 @@ module.exports = (app, passport) => {
|
|||
)
|
||||
res.redirect('/settings')
|
||||
} else {
|
||||
req.user.auth[service] = undefined
|
||||
req.user.save()
|
||||
.then(() => {
|
||||
try {
|
||||
req.user.auth[service] = undefined
|
||||
await req.user.save()
|
||||
req.flash('success', `${mw.capitalize(service)} account disconnected. `)
|
||||
res.redirect('/settings')
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
debug(`Failed to save user after disconnecting ${service} account!`)
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/settings')
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -36,7 +36,7 @@ module.exports = router
|
|||
secret: env.recaptchaSecret,
|
||||
response: req.body['g-recaptcha-response'],
|
||||
remoteip: req.ip
|
||||
}}, (err, response, body) => {
|
||||
}}, async (err, response, body) => {
|
||||
// Check for errors
|
||||
if (err) {
|
||||
mw.throwErr(err, req)
|
||||
|
@ -57,20 +57,19 @@ module.exports = router
|
|||
|
||||
// Captcha succeeded
|
||||
} else {
|
||||
mail.send({
|
||||
from: `${req.body.name} <${req.body.email}>`,
|
||||
to: `Tracman Contact <contact@tracman.org>`,
|
||||
subject: req.body.subject || 'A message',
|
||||
text: req.body.message
|
||||
})
|
||||
.then(() => {
|
||||
try {
|
||||
await mail.send({
|
||||
from: `${req.body.name} <${req.body.email}>`,
|
||||
to: `Tracman Contact <contact@tracman.org>`,
|
||||
subject: req.body.subject || 'A message',
|
||||
text: req.body.message
|
||||
})
|
||||
req.flash('success', `Your message has been sent. `)
|
||||
res.redirect(req.session.next || '/')
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/contact')
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -46,31 +46,31 @@ module.exports = router
|
|||
})
|
||||
|
||||
// Endpoint to validate forms
|
||||
.get('/validate', (req, res, next) => {
|
||||
.get('/validate', async (req, res, next) => {
|
||||
// Validate unique slug
|
||||
if (req.query.slug) {
|
||||
User.findOne({ slug: slug(req.query.slug) })
|
||||
.then((existingUser) => {
|
||||
try {
|
||||
let existingUser = await User.findOne({
|
||||
slug: slug(req.query.slug)
|
||||
})
|
||||
if (existingUser && existingUser.id!==req.user.id) res.sendStatus(400)
|
||||
else res.sendStatus(200)
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
}
|
||||
|
||||
// Validate unique email
|
||||
} else if (req.query.email) {
|
||||
User.findOne({ email: req.query.email })
|
||||
.then((existingUser) => {
|
||||
try {
|
||||
let existingUser = User.findOne({ email: req.query.email })
|
||||
if (existingUser && existingUser.id !== req.user.id) {
|
||||
res.sendStatus(400)
|
||||
} else { res.sendStatus(200) }
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
}
|
||||
|
||||
// Create slug
|
||||
} else if (req.query.slugify) res.send(slug(xss(req.query.slugify)))
|
||||
|
|
|
@ -46,16 +46,16 @@ router.get('/demo', (req, res, next) => {
|
|||
})
|
||||
|
||||
// Show map
|
||||
router.get('/:slug?', (req, res, next) => {
|
||||
User.findOne({slug: req.params.slug})
|
||||
.then((mapuser) => {
|
||||
if (!mapuser) next() // 404
|
||||
router.get('/:slug?', async (req, res, next) => {
|
||||
try {
|
||||
let map_user = await User.findOne({slug: req.params.slug})
|
||||
if (!map_user) next() // 404
|
||||
else {
|
||||
var active = '' // For header nav
|
||||
if (req.user && req.user.id === mapuser.id) active = 'map'
|
||||
if (req.user && req.user.id === map_user.id) active = 'map'
|
||||
res.render('map', {
|
||||
active: active,
|
||||
mapuser: mapuser,
|
||||
mapuser: map_user,
|
||||
mapApi: env.googleMapsAPI,
|
||||
user: req.user,
|
||||
noFooter: '1',
|
||||
|
@ -64,9 +64,7 @@ router.get('/:slug?', (req, res, next) => {
|
|||
newuserurl: (req.query.new) ? env.url + '/map/' + req.params.slug : ''
|
||||
})
|
||||
}
|
||||
}).catch((err) => {
|
||||
mw.throwErr(err, req)
|
||||
})
|
||||
} catch (err) { mw.throwErr(err, req) }
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
|
|
@ -23,9 +23,9 @@ router.route('/')
|
|||
})
|
||||
|
||||
// Set new settings
|
||||
.post((req, res, next) => {
|
||||
.post( async (req, res, next) => {
|
||||
// Validate email
|
||||
const checkEmail = new Promise((resolve, reject) => {
|
||||
const checkEmail = new Promise( async (resolve, reject) => {
|
||||
// Check validity
|
||||
if (!mw.validateEmail(req.body.email)) {
|
||||
req.flash('warning', `<u>${req.body.email}</u> is not a valid email address. `)
|
||||
|
@ -36,9 +36,9 @@ router.route('/')
|
|||
|
||||
// Check uniqueness
|
||||
else {
|
||||
User.findOne({ email: req.body.email })
|
||||
.then((existingUser) => {
|
||||
|
||||
try {
|
||||
let existingUser = await User.findOne({ email: req.body.email })
|
||||
|
||||
// Not unique!
|
||||
if (existingUser && existingUser.id !== req.user.id) {
|
||||
debug('Email not unique!')
|
||||
|
@ -54,45 +54,39 @@ router.route('/')
|
|||
|
||||
// Create token
|
||||
debug(`Creating email token...`)
|
||||
return req.user.createEmailToken()
|
||||
}
|
||||
|
||||
})
|
||||
.then( (token) => {
|
||||
|
||||
// Send token to user by email
|
||||
debug(`Mailing new email token to ${req.body.email}...`)
|
||||
return mail.send({
|
||||
to: `"${req.user.name}" <${req.body.email}>`,
|
||||
from: mail.noReply,
|
||||
subject: 'Confirm your new email address for Tracman',
|
||||
text: mail.text(
|
||||
`A request has been made to change your Tracman email address. \
|
||||
If you did not initiate this request, please disregard it. \n\n\
|
||||
To confirm your email, follow this link:\n${env.url}/settings/email/${token}. `
|
||||
),
|
||||
html: mail.html(
|
||||
`<p>A request has been made to change your Tracman email address. \
|
||||
If you did not initiate this request, please disregard it. </p>\
|
||||
<p>To confirm your email, follow this link:\
|
||||
<br><a href="${env.url}/settings/email/${token}">\
|
||||
${env.url}/settings/email/${token}</a>. </p>`
|
||||
let token = await req.user.createEmailToken()
|
||||
|
||||
// Send token to user by email
|
||||
debug(`Mailing new email token to ${req.body.email}...`)
|
||||
await mail.send({
|
||||
to: `"${req.user.name}" <${req.body.email}>`,
|
||||
from: mail.noReply,
|
||||
subject: 'Confirm your new email address for Tracman',
|
||||
text: mail.text(
|
||||
`A request has been made to change your Tracman email address. \
|
||||
If you did not initiate this request, please disregard it. \n\n\
|
||||
To confirm your email, follow this link:\n${env.url}/settings/email/${token}. `
|
||||
),
|
||||
html: mail.html(
|
||||
`<p>A request has been made to change your Tracman email address. \
|
||||
If you did not initiate this request, please disregard it. </p>\
|
||||
<p>To confirm your email, follow this link:\
|
||||
<br><a href="${env.url}/settings/email/${token}">\
|
||||
${env.url}/settings/email/${token}</a>. </p>`
|
||||
)
|
||||
})
|
||||
|
||||
req.flash('warning',
|
||||
`An email has been sent to <u>${req.body.email}</u>. Check your inbox to confirm your new email address. `
|
||||
)
|
||||
})
|
||||
|
||||
})
|
||||
.then( () => {
|
||||
req.flash('warning',
|
||||
`An email has been sent to <u>${req.body.email}</u>. Check your inbox to confirm your new email address. `
|
||||
)
|
||||
resolve()
|
||||
})
|
||||
.catch(reject)
|
||||
resolve()
|
||||
}
|
||||
} catch (err) { reject() }
|
||||
}
|
||||
})
|
||||
|
||||
// Validate slug
|
||||
const checkSlug = new Promise((resolve, reject) => {
|
||||
const checkSlug = new Promise( async (resolve, reject) => {
|
||||
// Check existence
|
||||
if (req.body.slug === '') {
|
||||
req.flash('warning', `You must supply a slug. `)
|
||||
|
@ -103,8 +97,9 @@ router.route('/')
|
|||
|
||||
// Check uniqueness
|
||||
else {
|
||||
User.findOne({ slug: req.body.slug })
|
||||
.then((existingUser) => {
|
||||
try {
|
||||
let existingUser = await User.findOne({ slug: req.body.slug })
|
||||
|
||||
// Not unique!
|
||||
if (existingUser && existingUser.id !== req.user.id) {
|
||||
req.flash( 'warning',
|
||||
|
@ -113,15 +108,15 @@ router.route('/')
|
|||
|
||||
// It's unique
|
||||
} else req.user.slug = slug(xss(req.body.slug))
|
||||
})
|
||||
.then(resolve)
|
||||
.catch(reject)
|
||||
|
||||
resolve()
|
||||
} catch (err) { reject() }
|
||||
}
|
||||
})
|
||||
|
||||
// Set settings when done
|
||||
Promise.all([checkEmail, checkSlug])
|
||||
.then(() => {
|
||||
try {
|
||||
await Promise.all([checkEmail, checkSlug])
|
||||
debug('Setting settings... ')
|
||||
|
||||
// Set values
|
||||
|
@ -139,61 +134,48 @@ router.route('/')
|
|||
|
||||
// Save user and send response
|
||||
debug(`Saving new settings for user ${req.user.name}...`)
|
||||
req.user.save()
|
||||
.then(() => {
|
||||
debug(`DONE! Redirecting user...`)
|
||||
req.flash('success', 'Settings updated. ')
|
||||
res.redirect('/settings')
|
||||
})
|
||||
.catch((err) => {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/settings')
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/settings')
|
||||
})
|
||||
await req.user.save()
|
||||
debug(`DONE! Redirecting user...`)
|
||||
req.flash('success', 'Settings updated. ')
|
||||
|
||||
} catch (err) { mw.throwErr(err, req) }
|
||||
finally { res.redirect('/settings') }
|
||||
})
|
||||
|
||||
// Delete account
|
||||
router.get('/delete', (req, res) => {
|
||||
User.findByIdAndRemove(req.user)
|
||||
.then(() => {
|
||||
router.get('/delete', async (req, res) => {
|
||||
try {
|
||||
await User.findByIdAndRemove(req.user)
|
||||
req.flash('success', 'Your account has been deleted. ')
|
||||
res.redirect('/')
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/settings')
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// Confirm email address
|
||||
router.get('/email/:token', mw.ensureAuth, (req, res, next) => {
|
||||
router.get('/email/:token', mw.ensureAuth, async (req, res, next) => {
|
||||
// Check token
|
||||
if (req.user.emailToken === req.params.token) {
|
||||
// Set new email
|
||||
req.user.email = req.user.newEmail
|
||||
req.user.save()
|
||||
try {
|
||||
// Set new email
|
||||
req.user.email = req.user.newEmail
|
||||
|
||||
// Delete token and newEmail
|
||||
.then(() => {
|
||||
// Delete token and newEmail
|
||||
req.user.emailToken = undefined
|
||||
req.user.newEmail = undefined
|
||||
req.user.save()
|
||||
})
|
||||
|
||||
// Report success
|
||||
.then(() => {
|
||||
await req.user.save()
|
||||
|
||||
// Report success
|
||||
req.flash('success', `Your email has been set to <u>${req.user.email}</u>. `)
|
||||
res.redirect('/settings')
|
||||
})
|
||||
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect(req.session.next || '/settings')
|
||||
})
|
||||
}
|
||||
|
||||
// Invalid token
|
||||
} else {
|
||||
|
@ -209,17 +191,17 @@ router.route('/password')
|
|||
})
|
||||
|
||||
// Email user a token, proceed at /password/:token
|
||||
.get((req, res, next) => {
|
||||
.get( async (req, res, next) => {
|
||||
// Create token for password change
|
||||
req.user.createPassToken()
|
||||
.then( (token, expires) => {
|
||||
try {
|
||||
let [token, expires] = await req.user.createPassToken()
|
||||
// Figure out expiration time
|
||||
let expirationTimeString = (req.query.tz)
|
||||
? moment(expires).utcOffset(req.query.tz).toDate().toLocaleTimeString(req.acceptsLanguages[0])
|
||||
: moment(expires).toDate().toLocaleTimeString(req.acceptsLanguages[0]) + ' UTC'
|
||||
|
||||
// Confirm password change request by email.
|
||||
return mail.send({
|
||||
return await mail.send({
|
||||
to: mail.to(req.user),
|
||||
from: mail.noReply,
|
||||
subject: 'Request to change your Tracman password',
|
||||
|
@ -240,46 +222,45 @@ router.route('/password')
|
|||
<p>This request will expire at ${expirationTimeString}. </p>`
|
||||
)
|
||||
})
|
||||
|
||||
})
|
||||
.then(() => {
|
||||
|
||||
// Alert user to check email.
|
||||
req.flash('success',
|
||||
`An link has been sent to <u>${req.user.email}</u>. \
|
||||
Click on the link to complete your password change. \
|
||||
This link will expire in one hour (${expirationTimeString}). `
|
||||
)
|
||||
res.redirect((req.user) ? '/settings' : '/login')
|
||||
})
|
||||
.catch( (err) => {
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
} finally {
|
||||
res.redirect((req.user) ? '/settings' : '/login')
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
router.route('/password/:token')
|
||||
|
||||
// Check token
|
||||
.all((req, res, next) => {
|
||||
.all( async (req, res, next) => {
|
||||
debug('/settings/password/:token .all() called')
|
||||
User
|
||||
.findOne({'auth.passToken': req.params.token})
|
||||
.where('auth.passTokenExpires').gt(Date.now())
|
||||
.then((user) => {
|
||||
if (!user) {
|
||||
debug('Bad token')
|
||||
req.flash('danger', 'Password reset token is invalid or has expired. ')
|
||||
res.redirect((req.isAuthenticated) ? '/settings' : '/login')
|
||||
} else {
|
||||
debug('setting passwordUser')
|
||||
res.locals.passwordUser = user
|
||||
next()
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/password')
|
||||
})
|
||||
try {
|
||||
let user = await User
|
||||
.findOne({'auth.passToken': req.params.token})
|
||||
.where('auth.passTokenExpires').gt(Date.now())
|
||||
|
||||
if (!user) {
|
||||
debug('Bad token')
|
||||
req.flash('danger', 'Password reset token is invalid or has expired. ')
|
||||
res.redirect((req.isAuthenticated) ? '/settings' : '/login')
|
||||
} else {
|
||||
debug('setting passwordUser')
|
||||
res.locals.passwordUser = user
|
||||
next()
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/password')
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
// Show password change form
|
||||
|
@ -289,7 +270,7 @@ router.route('/password/:token')
|
|||
})
|
||||
|
||||
// Set new password
|
||||
.post((req, res, next) => {
|
||||
.post( async (req, res, next) => {
|
||||
debug('/settings/password/:token .post() called')
|
||||
|
||||
// Validate password strength
|
||||
|
@ -302,26 +283,27 @@ router.route('/password/:token')
|
|||
} else {
|
||||
|
||||
// Create hashed password and save to db
|
||||
res.locals.passwordUser.generateHashedPassword(req.body.password)
|
||||
.then( () => {
|
||||
// User changed password
|
||||
if (req.user) {
|
||||
debug('User saved password')
|
||||
req.flash('success', 'Your password has been changed. ')
|
||||
res.redirect('/settings')
|
||||
try {
|
||||
await res.locals.passwordUser.generateHashedPassword(req.body.password)
|
||||
|
||||
// New user created password
|
||||
} else {
|
||||
debug('New user created password')
|
||||
req.flash('success', 'Password set. You can use it to log in now. ')
|
||||
res.redirect('/login?next=/map?new=1')
|
||||
}
|
||||
})
|
||||
.catch( (err) => {
|
||||
debug('Error creating hashed password and saving to db')
|
||||
mw.throwErr(err, req)
|
||||
res.redirect(`/settings/password/${req.params.token}`)
|
||||
})
|
||||
// User changed password
|
||||
if (req.user) {
|
||||
debug('User saved password')
|
||||
req.flash('success', 'Your password has been changed. ')
|
||||
res.redirect('/settings')
|
||||
|
||||
// New user created password
|
||||
} else {
|
||||
debug('New user created password')
|
||||
req.flash('success', 'Password set. You can use it to log in now. ')
|
||||
res.redirect('/login?next=/map?new=1')
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
debug('Error creating hashed password and saving to db')
|
||||
mw.throwErr(err, req)
|
||||
res.redirect(`/settings/password/${req.params.token}`)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
@ -338,17 +320,16 @@ router.route('/pro')
|
|||
})
|
||||
|
||||
// Join Tracman pro
|
||||
.post((req, res) => {
|
||||
User.findByIdAndUpdate(req.user.id,
|
||||
.post( async (req, res) => {
|
||||
try {
|
||||
let user = await User.findByIdAndUpdate(req.user.id,
|
||||
{$set: { isPro: true }})
|
||||
.then((user) => {
|
||||
req.flash('success', 'You have been signed up for pro. ')
|
||||
res.redirect('/settings')
|
||||
})
|
||||
.catch((err) => {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/settings/pro')
|
||||
})
|
||||
req.flash('success', 'You have been signed up for pro. ')
|
||||
res.redirect('/settings')
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
res.redirect('/settings/pro')
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
|
|
@ -7,22 +7,21 @@ const router = require('express').Router(),
|
|||
|
||||
router
|
||||
|
||||
.get('/mail', (req, res, next) => {
|
||||
mail.send({
|
||||
to: `"Keith Irwin" <hypergeek14@gmail.com>`,
|
||||
from: mail.noReply,
|
||||
subject: 'Test email',
|
||||
text: mail.text("Looks like everything's working! "),
|
||||
html: mail.html("<p>Looks like everything's working! </p>")
|
||||
})
|
||||
.then(() => {
|
||||
.get('/mail', async (req, res, next) => {
|
||||
try {
|
||||
await mail.send({
|
||||
to: `"Keith Irwin" <hypergeek14@gmail.com>`,
|
||||
from: mail.noReply,
|
||||
subject: 'Test email',
|
||||
text: mail.text("Looks like everything's working! "),
|
||||
html: mail.html("<p>Looks like everything's working! </p>")
|
||||
})
|
||||
console.log('Test email should have sent...')
|
||||
res.sendStatus(200)
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
mw.throwErr(err, req)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
.get('/password', (req, res) => {
|
||||
|
|
|
@ -58,7 +58,7 @@ module.exports = {
|
|||
})
|
||||
|
||||
// Set location
|
||||
socket.on('set', (loc) => {
|
||||
socket.on('set', async (loc) => {
|
||||
debug(`${socket.id} set location for ${loc.usr}`)
|
||||
debug(`Location was set to: ${JSON.stringify(loc)}`)
|
||||
|
||||
|
@ -80,10 +80,11 @@ module.exports = {
|
|||
).message
|
||||
)
|
||||
} else {
|
||||
// Get loc.usr
|
||||
User.findById(loc.usr)
|
||||
.where('sk32').equals(loc.tok)
|
||||
.then((user) => {
|
||||
try {
|
||||
// Get loc.usr
|
||||
let user = await User.findById(loc.usr)
|
||||
.where('sk32').equals(loc.tok)
|
||||
|
||||
if (!user) {
|
||||
console.error(
|
||||
new Error(
|
||||
|
@ -105,10 +106,8 @@ module.exports = {
|
|||
time: loc.tim
|
||||
}
|
||||
user.save()
|
||||
.catch((err) => { console.error(err.stack) })
|
||||
}
|
||||
})
|
||||
.catch((err) => { console.error(err.stack) })
|
||||
} catch (err) { console.error(err.stack) }
|
||||
}
|
||||
})
|
||||
|
||||
|
|
53
server.js
53
server.js
|
@ -28,21 +28,20 @@ let ready_promise_list = []
|
|||
mongoose.Promise = global.Promise
|
||||
|
||||
// Connect to database
|
||||
ready_promise_list.push( new Promise( (resolve, reject) => {
|
||||
mongoose.connect(env.mongoSetup, {
|
||||
useMongoClient: true,
|
||||
socketTimeoutMS: 30000,
|
||||
//reconnectTries: 30,
|
||||
keepAlive: true
|
||||
})
|
||||
.then( (db) => {
|
||||
ready_promise_list.push( new Promise( async (resolve, reject) => {
|
||||
try {
|
||||
mongoose.connect(env.mongoSetup, {
|
||||
useMongoClient: true,
|
||||
socketTimeoutMS: 30000,
|
||||
//reconnectTries: 30,
|
||||
keepAlive: true
|
||||
})
|
||||
console.log(` Mongoose connected to ${env.mongoSetup}`)
|
||||
resolve()
|
||||
} )
|
||||
.catch( (err) => {
|
||||
} catch (err) {
|
||||
console.error(err.stack)
|
||||
reject()
|
||||
} )
|
||||
}
|
||||
}) )
|
||||
|
||||
}
|
||||
|
@ -177,40 +176,40 @@ ready_promise_list.push(mail.verify())
|
|||
|
||||
// Listen
|
||||
ready_promise_list.push( new Promise( (resolve, reject) => {
|
||||
http.listen(env.port, () => {
|
||||
http.listen(env.port, async () => {
|
||||
|
||||
console.log(` Express listening on ${env.url}`)
|
||||
resolve()
|
||||
|
||||
// Check for clients for each user
|
||||
ready_promise_list.push( new Promise( (resolve, reject) => {
|
||||
User.find({})
|
||||
.then((users) => {
|
||||