(feat): Basic user auth
This commit is contained in:
parent
e5976d3c8d
commit
de460fbcbc
6 changed files with 90 additions and 9 deletions
5
models/__init__.py
Normal file
5
models/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_bcrypt import Bcrypt
|
||||
|
||||
db = SQLAlchemy()
|
||||
bcrypt = Bcrypt()
|
||||
25
models/user.py
Normal file
25
models/user.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from models import db, bcrypt
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||
password_hash = db.Column(db.String(128), nullable=False)
|
||||
|
||||
@staticmethod
|
||||
def create_user(username, password):
|
||||
"""Create a new user with a hashed password"""
|
||||
password_hash = bcrypt.generate_password_hash(password).decode('utf-8')
|
||||
new_user = User(username=username, password_hash=password_hash)
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
return new_user
|
||||
|
||||
@staticmethod
|
||||
def verify_user(username, password):
|
||||
"""Verify user creds"""
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user and bcrypt.check_password_hash(user.password_hash, password):
|
||||
return user
|
||||
return None
|
||||
Loading…
Add table
Add a link
Reference in a new issue