How can I implement (forgotten password)?

Hello!, please help me! :frowning:

What should I write to implement (forgotten password)?

I’ve added the ‘email’ package

accounts-base
accounts-password
accounts-ui // Does not appear (Forgot password?) Why?

Can you write me the code here?

Code:

loginRegister.js

Router.route("login", {
	path: "/login"
});

Router.route("register", {
	path: "/register"
});

Template.login.events({
	'click #login': function() {
		Meteor.loginWithPassword($("#email").val(), $("#password").val(), function(err) {
			if(err) {
				switch(err.error) {
					case 400:
						confirm("La preghiamo di riempire tutti i campi");
					break;
					case 403:
						alert("Credenziali di login errate.");
					break;
					default:
						alert("Errore generale. Per favore riportare l'erorre "+err.error+" a Omegasus Connect")
					break;
				}
			}else
				Router.go(Router.path("myZizool"));
		});
	}
});

function firstBig(x) {
	if(x && x.length > 1) {
		x = x.toLowerCase();
		x = x.charAt(0).toUpperCase() + x.slice(1);
	}
	return x;
}

Template.register.events({
	'click #register': function() {
		var fields = [
			"email",
			"username",
			"password",
			"confirmPassword",
			//"company",
			"name",
			"surname",
			"address",
			"zipcode",
			"city",
			"country",
			"province",
			"telephone",
			"cf",
			"cnIdentity",
			"pIVA"
		];
		for(field in fields) {
			if(fields[field] == "pIVA") {
				if($("#company").val() != "" && $("#"+fields[field]).val() == "") {
					alert("Hai inserito un Azienda, per favore indicare anche una Partita IVA");
					return false;
				}
			}else if($("#"+fields[field]).val() == "") {
				alert("Per favore riempire tutti i campi");
				return false;
			}
			// TEST commit senza pass
		}
		if(!$("#privacy").is(":checked")) {
			alert("Devi accettare l'Informativa sulla Privacy");
			return false;
		}
		if(!$("#terms").is(":checked")) {
			alert("Devi accettare i termini di servizio");
			return false;
		}
		if($("#password").val() != $("#confirmPassword").val()) {
			alert("Le password non corrispondono");
			return false;
		}
		Accounts.createUser({
			email: $("#email").val(),
			username: $("#username").val(),
			password: $("#password").val(),
			profile: {
				name: firstBig($("#shownName").val()),
				company: firstBig($("#company").val()),
				name: firstBig($("#name").val()),
				surname: firstBig($("#surname").val()),
				address: firstBig($("#address").val()),
				zipcode: firstBig($("#zipcode").val()),
				city: firstBig($("#city").val()),
				country: firstBig($("#country").val()),
				province: firstBig($("#province").val()),
				telephone: $("#countryCode").val()+$("#telephone").val(),
				cf: firstBig($("#cf").val()),
				pIVA: firstBig($("#pIVA").val())
			}
		}, function(err) {
			if(err) {
				switch(err.error) {
					case 403:
						switch(err.reason) {
							case "Email already exists.":
								alert("Email gia esistente");
							break;
							case "Username already exists.":
								alert("Username gia esistente");
							break;
						}
					break;
					default:
						alert("Errore generale. Per favore riportare l'erorre "+err.error+" a Omegasus Connect");
					break;
				}
			}else
				Router.go(Router.path("verify"))
		});
	}
});

Run an HTML template like this?

