feat(account): email-verify refs #4077
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2022-10-26 13:28:26 +02:00
parent a07a45c1b6
commit cf24301bfe
10 changed files with 33 additions and 114 deletions

View File

@ -53,9 +53,6 @@
"EmailUser": {
"dataSource": "vn"
},
"Email": {
"dataSource": "email"
},
"Image": {
"dataSource": "vn"
},

View File

@ -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', {

View File

@ -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);
});
});

View File

@ -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"
}

View File

@ -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"
}

View File

@ -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": ""
}
}]
}
}

View File

@ -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",

View File

@ -24,7 +24,7 @@
<div class="grid-row">
<div class="grid-block vn-pa-ml">
<p>
{{ $t(`Click on the following link to verify this email. If you haven't requested this email, just ignore it.`) }}
{{ $t(`click`) }}
<a :href="url">{{ $t('subject') }}</a>
</p>
</div>

View File

@ -0,0 +1,3 @@
subject: Email Verify
title: Email Verify
click: Click on the following link to verify this email. If you haven't requested this email, just ignore it

View File

@ -1,3 +1,3 @@
subject: Verificar correo
title: Verificar correo
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,
click: Pulsa en el siguiente link para verificar este correo. Si no has pedido este correo, simplemente ignóralo