Starpi forgot password custom routes

 // ‘use strict’;

/**
* This is controller for `essential`
*/

module.exports = {
async sendEmail(ctx) {
try {
let userResponse = await strapi.db.query(‘plugin::users-permissions.user’).findOne({
where: {
email: ctx.request.body.email
},
});
if (userResponse) {

let resetToken = function (length) {
var a = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890”.split(“”);
var b = [];
for (var i = 0; i < length; i++) {
var j = (Math.random() * (a.length1)).toFixed(0);
b[i] = a[j];
}
return b.join(“”);
}

let token = resetToken(60)
let user = {};
user.resetPasswordToken = token;

let userData = await strapi.entityService.update(‘plugin::users-permissions.user’, userResponse.id, {
data: user
});
const emailOptions = {
to: ctx.request.body.email,
from: ‘kahnuc.swain@nettantra.net’,
// cc: ‘kahnuc.swain@nettantra.net’,
// bcc: ‘kahnuc.swain@nettantra.net’,
replyTo: ‘kahnuc.swain@nettantra.net’,
dynamic_template_data: {
“name”: userResponse.username,
“resetLink”: `http://localhost:1337/?token=${token}`,
“siteUrl” : “http://localhost:1337/”,
“english”: true,
// “date”: “April 1st, 2021”
},
//d-eac1715b5f544630a46a229d03e3f4d2

// template_id: “d-43ec2d69630a47f785e5e3095c3cc6f2”

template_id: “d-36018da690af47dcbdb07554654a2260”
};
const emailTemplate = {
subject: ‘Welcome fhfh’,
text: `Welcome on mywebsite.fr!
Your account is now linked with: .`,
html: `<h1>Welcome on mywebsite.fr!</h1>
<p>Your account is now linked with: .<p>`,
};

const emailResponse = await strapi.plugins[’email’].services.email.sendTemplatedEmail(

emailOptions,

emailTemplate

);
return ‘Email For Password Reset Has Been Sent’;

} else {
return `Email Doesn’t Exist `;
}

} catch (error) {

return ‘error ‘ + error;

}

},

}

Leave a Comment

Your email address will not be published. Required fields are marked *