OhmSpectator / days-at-home-counter

0 stars 0 forks source link

Make the service available for several users #7

Closed OhmSpectator closed 1 year ago

OhmSpectator commented 2 years ago

Make the script usable not only by 1 person. Hence, it needs to support sessions with cookies.

OhmSpectator commented 1 year ago

https://overiq.com/flask-101/sessions-in-flask/

OhmSpectator commented 1 year ago

https://flask.palletsprojects.com/en/2.2.x/quickstart/#sessions

OhmSpectator commented 1 year ago

To distinguish the users of your Flask app, you could have each user log in with a unique username and password. When a user logs in, you could have the Flask app check the database to verify their username and password, and then retrieve their date intervals and calculation results from the database. Alternatively, you could use a session-based authentication system, where each user is assigned a unique session token when they log in. The session token would be stored in a cookie on the user's computer, and the Flask app would use the token to identify the user and retrieve their date intervals and calculation results from the database.

OhmSpectator commented 1 year ago

To implement a session-based authentication system in your Flask app, you will need to do the following:

When a user logs in, generate a unique session token for them and store it in a database along with their username and other information.

Set a cookie on the user's computer containing the session token.

When the user makes a request to the Flask app, check the request headers to see if there is a cookie containing a session token.

If there is a session token in the cookie, use it to look up the user's information in the database and retrieve their date intervals and calculation results.

If there is no session token in the cookie, or the session token is invalid, redirect the user to the login page.

To generate the session token, you could use a random string generator to create a long, random string that will be used as the session token. To set the cookie, you can use the set_cookie() method of the Flask Response object, which will set a cookie on the user's computer containing the session token. To check the request headers for the cookie, you can use the request.cookies dictionary, which will contain the cookies that were sent with the request.

Here is an example of how this might look in your Flask app:

from flask import Flask, request, Response
from uuid import uuid4

app = Flask(__name__)

# Generate a random string to use as a session token
def generate_session_token():
    return str(uuid4())

# Set a cookie containing the session token
def set_session_token(response, session_token):
    response.set_cookie('session_token', session_token)

# Check the request headers for a cookie containing a session token
def get_session_token(request):
    return request.cookies.get('session_token')

# Handle user login
@app.route('/login', methods=['POST'])
def login():
    # Retrieve the user's login credentials from the request
    username = request.form['username']
    password = request.form['password']

    # Check the username and password against the database to verify the user's identity

    # If the user is valid, generate a session token and set it as a cookie
    session_token = generate_session_token()
    response = Response('Successful login')
    set_session_token(response, session_token)

    # Save the session token to the database along with the user's information

    return response

# Handle requests to the Flask app
@app.route('/', methods=['GET', 'POST'])
def index():
    # Check for a cookie containing a session token
    session_token = get_session_token(request)

    # If there is no session token, redirect the user to the login page
    if not session_token:
        return redirect('/login')

    # If there is a session token, use it to look up the user's information in the database
    # and retrieve their date intervals and calculation results

    # Render the app's main page, passing in the user's date intervals and calculation results as needed
OhmSpectator commented 1 year ago

The Flask session mechanism allows you to store user-specific information in a session object, which is a dictionary-like object that is stored on the server and associated with a unique session identifier.

To use the Flask session mechanism, you will need to do the following:

Enable the session extension in your Flask app by calling the Flask.secret_key method and passing in a secret key that will be used to encrypt and decrypt the session data.

app = Flask(__name__)
app.secret_key = 'my_secret_key'

When a user logs in, generate a unique session identifier and store it in the user's session object.

@app.route('/login', methods=['POST'])
def login():
    # Retrieve the user's login credentials from the request
    username = request.form['username']
    password = request.form['password']

    # Check the username and password against the database to verify the user's identity

    # If the user is valid, generate a session identifier and store it in the session object
    session_id = generate_session_id()
    session['session_id'] = session_id

    # Save the session identifier to the database along with the user's information

    return 'Successful login'

When the user makes a request to the Flask app, check the session object for a session identifier.

@app.route('/', methods=['GET', 'POST'])
def index():
    # Check the session object for a session identifier
    session_id = session.get('session_id')

    # If there is no session identifier, redirect the user to the login page
    if not session_id:
        return redirect('/login')

    # If there is a session identifier, use it to look up the user's information in the database
    # and retrieve their date intervals and calculation results

    # Render the app's main page, passing in the user's date intervals and calculation results as needed
OhmSpectator commented 1 year ago

In Flask, a session is a way to store information that can be accessed across multiple requests. This is useful when you want to maintain some state or data across multiple requests made by a user. For example, if a user logs in to your application, you can use the session to store their login information so that it can be accessed in other requests made by the user.

In Flask, sessions are implemented using cookies, so you can think of a session as a special kind of cookie that is stored on the server instead of the client's browser. When a user makes a request, the session cookie is sent along with the request, and Flask uses the data stored in the cookie to retrieve the corresponding session information from the server. This allows Flask to maintain the state of the session across multiple requests made by the user.

OhmSpectator commented 1 year ago

If you want to use server-side sessions in Flask, you can either write your own session interface or use extensions like Flask-Session