Add email confirmation

This commit is contained in:
Stephan Porada 2019-07-08 15:59:15 +02:00
parent ae11e04c6c
commit 11a3e7551f

View File

@ -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 '<User %r>' % 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')