From 11a3e7551f0d17ec2fac22ebe4941c4d4901a638 Mon Sep 17 00:00:00 2001 From: Stephan Porada Date: Mon, 8 Jul 2019 15:59:15 +0200 Subject: [PATCH] Add email confirmation --- app/models.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index d7d33b85..45a2a633 100644 --- a/app/models.py +++ b/app/models.py @@ -22,16 +22,31 @@ class User(UserMixin, db.Model): username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) + confirmed = db.Column(db.Boolean, default=False) def __repr__(self): return '' % self.username - password_hash = db.Column(db.String(128)) + def generate_confirmation_token(self, expiration=3600): + s = Serializer(current_app.config['SECRET_KEY'], expiration) + return s.dumps({'confirm': self.id}).decode('utf-8') def generate_reset_token(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'reset': self.id}).decode('utf-8') + def confirm(self, token): + s = Serializer(current_app.config['SECRET_KEY']) + try: + data = s.loads(token.encode('utf-8')) + except: + return False + if data.get('confirm') != self.id: + return False + self.confirmed = True + db.session.add(self) + return True + @property def password(self): raise AttributeError('password is not a readable attribute')