Skip to content

Conversation

@lefnire
Copy link

@lefnire lefnire commented Aug 3, 2017

This creates a login_required() decorator which adds g.user and g.auth_token to flask's request globals. For your project, it simply refactors two functions' shared code into one; no biggie. But for projects which use this code, it allows them to use the same logic for all their Flask routes requiring auth. Eg:

# new file - views.py
from project.server.auth.views import login_required
from project.server import app

@app.route('/something')
@login_required
def something(): pass

def decorated_function(*args, **kwargs):
# get the auth token
auth_header = request.headers.get('Authorization')
auth_token = auth_token = auth_header.split(" ")[1] if auth_header else ''
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplication :)
auth_token = auth_token ...

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

Successfully merging this pull request may close these issues.

2 participants