Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middleware in flask

I just stated using Flask and was trying to implement a small feature in my project. The objective is to set a cookie only if the request comes from a authenticated user.

I found two ways of doing this.

First method

@app.before_request
def before_request():
   # set cookie if user is logged in

Second method, by implementing something like this adding-a-simple-middleware-to-your-flask-application

Can someone explain to me what are the main differences between the two methods and when and where which method should be used.

Also, I am currently using "flask-login" to keep track of the logged in user. If I use the first method, I can easily verify if someone is logged in by importing the current_user

from flask.ext.login import current_user

but if I try to do the same while using the second method, the current_user is always "None" as the application context is incorrect.

So, I wanted to know if I decided to go ahead with the second implementation, how do I check if the user is logged in or not.

like image 346
sauvik Avatar asked Jul 30 '16 21:07

sauvik


People also ask

What is deployment in Flask?

Deployment is very simple you simply set up your python environment and run the app.py application, since I already have Anaconda installed in my computer I will run the app.py file from there, otherwise you can create your separate virtual environment install all the dependencies their and run it from there, you don't ...

Is Flask a frontend or backend?

Thanks to Flask, a backend this compact and controlled is capable of handling all the data processing required to support a full-featured frontend finance tracking app for fiscal fanatics, like me! I hope you've enjoyed my article on Flask as a compact backend development tool for Python.

What are Flask extensions?

A Flask extension is a Python module, which adds specific type of support to the Flask application. Flask Extension Registry is a directory of extensions available. The required extension can be downloaded by pip utility.

What is a Flask decorator?

Understanding Flask decorators A decorator is a function that takes in another function as a parameter and then returns a function. This is possible because Python gives functions special status. A function can be used as a parameter and a return value, while also being assigned to a variable.


1 Answers

I've never used the second method you've mentioned. I'm sure that it can be done with it, but it's very uncommon. I would suggest to use more common features of flask. For sake of maintainers of your code :) So the first method you've mentioned is fine.

Or you can use decorators for more granular access restrictions. Keep in mind that setting cookies in flask can be done when making actual response object. That means you should use Deferred Request Callbacks for setting cookies in decorated function.

like image 131
sjudǝʊ Avatar answered Nov 15 '22 09:11

sjudǝʊ