28 lines
1.0 KiB
Python
28 lines
1.0 KiB
Python
"""Single-admin user model (Chapter 12). Not in Chapter 06's table list —
|
|
that chapter scopes log-analytics tables; auth is a separate concern this
|
|
chapter owns. Chapter 01 confirms single-admin, no self-registration.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
import bcrypt
|
|
from flask_login import UserMixin
|
|
|
|
from app.extensions import db
|
|
|
|
|
|
class User(db.Model, UserMixin):
|
|
__tablename__ = "users"
|
|
|
|
id: int = db.Column(db.Integer, primary_key=True)
|
|
email: str = db.Column(db.String(255), unique=True, nullable=False, index=True)
|
|
password_hash: str = db.Column(db.String(255), nullable=False)
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
|
|
|
def set_password(self, raw_password: str) -> None:
|
|
self.password_hash = bcrypt.hashpw(raw_password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
|
|
|
def check_password(self, raw_password: str) -> bool:
|
|
return bcrypt.checkpw(raw_password.encode("utf-8"), self.password_hash.encode("utf-8"))
|