diff --git a/template/server/config.json b/template/server/config.json index 98d844b..befc927 100644 --- a/template/server/config.json +++ b/template/server/config.json @@ -3,6 +3,8 @@ "restApiHost": "localhost", "host": "0.0.0.0", "port": 8000, + "emailFrom": "noreply@mydomain.com", + "clientURI": "http://localhost", "remoting": { "context": false, "rest": { diff --git a/template/server/models/account.js b/template/server/models/account.js index d95a6ed..4ad7900 100644 --- a/template/server/models/account.js +++ b/template/server/models/account.js @@ -1,21 +1,63 @@ -import {host} from '../config.json'; +import {clientURI, emailFrom} from '../config.json'; -export default function(Account) { - Account.on('resetPasswordRequest', (info) => { - const url = `https://${host}/#!/profile`; - const html = `Hello!

Click here to create a new password.` + - '

If you have not requested a password change,' + - 'please ignore this email.

Webmaster'; - Account.app.models.Email.send({ - to: info.email, - from: '{{name}} ', - subject: '[{{name}}] Create a new password', - html, - }, (err) => { - if (err) return console.log(err); - console.log('> sending password reset email to:', info.email); - return null; +export default function Account(Account) { + function resetPassword(options) { + console.log( + '> received request password reset for email/token: ', + options.email, + options.accessToken.id + ); + const url = `${clientURI}/profile`; + const html = `Olá!

Clique aqui para criar uma nova senha.` + + '

Caso você não tenha solicitado uma nova senha,' + + 'por favor, ignore esse e-mail.'; + const sendEmail = new Promise((resolve, reject) => { + Account.app.models.Email.send({ + to: options.email, + from: 'Vivescer <' + emailFrom + '>', + subject: '[Vivescer] Alteração de senha', + html, + }, (err) => { + err = new Error('ups'); + if (err) { + console.log('> failed to send reset email to', options.email, err); + reject(); + return; + } + resolve(); + }); }); - }); + + return sendEmail; + } + + Account.resetPassword = (options) => { + return new Promise((sendSuccess, sendError) => { + Account.on('resetPasswordRequest', updatedOptions => { + resetPassword(updatedOptions) + .then(sendSuccess) + .catch(sendError); + }); + Account.base + .resetPassword(options) + .catch(sendError); + }); + }; + + Account.remoteMethod( + 'resetPassword', + { + description: 'Reset password for a user with email.', + accepts: [ + { + arg: 'options', + type: 'object', + required: true, + http: {source: 'body'}, + }, + ], + http: {verb: 'post', path: '/reset'}, + } + ); }