GrLdev / Rate-My-Student-Home

A website designed to allow students rate their student homes in Cardiff.
3 stars 0 forks source link

Email verification #45

Open theobaur13 opened 1 year ago

theobaur13 commented 1 year ago

Set up a rate-my-student-home email to send emails from, create a verification link lasting 48h that needs to be clicked before a review is shown publically on the website.

Snippet from previous project:

# __init__.py
from flask_redmail import RedMail
#Emailer
app.config["EMAIL_HOST"] = email_host
app.config["EMAIL_PORT"] = email_port
app.config['EMAIL_USER'] = email_user
app.config['EMAIL_PASSWORD'] = email_password
class Mailer():
    def __init__(self):
        self.sender = RedMail(app)

    def send_html(self, to, html, subject):
        self.sender.send(
            subject=subject,
            receivers=[to],
            html=html
        )

mailer = Mailer()

# routes.py
from app import mailer
from itsdangerous import URLSafeTimedSerializer, SignatureExpired

s = URLSafeTimedSerializer("SECRET_KEY_CHANGE_ME")

token = s.dumps(user.email, salt="email-confirm")
confirm_url = url_for("confirm_email", token=token, _external=True)
email_content = render_template("email_accountcreation.html", confirm_url=confirm_url)
mailer.send_html(user.email, email_content, "Registration")

@app.route("/confirm_email/<token>")
def confirm_email(token):
    try:
        email = s.loads(token, salt="email-confirm", max_age=172800)
    except SignatureExpired:
        flash("The token is expired!", "danger")

# email_accountcreation.html
<p>Thank you for registering with Toolshare!</p>
<p>Please verify your email by clicking the following link</p>
<p>
  <a href="{{ confirm_url }}">
    {{ confirm_url }}
  </a>
</p>

In future we can host our own mail server