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) const email = this.email(key_action.url()) await this.jobs.queue('mailer').add('EMail', { to: user.email, subject: 'Reset Your Password | ' + this.configs.get('app.name'), html: email, }) 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() } email(keyaction_link) { const app_name = this.configs.get('app.name') const login_url = this.configs.get('app.url') + 'auth/login' const image_url = this.configs.get('app.url') + 'assets/reset_pass_email_image.jpg' return ` ` } } module.exports = exports = PasswordResetJob