Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login_callback should return and why not add a decorator to the doc #23

Open
daserzw opened this issue Oct 26, 2016 · 1 comment
Open

Comments

@daserzw
Copy link

daserzw commented Oct 26, 2016

Hi,

in the actual documentation the login_callback does not return, and that seems to cause a lot of problems. While it is ok to just render a template (login_ok) or redirect to '/', a simple decorator could be included in the doc to have a more general case, like:

@sso.login_handler
def login_callback(user_info):
    """Store login information in session."""
    session['user'] = user_info
    return redirect(request.args.get('next_url'))

def sso_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if 'user' not in session:
            return redirect(url_for('sso_login', next_url=request.url))
        return f(*args, **kwargs)
    return decorated_function

then you can decorate whatever view function that requires authentication with some added value:

  • you do not have to actually check for user auth each time
  • you can use session['user'] being certain that it will be available
  • you'll be redirected to the url you actually asked for

Example:

@app.route('/secure_location')
@sso_required
def secure_location():
    return render_template('secure_location.html', user=session['user'])
@daserzw daserzw changed the title login_callback should return and add a decorator to the doc login_callback should return and why not add a decorator to the doc Oct 26, 2016
@golharam
Copy link

This would also go along with the other Flask authentication modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants