initial login logic
This commit is contained in:
32
models.py
Normal file
32
models.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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}>'
|
||||
Reference in New Issue
Block a user