Search code examples
pythonflaskwsgi

Global variables in python flask web application


I have implemented a web server using python flask and hosted in pythonanywhere.com ... I have used global variables in my implementation for handle login sessions. for example-

TOKENS = {"OAUTH_TOKEN": ""}
if(TOKENS['OAUTH_TOKEN']) == "":
   authorized = Flase
else:
   authorized = True

But the problem is when a user logged and authorized the second user get as a authorized user. pythonanywhere used WSGI as a server for run python flask web application. How can I handle each user as separates threads?


Solution

  • You can write your code to create a login solotion but It's not realy safe.need to set the login credential in the session and load the user with each request and load it in each request and set g object.

    #login
    session['user'] = user_id
    
    
    @app.before_request
    def before_request():
        current_user = user_obj
        g.user = current_user
    

    You can use this guide for a good login app.