Added catch for 'mailbox not found' errors

master
Keith Irwin 2017-12-19 06:40:38 +00:00
parent 4bcf4da286
commit 59ee66641d
No known key found for this signature in database
GPG Key ID: 378933C743E2BBC0
1 changed files with 38 additions and 15 deletions

View File

@ -53,11 +53,12 @@ module.exports = (app, passport) => {
res.redirect('/login#signup')
})
.post((req, res, next) => {
// Send token and alert user
function sendToken (user) {
debug(`sendToken() called for user ${user.id}`)
// Create a password token
// Create a new password token
user.createPassToken((err, token, expires) => {
if (err) {
debug(`Error creating password token for user ${user.id}!`)
@ -99,11 +100,29 @@ module.exports = (app, passport) => {
)
res.redirect('/login')
})
.catch((err) => {
debug(`Failed to email new user ${user.id} instructions to continue!`)
.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. `)
// 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')
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')
})
} })
}
})
}
@ -115,6 +134,7 @@ module.exports = (app, passport) => {
debug(`Searching for user with email ${req.body.email}...`)
User.findOne({'email': req.body.email})
.then((user) => {
// User already exists
if (user && user.auth.password) {
debug(`User ${user.id} has email ${req.body.email} and has a password`)
@ -128,17 +148,20 @@ module.exports = (app, passport) => {
// User exists but hasn't created a password yet
} else if (user) {
debug(`User ${user.id} has email ${req.body.email} but doesn't have a password`)
// Send another token (or the same one if it hasn't expired)
// Send another token
sendToken(user)
// Create user
} else {
debug(`User with email ${req.body.email} doesn't exist; creating one`)
let email = req.body.email
user = new User()
user.created = Date.now()
user.email = req.body.email
user.slug = slugify(user.email.substring(0, user.email.indexOf('@')))
user.email = email
user.slug = slugify(email.substring(0, email.indexOf('@')))
// Generate unique slug
const slug = new Promise((resolve, reject) => {
@ -210,7 +233,7 @@ module.exports = (app, passport) => {
.catch((err) => {
debug(`Failed to check if somebody already has the email ${req.body.email}`)
mw.throwErr(err, req)
res.redirect('/signup')
res.redirect('/login#signup')
})
})