33 lines
977 B
Python
33 lines
977 B
Python
import os
|
|
import bcrypt
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_login import UserMixin
|
|
|
|
db = SQLAlchemy()
|
|
|
|
|
|
class User(UserMixin, db.Model):
|
|
__tablename__ = 'users'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
email = db.Column(db.String(255), unique=True, nullable=False, index=True)
|
|
password_hash = db.Column(db.String(255), nullable=False)
|
|
created_at = db.Column(db.DateTime, server_default=db.func.now())
|
|
|
|
def set_password(self, password):
|
|
"""Hash password with bcrypt (includes salt automatically)."""
|
|
salt = bcrypt.gensalt()
|
|
self.password_hash = bcrypt.hashpw(
|
|
password.encode('utf-8'), salt
|
|
).decode('utf-8')
|
|
|
|
def check_password(self, password):
|
|
"""Verify password against stored hash."""
|
|
return bcrypt.checkpw(
|
|
password.encode('utf-8'),
|
|
self.password_hash.encode('utf-8')
|
|
)
|
|
|
|
def __repr__(self):
|
|
return f'<User {self.email}>'
|