Flask-Security登录无效密码问题

时间:2018-03-14 16:51:44

标签: python flask sqlite flask-security flask-bootstrap

我正在为简单的注册和登录实施flask-security,但由于某种原因,我无法理解或找到,为什么在成功注册并成功登录后我再次尝试登录时我得到了无效密码'

我正在使用flask-bootstrap,sqlite3,python 3.6.4,pyenv + virtualenv,

以下是该应用程序的结构:

body-mass-index/
    run.py
    application/
        __init__.py
        templatess/
            index.html
            home.html
            security/
                register_user.html
                login_user.html

这是init文件:

def create_app():
    app = Flask(__name__)

    # Configuration
    app.config['DEBUG'] = True
    app.config['SECRET_KEY'] = \
        '*id4)fbbiyd*57oa18da3^t0$)m%ti#9ua+t^ihd4z9t*x%&sx'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/bmi.db'
    app.config['SECURITY_PASSWORD_SALT'] = os.urandom(32)
    app.config['SECURITY_REGISTERABLE'] = True
    app.config['SECURITY_SEND_REGISTER_EMAIL'] = False
    app.config['SECURITY_POST_LOGIN_VIEW'] = '/bmi'
    app.config['SECURITY_POST_LOGOUT_VIEW'] = '/'
    app.config['SECURITY_POST_REGISTER_VIEW'] = '/bmi'
    app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False

    # Create database connection object
    db = SQLAlchemy(app)

    # Define models

    roles_users = db.Table(
        'roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

    class Role(db.Model, RoleMixin):
        __tablename__ = 'role'

        id = db.Column(db.Integer(), primary_key=True)
        name = db.Column(db.String(80), unique=True)
        description = db.Column(db.String(255))

        def __repr__(self):
            return '<Role> {}'.format(self.name)

    class User(db.Model, UserMixin):
        __tablename__ = 'user'

        id = db.Column(db.Integer, primary_key=True)
        email = db.Column(db.String(255), unique=True)
        password = db.Column(db.String(255))
        active = db.Column(db.Boolean())
        confirmed_at = db.Column(db.DateTime())
        roles = db.relationship(
            'Role',
            secondary=roles_users,
            backref=db.backref('users', lazy='dynamic'))

        def __repr__(self):
            return '<User> {} {}'.format(self.mail, self.email)

    # Setup Flask-Security
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security = Security(app, user_datastore)

    @app.before_first_request
    def before_first_request():
        # Create any database tables that don't exist yet.
        db.create_all()

    # Initialize bootstrap
    Bootstrap(app)

    # Debug = True to enable the toolbar
    toolbar = DebugToolbarExtension(app)

    # Views

    @app.route('/', methods=['GET'])
    def index():
        return render_template('index.html')

    @app.route('/bmi', methods=['GET', 'POST'])
    @login_required
    def bmi():
        user = User.query.filter_by(id=session['user_id']).first()
        return render_template('bmi.html', user=user.email)

    return app

这里是登录文件:

{% extends "layout.html" %}
{% from "security/_macros.html" import render_field_with_errors, render_field %}

{% block title %} {{ _('Login') }} {% endblock %}

{% block mynav %}

    <li><a href="{{ url_for_security('register') }}">Register</a></li>
    <li class="active"><a href="{{ url_for_security('login') }}">Login</a></li>

{% endblock mynav %}

{% block mycontent %}

    <h1>{{ _('Login') }}</h1>
    <form action="{{ url_for_security('login') }}" method="POST" name="login_user_form">
        {{ login_user_form.hidden_tag() }}
        {{ render_field_with_errors(login_user_form.email, class="form-control", placeholder="example@email.com") }}
        {{ render_field_with_errors(login_user_form.password, class="form-control", placeholder="Password") }}
        {{ render_field_with_errors(login_user_form.remember) }}
        {{ render_field(login_user_form.next, class="btn btn-primary") }}
        {{ render_field(login_user_form.submit, class="btn btn-primary", value="Login") }}
    </form>

{% endblock mycontent %}

我感谢您的时间和精力,谢谢。

1 个答案:

答案 0 :(得分:0)

原来,辅助的User类定义严重不足,以及 roles_users 表的实现

此外,第一次请求的顺序不正确,现在已经修复了!

现在一切正常,这个示例应用程序将Flask与React集成,享受! github repo for bmi challange