feat(verify): use user.verify
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2022-10-17 07:22:22 +02:00
parent 3e746ceb7a
commit 6bfd54fc71
5 changed files with 36 additions and 92 deletions

View File

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

View File

@ -3,7 +3,7 @@ const md5 = require('md5');
const app = require('../../loopback/server/server.js');
const dataSources = require('../../loopback/server/datasources.json');
const LoopBackContext = require('loopback-context');
const {Email} = require('vn-print');
const path = require('path');
module.exports = Self => {
require('../methods/account/login')(Self);
@ -34,109 +34,58 @@ module.exports = Self => {
});
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 userId = ctx.instance.id;
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',
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
};
console.log('userId', userId);
const user = await models.user.findById(userId);
user.verify(options)
.then(res => console.log('> Verification email sent:', res));
/*
const changes = ctx.data || ctx.instance;
if (ctx.isNewInstance || !changes.email) return;
const userId = ctx.currentInstance.id;
const user = await models.Account.findById(userId);
if (user.email == changes.email) return;
const httpCtx = {req: loopBackContext.active};
const httpRequest = httpCtx.req.http.req;
const headers = httpRequest.headers;
const origin = headers.origin;
const $t = httpRequest.__;
const title = $t('Verify email');
const body = `
<p>
${$t(`Click on the following link to verify this email. If you haven't requested this email, just ignore it`)}:
</p>
</b>
<a href="${origin}/#!/account/${userId}/basic-data?emailVerified">
${title}
</a>`;
const url = app.get('rootUrl') || app.get('url');
console.log(app.get('rootUrl'));
console.log(app.get('url'));
console.log(dataSources);
const options = {
type: 'email',
to: 'alexm@verdnatura.es',
from: dataSources.app.senderEmail,
subject: title,
template: body,
redirect: `${origin}#/login/${instance.email}?emailConfirmed`,
host: dataSources.smtp.host,
port: dataSources.smtp.port,
protocol: 'https',
user: Self
};
const params = {
recipient: 'alexm@verdnatura.es',
url: `${origin}/#!/verified`
};
console.log(params);
const sendEmail = new Email('email-verify', params);
// ctx.instance.verify(options);
sendEmail.send();*/
await user.verify(options);
});
Self.afterRemote('prototype.patchAttributes', async(ctx, instance) => {
if (!ctx.args || !ctx.args.data.email) return;
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 userId = ctx.instance.id;
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',
to: instance.email,
from: dataSources.email.transports[0].auth.from,
subject: 'Verify email',
template: path.resolve(__dirname, '../views/verify.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
};
console.log(userId);
const user = await models.user.findById(userId);
user.verify(options)
.then(res => console.log('> Verification email sent:', res));
await user.verify(options);
});
Self.remoteMethod('getCurrentUserData', {

View File

@ -114,14 +114,13 @@
"connector": "mail",
"transports": [{
"type": "smtp",
"host": "smtp.verdnatura.es",
"host": "localhost",
"secure": true,
"port": 465,
"auth": {
"user": "nocontestar",
"pass": "",
"from": "nocontestar@verdnatura.es"
"user": "",
"pass": ""
}
}]
}
}
}

View File

@ -3,15 +3,8 @@ import Section from 'salix/components/section';
export default class Controller extends Section {
$onInit() {
if (this.$params.emailVerified) {
const params = {
emailVerified: true
};
return this.$http.patch(`Accounts/${this.$params.id}`, params)
.then(() => {
this.vnApp.showSuccess(this.$t('Email verified successfully!'));
});
}
if (this.$params.emailConfirmed)
this.vnApp.showSuccess(this.$t('Email verified successfully!'));
}
onSubmit() {

View File

@ -74,7 +74,7 @@
}
},
{
"url": "/basic-data?access_token&emailVerified",
"url": "/basic-data?access_token&emailConfirmed",
"state": "account.card.basicData",
"component": "vn-user-basic-data",
"description": "Basic data",