我在我的项目中使用了flask-jwt,我已经声明controller.py
:
def _validate_user(email, password):
"""
validates user from db
"""
user = Users.get_user(email)
if check_password_hash(user.password_hash, password):
return user
else:
return None
def verify(email, password):
if not (email and password):
return False
valid_user = _validate_user(email, password)
if valid_user:
return valid_user
def identity(payload):
user_id = payload["identity"]
return {"userid", user_id}
然后在api的view.py
模块上我有这个
from flask_jwt import JWT, jwt_required
from ..controller import (
verify,
identity
)
jwt = JWT(app, verify, identity)
我使用电子邮件作为用户名概念,我没有向用户询问他的用户名,而是使用用户的电子邮件地址作为用户名,并且我已经models.py
像这样定义了表格:
class Users(UserMixin, db.Model):
""" Stores the user information
"""
__tablename__ = "users"
userid = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(60), index=True, unique=True, nullable=False)
password_hash = db.Column(db.String(255), nullable=False)
registered_on = db.Column(db.DateTime, default=datetime.utcnow())
authenticated = db.Column(db.Boolean, default=False)
admin = db.Column(db.Boolean, nullable=False, default=False)
curl -H "Content-Type: application/json" -u san@test.com:test -i -X GET http://127.0.0.1:5000/auth
但是使用上面的方法我不断收到错误方法不允许,如果我尝试使用 POST 我得到错误提示 failed to decode <p>Failed to decode JSON object: No JSON object could be decoded</p>
。
我很困惑发生了什么事,办公室的类似设置正在工作,但我从我能记得的头顶上实现了..似乎我错过了一点。