フラスコログインを使用していますが、この問題が発生します。
ログイン関数は次のように実行されます。
@api.route('/login', methods=['POST'])
def login():
if current_user.is_authenticated():
return jsonify(flag='success')
username = request.form.get('username')
password = request.form.get('password')
if username and password:
user, authenticated = fsUser.authenticate(username, password)
if user and authenticated:
if login_user(user, remember='y'):
print 'is authenticated: ',current_user.is_authenticated()
return jsonify(flag='success')
current_app.logger.debug('login(api) failed, username: %s.' % username)
return jsonify(flag='fail', msg='Sorry, try again.')
コードは問題なく動作しています。return flag='success' に向かっても正常に実行されます。私はそれが作成するセッションがあることを確認しました。current_user がまだ匿名であることを除いて、すべてうまく機能します。したがって、 current_user.is_authenticated() はまだ失敗を返します。
そして、どこを確認すればよいかわかりません。誰か助けてもらえますか?
PSユーザーオブジェクトは、SQLAlchemyによってSQLデータベースから取得されます。それが問題の原因である可能性がある場合は、少し変更した後も model.py を提供できます。
編集:私のユーザーコールバック定義:
@login_manager.user_loader
def load_user(id):
user = cache.get(id)
if not user:
user = User.get_by_id(id)
cache.set(id, user, 20*60)
return user
確認のために印刷しました。上記のユーザーの戻り値は正しいです。これは、current_user がまだ匿名オブジェクトであり、デフォルトのままです。
ユーザークラス:
class User(db.Model, UserMixin):
__tablename__ = 'my_users'
id = Column('user_id', db.Integer, primary_key=True)
level = Column('user_level', db.Integer, nullable=False)
name = Column('user_name', db.String(255))
email = Column('user_email', db.String(255), nullable=False, unique=True)
# ===============================================================
# Users
# ================================================================
# Password
_password = Column('user_password', db.String, nullable=False)
def _get_password(self):
return self._password
def _set_password(self, password):
self._password = generate_password_hash(password)
# Hide password encryption by exposing password field only.
password = db.synonym('_password',
descriptor=property(_get_password,
_set_password))
def check_password(self, password):
if self.password is None:
return False
return check_password_hash(self.password, password)
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return unicode(self.id)
def find_user(self):
return unicode('hahaha@gmail.com')