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

Add view-decorator login_required() to refactor 401 cases #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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