I'm trying to send a re-verification email when a user doesn't receives the verification email after createing the user. Below is the configuration:
Users.getVerifyOptions = function() {
const defaultOptions = {
type: 'email',
from: 'no-reply@example.com',
subject: 'Re-verification Email',
template: path.resolve(__dirname, '../../server/views/verify.ejs'),
redirect: '',
host: "mywebsite.com",
port: 80
};
return Object.assign({}, this.settings.verifyOptions || defaultOptions);
};
//Re-verify Method to render
Users.afterRemote('prototype.verify', function(context, user, next) {
context.res.render('response', {
title: 'A Link to reverify your identity has been sent '+
'to your email successfully',
content: 'Please check your email and click on the verification link '+
'before logging in',
redirectTo: '',
redirectToLinkText: ''
});
});
Now the problem is, if the user is already verified, and the option emailVerified
is set to true in the database then also the request to POST /users/{id}/verify
generates a new verification token and sends an email. Is there any way to stop this endpoint to send any email if the emailverified
option is set to true
and return a message saying Email is already verified ?
You can try by check user.emailVerified is
true
orfalse
Example :
Users.afterRemote('prototype.verify', function (context, user, next) {
if(!user.emailVerified) {
context.res.render('response', {
title: 'A Link to reverify your identity has been sent ' +
'to your email successfully',
content: 'Please check your email and click on the verification link ' +
'before logging in',
redirectTo: '',
redirectToLinkText: ''
});
} else {
return next('already verified');
}
});
Or else you can check verified user when login, when not verified send verification link.
Example :
app.post('/login', function(req, res) {
User.login({
email: req.body.email,
password: req.body.password
}, 'user', function(err, token) {
if (err) {
if(err.details && err.code === 'LOGIN_FAILED_EMAIL_NOT_VERIFIED'){
//send verification link
}
}
})
});