0

This part takes care of sending the link to the user. So far it does the job perfectly and I always get the email

app = Flask(__name__)
app.secret_key = 'b11223344AaadD$$r.,IIr]]tP[tu@urr'
app.config.from_pyfile('config.cfg')

mail = Mail(app)

s = URLSafeTimedSerializer(app.config['SECRET_KEY'])

engine = create_engine("postgresql://postgres:andersen23@localhost:5432/test")
db = scoped_session(sessionmaker(bind=engine))

@app.route('/recover', methods=['POST', 'GET'])
def recover():
   headline = 'Recover Your Password'
   alert = 'Type Your Email'
   alert_ = ''
   if request.method == 'GET':
      return render_template('recover.html', headline=headline, alert1=alert, alert=alert_)

   email = request.form.get('email')
   session['email'] = email

   mail1 = db.execute("SELECT contact0.email FROM person JOIN contact0 ON contact0.id = person.id 
   WHERE email = :email",
   {"email": email}).fetchone()

   token = s.dumps(email, salt='confirm')
   link = url_for('confirm', token=token, _external=True)

   msg = Message('Confirm Email', sender='esdavitnem@gmail.com', recipients=[email])

   name = db.execute(
   "SELECT person.first_name FROM person JOIN contact0 ON contact0.id = person.id WHERE email = 
   :username",
   {"username": email}).fetchone()


   if not isinstance(mail1, type(None)):
      alert_ = f"Link Sent to {email}! \n Expires in 5 minutes!"
      msg.body = f"Dear {name[0]}, \n\nYour link is {link} \n\nBest,\nDavid from Drunkify"
      mail.send(msg)

      return render_template('recover.html', headline=headline, alert1=alert, alert=alert_)

   if isinstance(mail1, type(None)):
      alert_ = f"No user exists with {email} email"
      return render_template('recover.html', headline=headline, alert1=alert, alert=alert_)

This part takes care of opening the token and showing an html code to the user.

My main issue is that render_template only works with one html file in my templates.

register1.html is a page for my user to change their password. But the code error message keeps suggesting me to redirect them to register.html

@app.route('/confirm_email/<token>')
def confirm(token):
   headline = 'Type Your New Password'
   try:
      email = s.loads(token, salt='confirm', max_age=300)
   
   except SignatureExpired:
     return 'The Token Expired'
   return render_template('register1.html', headline=headline)

Register1.html

<!DOCTYPE html>
<html lang="en">

<head>
<!-- Title Page-->
<title>Recover Your Password</title>

<link href="../static/main.css" rel="stylesheet" media="all">
</head>

<body>
<div class="page-wrapper bg-gra-01 p-t-180 p-b-100 font-poppins">
<div class="wrapper wrapper--w780">
    <div class="card card-3">
        <div class="card-heading"></div>
        <div class="card-body">
            <h2 class="title">Change Your Password</h2>
            <form action = "{{ url_for('confirm') }}" method="post">
                <div class="form-group">
                     <input class="form-control" type="password" 
   
 placeholder="New Password" name="password" required>
                    <div class="help-block with-errors"></div>
                </div>

                <div class="p-t-10">
                    <button type="submit" class="btn btn--pill btn--green">
                        Submit
                    </button>
                </div>
            </form>
        </div>
    </div>
    </div>
 </div>

</body>

</html>

1 Answers1

0

What you're seeing is a mismatch in expectations between

def confirm(token):

which expects an argument, and

{{ url_for('confirm') }}

which isn't providing one, but lives in a form that does provide password as part of the POST parameters (a different mechanism). I'm wondering if you didn't intend to do

{{ url_for('recover') }}

instead.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46