diff --git a/back/model-config.json b/back/model-config.json index f7456ac4b..830a78fd4 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -53,9 +53,6 @@ "EmailUser": { "dataSource": "vn" }, - "Email": { - "dataSource": "email" - }, "Image": { "dataSource": "vn" }, diff --git a/back/models/account.js b/back/models/account.js index 480a84e77..c2502380a 100644 --- a/back/models/account.js +++ b/back/models/account.js @@ -1,11 +1,7 @@ /* eslint max-len: ["error", { "code": 150 }]*/ const md5 = require('md5'); -const app = require('../../loopback/server/server.js'); -const dataSources = require('../../loopback/server/datasources.json'); const LoopBackContext = require('loopback-context'); -const path = require('path'); -// const {Email} = require('vn-print'); -// const qs = require('querystring'); +const {Email} = require('vn-print'); module.exports = Self => { require('../methods/account/login')(Self); @@ -35,33 +31,6 @@ module.exports = Self => { ctx.data.password = md5(ctx.data.password); }); - Self.afterRemote('create', async(ctx, instance) => { - const models = Self.app.models; - - const loopBackContext = LoopBackContext.getCurrentContext(); - const httpCtx = {req: loopBackContext.active}; - const httpRequest = httpCtx.req.http.req; - const headers = httpRequest.headers; - const origin = headers.origin; - - const userId = ctx.instance.id; - const options = { - type: 'email', - to: instance.email, - from: dataSources.email.transports[0].auth.from, - subject: 'hanks for registering', - template: path.resolve(__dirname, '../views/newVerify.ejs'), - redirect: `${origin}/#!/account/${instance.id}/basic-data?emailConfirmed`, - host: origin.split(':')[1].split('/')[2], - port: origin.split(':')[2], - protocol: origin.split(':')[0], - user: Self - }; - - const user = await models.user.findById(userId); - await user.verify(options); - }); - Self.afterRemote('prototype.patchAttributes', async(ctx, instance) => { if (!ctx.args || !ctx.args.data.email) return; const models = Self.app.models; @@ -71,52 +40,40 @@ module.exports = Self => { const httpRequest = httpCtx.req.http.req; const headers = httpRequest.headers; const origin = headers.origin; + const url = origin.split(':'); const userId = ctx.instance.id; const user = await models.user.findById(userId); - /* //generate verification url - const generateVerificationToken = models.User.generateVerificationToken(user); - verifyOptions.verifyHref = - verifyOptions.protocol + - '://' + - verifyOptions.host + - displayPort + - urlPath + - '?' + qs.stringify({ - uid: '' + verifyOptions.user[pkName], - redirect: verifyOptions.redirect, - }); - verifyOptions.verifyHref += - verifyOptions.verifyHref.indexOf('?') === -1 ? '?' : '&'; - verifyOptions.verifyHref += 'token=' + user.verificationToken; + class Mailer { + async send(verifyOptions, cb) { + const params = { + url: verifyOptions.verifyHref, + recipient: verifyOptions.to, + lang: ctx.req.getLocale() + }; + + const email = new Email('email-verify', params); + email.send(); + + cb(null, verifyOptions.to); + } + } - verifyOptions.verificationToken = user.verificationToken; - verifyOptions.text = verifyOptions.text || g.f('Please verify your email by opening ' + - 'this link in a web browser:\n\t%s', verifyOptions.verifyHref); - verifyOptions.text = verifyOptions.text.replace(/\{href\}/g, verifyOptions.verifyHref); - const email = new Email('email-verify', { - recipient: instance.email, - lang: ctx.req.getLocale(), - url: - }); - await email.send(); -*/ const options = { type: 'email', to: instance.email, - from: 'test@test.es', // dataSources.email.transports[0].auth.from, - subject: 'Verify email', - template: path.resolve(__dirname, '../views/verify.ejs'), // user.country.code = 'es' ? verifyES.ejs : verify.ejs + from: {}, redirect: `${origin}/#!/account/${instance.id}/basic-data?emailConfirmed`, - host: origin.split(':')[1].split('/')[2], - port: origin.split(':')[2], - protocol: origin.split(':')[0], + template: false, + mailer: new Mailer, + host: url[1].split('/')[2], + port: url[2], + protocol: url[0], user: Self }; - console.log(options); - // await user.verify(options); + await user.verify(options); }); Self.remoteMethod('getCurrentUserData', { diff --git a/back/models/specs/account.spec.js b/back/models/specs/account.spec.js index 7e4d877f1..81496a15f 100644 --- a/back/models/specs/account.spec.js +++ b/back/models/specs/account.spec.js @@ -32,30 +32,4 @@ describe('loopback model Account', () => { expect(result).toBeFalsy(); }); - - it('should send email when change email', async() => { - const tx = await models.Account.beginTransaction({}); - const newEmail = 'emailNotVerified@mydomain.com'; - - let lastEmail; - try { - const options = {transaction: tx}; - - const account = await models.Account.findById(userId, null, options); - await account.updateAttribute('email', newEmail, options); - - [lastEmail] = await models.Mail.find({ - where: { - receiver: newEmail - } - }, options); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - - expect(lastEmail.receiver).toEqual(newEmail); - }); }); diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 4d1f63130..c6351d5bd 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -135,5 +135,6 @@ "Click on the following link to verify this email. If you haven't requested this email, just ignore it": "Click on the following link to verify this email. If you haven't requested this email, just ignore it", "Password does not meet requirements": "Password does not meet requirements", "You don't have privileges to change the zone": "You don't have privileges to change the zone or for these parameters there are more than one shipping options, talk to agencies", - "Not enough privileges to edit a client": "Not enough privileges to edit a client" + "Not enough privileges to edit a client": "Not enough privileges to edit a client", + "Email verify": "Email verify" } diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 174f5921e..ad8e44bde 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -239,5 +239,6 @@ "Landing cannot be lesser than shipment": "Landing cannot be lesser than shipment", "Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador", "Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o por un administrador", - "Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente" + "Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente", + "Email verify": "Correo de verificación" } diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index 3a7083318..00f6bf624 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -112,19 +112,5 @@ "allowedContentTypes": [ "application/x-7z-compressed" ] - }, - "email": { - "name": "email", - "connector": "mail", - "transports": [{ - "type": "smtp", - "host": "localhost", - "secure": true, - "port": 465, - "auth": { - "user": "", - "pass": "" - } - }] } } diff --git a/package.json b/package.json index 26c164832..abf7c9e4b 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "jsdom": "^16.7.0", "jszip": "^3.10.0", "ldapjs": "^2.2.0", - "loopback": "^3.26.0", + "loopback": "^3.28.0", "loopback-boot": "3.3.1", "loopback-component-explorer": "^6.5.0", "loopback-component-storage": "3.6.1", diff --git a/print/templates/email/email-verify/email-verify.html b/print/templates/email/email-verify/email-verify.html index 0eae8c57a..b8a6e263c 100644 --- a/print/templates/email/email-verify/email-verify.html +++ b/print/templates/email/email-verify/email-verify.html @@ -24,7 +24,7 @@
- {{ $t(`Click on the following link to verify this email. If you haven't requested this email, just ignore it.`) }} + {{ $t(`click`) }} {{ $t('subject') }}