[Flask] How to use flask auth with blueprints

linnchord linnchord at gmail.com
Wed Dec 16 00:39:29 EST 2015


I don't get it.  What do you want exactly? The @login_required is doing the
right thing in your description.

If you don't need this, you can remove it.

If you need auth in you view handler only in some positions, you can do

if not current_user.is_authenticated:
    return current_app.login_manager.unauthorized()

Or, you can write a decorator you need, maybe @login_check.

David Montgomery <davidmontgomery at gmail.com>于2015年12月16日周三 上午6:57写道:

> Hi,
>
> New to flask....and having issues with scaling the app.
>
> So I created a bluiprint and decorated with @login_required. Auth, Users
> etc are on my main views.py. If I remove @login_required my code works. But
> if I add I am redirected to login page. Auth does work prior to moving the
> code to a blueprint. So how do I import into my blueprint the objects
> needed for auth?
>
> Here is my blueprint
>
> from flask import Flask, render_template,request,jsonify,Responsefrom flask import make_response,session,redirect,url_for,gfrom flask.ext.security import Securityfrom flask.ext.login import login_user, logout_user, current_user, login_required, UserMixin,confirm_login,fresh_login_requiredfrom flask_security import auth_token_required, http_auth_requiredfrom passlib.apps import custom_app_context as pwd_contextfrom flask.ext.httpauth import HTTPBasicAuth # sudo pip install flask-httpauthfrom flask import Blueprint
> auth = HTTPBasicAuth()
>
> delstack = Blueprint('/delstack/<stack_id>', __name__)@delstack.route('/delstack/<stack_id>', methods=['DELETE'])@login_requireddef delstack_route(stack_id):
>
>     jd = {'status':'ok'}
>     data = json.dumps(jd)
>     resp = Response(data, status=200, mimetype='application/json')
>     return resp
>
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
-- 
>> linnchord at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20151216/8dec16a6/attachment.html>


More information about the Flask mailing list