Tharinda Ehelepola
Tharinda Ehelepola

Reputation: 59

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?

Upvotes: 2

Views: 1578

Answers (2)

rezkam
rezkam

Reputation: 1357

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.

Upvotes: 5

rubikonx9
rubikonx9

Reputation: 1413

You can use Flask-Login extension. It can handle most of typical user-management activities out-of-the-box.

Upvotes: 3

Related Questions