Skip to content

Instantly share code, notes, and snippets.

@jt-wang
Last active February 18, 2016 13:41
Show Gist options
  • Save jt-wang/ce2265ae6bef4e6691a2 to your computer and use it in GitHub Desktop.
Save jt-wang/ce2265ae6bef4e6691a2 to your computer and use it in GitHub Desktop.

写在内部没有出错,登录之后再 去 /login 页面 会返回一个 json。

    @app.route('/login', methods=['GET', 'POST'])
    def login():
        if request.method == 'GET':
            if flask_login.current_user is not None and flask_login.current_user.is_authenticated:
                return jsonify({'status': False, 'cause': 'already logged in'})

如果用装饰器,则flask_login失效:登录请求发送后,不返回login()里写的返回值,却会立刻返回装饰器里的json,当然,此时 flask_login.current_user.is_authenticated == True ,但是奇怪的是并没有登录成功。再进入/login 也不会被 before_login_only 拦截下来。

    def before_login_only(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            if flask_login.current_user is not None and flask_login.current_user.is_authenticated:
                return jsonify({'status': False, 'cause': 'already logged in'})
            else:
                return func(*args, **kwargs)
                
        return wrapper


    @app.route('/login', methods=['GET', 'POST'])
    @before_login_only
    def login():
        if request.method == 'GET':
        """
        codes
        """

即使不用装饰器,直接写在函数内,如果在判断request.method之前,则还是会出同样错误:

    @app.route('/login', methods=['GET', 'POST'])
    @before_login_only
    def login():
        if flask_login.current_user is not None and flask_login.current_user.is_authenticated:
            return jsonify({'status': False, 'cause': 'already logged in'})    
        if request.method == 'GET':
        """
        codes
        """
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment