Spaces:
Runtime error
Runtime error
from flask_login import LoginManager, UserMixin | |
from werkzeug.security import check_password_hash, generate_password_hash | |
from portalrhjobs.ext.database import db | |
from PortalRH.portalrhjobs.ext.models import User | |
login_manager = LoginManager() | |
def init_app(app): | |
def verify_login(user): | |
"""Valida o usuario e senha para efetuar o login""" | |
username = user.get('username') | |
password = user.get('password') | |
if not username or not password: | |
return False | |
existing_user = User.query.filter_by(username=username).first() | |
if not existing_user: | |
return False | |
if check_password_hash(existing_user.password, password): | |
return True | |
return False | |
def create_user(username, password): | |
"""Registra um novo usuario caso nao esteja cadastrado""" | |
if User.query.filter_by(username=username).first(): | |
raise RuntimeError(f'{username} ja esta cadastrado') | |
user = User(username=username, password=generate_password_hash(password)) | |
db.session.add(user) | |
db.session.commit() | |
return user | |