use ejs template
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2022-10-11 14:56:33 +02:00
parent 2e36164a98
commit acc88c8239
4 changed files with 85 additions and 10 deletions

View File

@ -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(

6
back/views/newVerify.ejs Normal file
View File

@ -0,0 +1,6 @@
<p>
Thanks for registering,
</p>
<p>
Click <a href="<%=verifyHref%>">here</a> to confirm your email address.
</p>

3
back/views/verify.ejs Normal file
View File

@ -0,0 +1,3 @@
<p>
Click <a href="<%=verifyHref%>">here</a> to confirm your email address.
</p>

View File

@ -108,5 +108,20 @@
"allowedContentTypes": [
"application/x-7z-compressed"
]
}
}
},
"email": {
"name": "email",
"connector": "mail",
"transports": [{
"type": "smtp",
"host": "smtp.verdnatura.es",
"secure": true,
"port": 465,
"auth": {
"user": "nocontestar",
"pass": "",
"from": "nocontestar@verdnatura.es"
}
}]
}
}