Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: fix(account): fix reset not returning errors #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions template/server/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"restApiHost": "localhost",
"host": "0.0.0.0",
"port": 8000,
"emailFrom": "[email protected]",
"clientURI": "http://localhost",
"remoting": {
"context": false,
"rest": {
Expand Down
78 changes: 60 additions & 18 deletions template/server/models/account.js
Original file line number Diff line number Diff line change
@@ -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! <br /><br />Click <a href="${url}?access_token=${
info.accessToken.id}">here</a> to create a new password.` +
' <br /><br /> If you have not requested a password change,' +
'please ignore this email. <br /><br />Webmaster';
Account.app.models.Email.send({
to: info.email,
from: '{{name}} <[email protected]>',
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á! <br /><br /><a href="${url}?access_token=${
options.accessToken.id}">Clique aqui</a> para criar uma nova senha.` +
' <br /><br /> 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'},
}
);
}