Stormpath 有没有简单的 'is_authenticated' 解决方案?
Is there a simple 'is_authenticated' solution in Stormpath?
我在 Stormpath 文档中找到了这个:
is_authenticated() (http://flask-stormpath.readthedocs.io/en/latest/api.html)
All users will always be authenticated, so this will always return True.
因此 is_authenticated 似乎不像在 flask-login 中那样有效。我是否必须做一个解决方法或者是否有类似的功能已经预先构建在这个 API?
---编辑---
感谢您的回答,但似乎还是不行。我想做的是:
navbar.html
<div class="navbar-right">
{% if user %}
<p class="navbar-text">Signed in as <a href="#" class="navbar-link">{{ result }}</a></p>
{% else %}
<button id="registerbtn" type="button" class="btn btn-default navbar-btn">Sign up</button>
{% endif %}
</div>
app.py
@app.route('/navbar')
def navbar():
if user:
return render_template('navbar.html', result=user.given_name)
else:
return render_template('navbar.html')
我收到此错误消息:
AttributeError: 'AnonymousUserMixin' object has no attribute 'given_name'
我是这个图书馆的作者。确实有一种方法可以检查用户是否经过身份验证——您找到的代码实际上是内部 Flask-Login API 的一部分,并不意味着 public 使用。
你要做的是:
from flask_stormpath import user
def my_view():
if user:
# there is a user who is authenticated!
else:
# nobody has authenticated :(
这会让你得到你想要的 =)
我也有这个错误。我使用了与上面提供的代码类似的代码:
if user:
return render_template('index.html', given_name=user.given_name)
return render_template('index.html')
并且会得到与 OP 中相同的错误:
AttributeError: 'AnonymousUserMixin' object has no attribute 'given_name'
我通过将代码更改为:
来修复它
if user.is_anonymous() == False:
return render_template('index.html', given_name=user.given_name)
return render_template('index.html')
似乎错误是用户对象的 if 语句总是解析为 True,因为从 flask.ext.stormpath 导入用户时用户对象以某种方式实例化。
我在 Stormpath 文档中找到了这个:
is_authenticated() (http://flask-stormpath.readthedocs.io/en/latest/api.html)
All users will always be authenticated, so this will always return True.
因此 is_authenticated 似乎不像在 flask-login 中那样有效。我是否必须做一个解决方法或者是否有类似的功能已经预先构建在这个 API?
---编辑---
感谢您的回答,但似乎还是不行。我想做的是:
navbar.html
<div class="navbar-right">
{% if user %}
<p class="navbar-text">Signed in as <a href="#" class="navbar-link">{{ result }}</a></p>
{% else %}
<button id="registerbtn" type="button" class="btn btn-default navbar-btn">Sign up</button>
{% endif %}
</div>
app.py
@app.route('/navbar')
def navbar():
if user:
return render_template('navbar.html', result=user.given_name)
else:
return render_template('navbar.html')
我收到此错误消息:
AttributeError: 'AnonymousUserMixin' object has no attribute 'given_name'
我是这个图书馆的作者。确实有一种方法可以检查用户是否经过身份验证——您找到的代码实际上是内部 Flask-Login API 的一部分,并不意味着 public 使用。
你要做的是:
from flask_stormpath import user
def my_view():
if user:
# there is a user who is authenticated!
else:
# nobody has authenticated :(
这会让你得到你想要的 =)
我也有这个错误。我使用了与上面提供的代码类似的代码:
if user:
return render_template('index.html', given_name=user.given_name)
return render_template('index.html')
并且会得到与 OP 中相同的错误:
AttributeError: 'AnonymousUserMixin' object has no attribute 'given_name'
我通过将代码更改为:
来修复它if user.is_anonymous() == False:
return render_template('index.html', given_name=user.given_name)
return render_template('index.html')
似乎错误是用户对象的 if 语句总是解析为 True,因为从 flask.ext.stormpath 导入用户时用户对象以某种方式实例化。