<template name="recoverPassword">
  <div class="main-page">
    <div class="profile-container">
      <center>
        <div id="warning"></div>
        <div id="success"></div>
        {{#if resetPassword}}
        <form id="resetPasswordForm" method="post">
            <input id="resetPasswordPassword" name="password" placeholder="New Password" type="password" >
            <input id="resetPasswordPasswordConfirm" name="password-confirm" placeholder="Confirm" type="password" >
            <input class="btn-submit" type="submit" value="Reset">
        </form>
        {{else}}
        <form id="sendEmail">
          <div class="log-classic">
            <div class="show-no-error">
              <input class="log-upper-box" type="email" id="recover-email" placeholder='{{_ "global.emailHolder"}}' required>
            </div>
            <div class="bt-act-in">
              <input type="submit" class="btn bt-signin" value="{{_ "recover.sendMail"}}"/>
            </div>
          </div>
        </form>
        {{/if}}
      </center>
    </div>
  </div>
</template>

smtp.js

// server/smtp.js
Meteor.startup(function () {
  smtp = {
    username: 'novimar.net@gmail.com',   // eg: server@gentlenode.com
    password: 'XXXXXXXXX',   // eg: 3eeP1gtizk5eziohfervU
    server:   'smtp.gmail.com',  // eg: mail.gandi.net
    port: 25
  }

  process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) + ':' + smtp.port;
});

Can not anyone help me?

Did you get a chance to give this a try: https://docs.meteor.com/api/passwords.html#Accounts-forgotPassword

Yes, but I can not get it running :frowning:

Always appear ‘User not found [403]’, even if the user exists!


if(Meteor.isClient){
  Template.recoverPassword.rendered = function(){
    if (Accounts._resetPasswordToken){
      Session.set('resetPassword', Accounts._resetPasswordToken);
    }
  }

  Template.recoverPassword.helpers({
    resetPassword: function(){
      return Session.get('resetPassword') || false;
    }
  });

  Template.recoverPassword.events({
    'submit #sendEmail': function(event, template){
      event.preventDefault();
      template.find('#warning').innerHTML = "";
      template.find('#success').innerHTML = "";
      var email = template.find('#recover-email').value.trim();
      Accounts.forgotPassword({email: email}, function(err) {
        if (err){
          if (err.message === 'User not found [403]') {
            template.find('#warning').innerHTML = "Nessun account è registrato con questa e-mail";
          }else{
            console.log(err.message);
            template.find('#warning').innerHTML = "Ci dispiace ma qualcosa è andato storto. :(";
          }
        }else{
          template.find('#success').innerHTML = "Un e-mail è stata appena inviata!";
        }
      });
    },
    'submit #resetPassword': function(e, t) {
      e.preventDefault();
      var password = t.find('#resetPasswordPassword').value;
      var passwordConfirm = t.find('#resetPasswordPasswordConfirm').value;
          console.log("pwd : " + password);
      if (true){
        Accounts.resetPassword(Session.get('resetPassword'), password, function(err) {
          if (err) {
            console.log(err.message);
            template.find('warning').innerHTML = "Ci dispiace ma qualcosa è andato storto. :(";
          } else {
            console.log("wesh");
            template.find('success').innerHTML = "YLa tua Password è stata aggiornata!";
            Session.set('resetPassword', null);
          }
        });
      }
    }
  });
}

Now working!, but not send mail :frowning:

Can you see the mail getting printed on your server console during development? If that’s the case, you need to set up MAIL_URL to point at a proper smtp server.

I’m configuring it, using SMTP Gmail, but it does not work, Internal Error 500 appears in the Log.

Where is MAIL_URL configured? In mup.js?

   env: {
      // TODO: Change to your app's url
      // If you are using ssl, it needs to start with https://
      ROOT_URL: 'http://manager2.novimar.net/',
      MONGO_URL: 'mongodb://localhost/meteor',
      MAIL_URL: 'smtp://USERNAME:PASSWORD@smtp.gmail.com:465',
      PORT: 5040,
    },

PS: The Google Account is Enable SMTP and “Less Safe App” Settings (Disabled)

For port 465 you should specify smtps://. Meteor no longer tries to fix this discrepancy.

I tried to use smtps:// but still from error 500

This is the “Password Recovery” code:

recover.js

if(Meteor.isClient){
  Template.recoverPassword.rendered = function(){
    if (Accounts._resetPasswordToken){
      Session.set('resetPassword', Accounts._resetPasswordToken);
    }
  }

  Template.recoverPassword.helpers({
    resetPassword: function(){
      return Session.get('resetPassword') || false;
    }
  });

  Template.recoverPassword.events({
    'submit #sendEmail': function(event, template){
      event.preventDefault();
      template.find('#warning').innerHTML = "";
      template.find('#success').innerHTML = "";
      var email = template.find('#recover-email').value.trim();
      Accounts.forgotPassword({email: email}, function(err) {
        if (err){
          if (err.message === 'User not found [403]') {
            template.find('#warning').innerHTML = "Nessun account è registrato con questa e-mail";
          }else{
            console.log(err.message);
            template.find('#warning').innerHTML = "Ci dispiace ma qualcosa è andato storto. :(";
          }
        }else{
          template.find('#success').innerHTML = "Un e-mail è stata appena inviata!";
        }
      });
    },
    'submit #resetPassword': function(e, t) {
      e.preventDefault();
      var password = t.find('#resetPasswordPassword').value;
      var passwordConfirm = t.find('#resetPasswordPasswordConfirm').value;
          console.log("pwd : " + password);
      if (true){
        Accounts.resetPassword(Session.get('resetPassword'), password, function(err) {
          if (err) {
            console.log(err.message);
            template.find('warning').innerHTML = "Ci dispiace ma qualcosa è andato storto. :(";
          } else {
            console.log("wesh");
            template.find('success').innerHTML = "La tua Password è stata aggiornata!";
            Session.set('resetPassword', null);
          }
        });
      }
    }
  });
}

Could you please paste here the full error message you see on the server console.

How can I see the Log ?, I always used Mozilla Firefox for Logs

The server logs are on your terminal where you had typed in meteor run.

My App is Deployed to a Custom Server by mup setup and mup deploy

Then you should check your server logs using mup. Did you read the mup guide? https://github.com/zodern/meteor-up#other-utility-commands should help you get started.

Here: mup logs

[207.154.255.13]
[207.154.255.13]Exception while invoking method 'forgotPassword' Error: 140619189524256:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794:
[207.154.255.13]    at Object.Future.wait (/bundle/bundle/programs/server/node_modules/fibers/future.js:449:15)
[207.154.255.13]    at Mail._syncSendMail (packages/meteor.js:213:24)
[207.154.255.13]    at smtpSend (packages/email.js:116:13)
[207.154.255.13]    at Object.Email.send (packages/email.js:174:5)
[207.154.255.13]    at AccountsServer.Accounts.sendResetPasswordEmail (packages/accounts-password/password_server.js:614:9)
[207.154.255.13]    at [object Object].Meteor.methods.forgotPassword (packages/accounts-password/password_server.js:546:12)
[207.154.255.13]    at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1737:12)
[207.154.255.13]    at packages/ddp-server/livedata_server.js:719:19
[207.154.255.13]    at [object Object]._.extend.withValue (packages/meteor.js:1122:17)
[207.154.255.13]    at packages/ddp-server/livedata_server.js:717:40
[207.154.255.13]    - - - - -
[207.154.255.13]
[207.154.255.13]    at Error (native)

