diff --git a/back/models/account.js b/back/models/account.js index 5082b2351..55c3ec8f6 100644 --- a/back/models/account.js +++ b/back/models/account.js @@ -1,7 +1,7 @@ /* eslint max-len: ["error", { "code": 150 }]*/ const md5 = require('md5'); const app = require('../../loopback/server/server.js'); -const dataSources = require('../../print/config/print.json'); +const dataSources = require('../../loopback/server/datasources.json'); const LoopBackContext = require('loopback-context'); const {Email} = require('vn-print'); @@ -33,10 +33,35 @@ module.exports = Self => { ctx.data.password = md5(ctx.data.password); }); - Self.observe('before save', async(ctx, instance) => { + Self.afterRemote('create', async(ctx, instance) => { console.log(instance); const models = Self.app.models; + const userId = ctx.instance.id; const loopBackContext = LoopBackContext.getCurrentContext(); + const httpCtx = {req: loopBackContext.active}; + const httpRequest = httpCtx.req.http.req; + const headers = httpRequest.headers; + const origin = headers.origin; + + const user = await models.User.findById(userId); + console.log(user); + + const options = { + type: 'email', + to: 'alexm@verdnatura.es', + from: dataSources.transports[0].host, + subject: 'Thanks for registering', + template: path.resolve(__dirname, '../../views/verify.ejs'), + redirect: `${origin}/!#/login/${instance.email}?emailConfirmed`, + host: 'verdnatura.es', + port: '1234', + protocol: 'https', + user: Self + }; + + await models.User.verify(options) + .then(res => console.log('> Verification email sent:', res)); + /* const changes = ctx.data || ctx.instance; if (ctx.isNewInstance || !changes.email) return; @@ -86,7 +111,33 @@ module.exports = Self => { const sendEmail = new Email('email-verify', params); // ctx.instance.verify(options); - sendEmail.send(); + sendEmail.send();*/ + }); + + Self.afterRemote('prototype.patchAttributes', async(ctx, instance) => { + console.log(instance); + const models = Self.app.models; + const userId = ctx.instance.id; + const loopBackContext = LoopBackContext.getCurrentContext(); + const httpCtx = {req: loopBackContext.active}; + const httpRequest = httpCtx.req.http.req; + const headers = httpRequest.headers; + const origin = headers.origin; + + const options = { + type: 'email', + to: 'alexm@verdnatura.es', + from: dataSources.email.transports[0].host, + subject: 'Thanks for registering', + template: '../views/verify.ejs', + redirect: `${origin}/!#/login/${instance.email}?emailConfirmed`, + host: 'verdnatura.es', + port: '1234', + protocol: 'https', + user: Self + }; + await models.user.verify(options) + .then(res => console.log('> Verification email sent:', res)); }); Self.remoteMethod('getCurrentUserData', { @@ -94,12 +145,12 @@ module.exports = Self => { accepts: [ { arg: 'ctx', - type: 'Object', + type: 'object', http: {source: 'context'} } ], returns: { - type: 'Object', + type: 'object', root: true }, http: { @@ -120,7 +171,7 @@ module.exports = Self => { * * @param {Integer} userId The user id * @param {String} name The role name - * @param {Object} options Options + * @param {object} options Options * @return {Boolean} %true if user has the role, %false otherwise */ Self.hasRole = async function(userId, name, options) { @@ -132,8 +183,8 @@ module.exports = Self => { * Get all user roles. * * @param {Integer} userId The user id - * @param {Object} options Options - * @return {Object} User role list + * @param {object} options Options + * @return {object} User role list */ Self.getRoles = async(userId, options) => { let result = await Self.rawSql( diff --git a/back/views/newVerify.ejs b/back/views/newVerify.ejs new file mode 100644 index 000000000..49f4e981a --- /dev/null +++ b/back/views/newVerify.ejs @@ -0,0 +1,6 @@ +
+ Thanks for registering, +
++ Click here to confirm your email address. +
diff --git a/back/views/verify.ejs b/back/views/verify.ejs new file mode 100644 index 000000000..90a434463 --- /dev/null +++ b/back/views/verify.ejs @@ -0,0 +1,3 @@ ++ Click here to confirm your email address. +
diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index 5dade9c2e..2a410f0b5 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -108,5 +108,20 @@ "allowedContentTypes": [ "application/x-7z-compressed" ] - } -} \ No newline at end of file + }, + "email": { + "name": "email", + "connector": "mail", + "transports": [{ + "type": "smtp", + "host": "smtp.verdnatura.es", + "secure": true, + "port": 465, + "auth": { + "user": "nocontestar", + "pass": "", + "from": "nocontestar@verdnatura.es" + } + }] + } +}