2020-05-25 20:45:26 +00:00
|
|
|
const { Job } = require('flitter-jobs')
|
|
|
|
|
|
|
|
class PasswordResetJob extends Job {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models', 'jobs', 'output', 'configs']
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute(job) {
|
|
|
|
const {data} = job
|
|
|
|
const {user_id} = data
|
|
|
|
|
|
|
|
try {
|
|
|
|
const User = this.models.get('auth:User')
|
|
|
|
const user = await User.findById(user_id)
|
|
|
|
if (!user) {
|
|
|
|
this.output.error(`Unable to find user with ID: ${user_id}`)
|
|
|
|
throw new Error('Unable to find user with that ID.')
|
|
|
|
}
|
|
|
|
|
|
|
|
this.output.info(`Resetting password for user: ${user.uid}`)
|
|
|
|
// Create an authenticated key-action
|
|
|
|
const key_action = await this.key_action(user)
|
|
|
|
await this.jobs.queue('mailer').add('EMail', {
|
|
|
|
to: user.email,
|
|
|
|
subject: 'Reset Your Password | ' + this.configs.get('app.name'),
|
2020-07-10 14:55:28 +00:00
|
|
|
email_params: {
|
|
|
|
header_text: 'Forget your password?',
|
|
|
|
body_paragraphs: [
|
|
|
|
'It looks like you requested a password reset for your account. Click the button below, and we\'ll walk you through creating a new one.',
|
|
|
|
'If you didn\'t request this e-mail, please contact your system administrator.',
|
|
|
|
],
|
|
|
|
button_text: 'Reset Password',
|
|
|
|
button_link: key_action.url(),
|
|
|
|
}
|
2020-05-25 20:45:26 +00:00
|
|
|
})
|
|
|
|
this.output.success('Password reset logged.')
|
|
|
|
} catch (e) {
|
|
|
|
this.output.error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async key_action(user) {
|
|
|
|
const KeyAction = this.models.get('auth:KeyAction')
|
|
|
|
const ka_data = {
|
|
|
|
handler: 'controller::auth:Password.password_reset_keyaction',
|
|
|
|
used: false,
|
|
|
|
user_id: user._id,
|
|
|
|
auto_login: true,
|
|
|
|
no_auto_logout: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new KeyAction(ka_data)).save()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = PasswordResetJob
|