What’s your MAIL_URL setting? Are you sure it is correct? If it starts with smtps:// try changing it to smtp:// or vice versa.

Have tried both smtps:// and smtp:// no results, will GMAIL be?

I have followed this guide:

Can you help me? :frowning:

Try this.
Remove the MAIL_URL from mup.js
In Meteor.startup() on the server, put in:

process.env.MAIL_URL = "smtp://USERNAME:PASSWORD@smtp.gmail.com:465";

Also keep in mind that if you are including the @gmail.com (or a custom domain) in the USERNAME, you must replace the @ with %40. So it would look something like this:

process.env.MAIL_URL = "smtp://myname%40gmail.com:PASSWORD@smtp.gmail.com:465";

I’ve tried it as you said, but I get this error:

[207.154.255.13]====== BEGIN MAIL #0 ======
[207.154.255.13](Mail not sent; to enable sending, set the MAIL_URL environment variable.)
[207.154.255.13]Content-Type: text/plain
[207.154.255.13]From: Meteor Accounts <no-reply@meteor.com>
[207.154.255.13]To: novimar.net@gmail.com
[207.154.255.13]Subject: How to reset your password on manager2.novimar.net
[207.154.255.13]Message-ID: <6e192328-5100-d4ba-0b52-df2145c05751@meteor.com>
[207.154.255.13]Content-Transfer-Encoding: quoted-printable
[207.154.255.13]Date: Wed, 12 Jul 2017 13:20:28 +0000
[207.154.255.13]MIME-Version: 1.0
[207.154.255.13]
[207.154.255.13]Hello Pasquale,
[207.154.255.13]
[207.154.255.13]To reset your password, simply click the link below.
[207.154.255.13]
[207.154.255.13]http://manager2.novimar.net/#/reset-password/6qUV0maRsn66eak-mzT3_-bl7Po_8G=
[207.154.255.13]ZCMaYIzYM0a2I
[207.154.255.13]
[207.154.255.13]Thanks.
[207.154.255.13]====== END MAIL #0 ======

smtp.js

Meteor.startup(function () {
    process.env.MAIL_URL="smtps://novimar.net%40gmail.com:PASSWORD@smtp.gmail.com:465"; 
});