justinarat / Teamify

0 stars 0 forks source link

Identifying users for page rendering #40

Closed justinarat closed 4 months ago

justinarat commented 5 months ago

Need to figure out how to identify users so pages can be set up depending on the user.

Might be good to check out flask request, response and session objects for this.

justinarat commented 5 months ago

I think we can use the session object for identifying the user.

It holds key-value pairs which is stored as a cookie in the client browser and Flask automatically handles mapping session data to the clients. Found another shorter example of flask sessions here.

When a user logs in, we can store their username or user id in the session object and use that to identify them. Example:


from app import app
from flask import session, redirect, render_template
from flask import request # request lets us view the contents of the HTTP request and get the user id

@app.route("/login", method=["POST"])
def login():
  user_id = request.form["user_id"]     # Get the user_id from the POST body
  session["user_id"] = user_id          # Store user_id into session object
  return redirect(url_for("games"))

@app.route("/games")
def games():
  user_id = session["user_id"]          # Get user_id from session object

  # Now can get user_data from the database and in Jinja you
  # can change the way pages are rendered depending on the user.
  # For example if they're an admin, the admin link is rendered.

  return render_template("games.html", template_folder("templates"), user_data) # user_data can be split up
justinarat commented 5 months ago

I think we should just use flask login for this instead of storing the user_id in the session object.

dominictdavies commented 4 months ago

This was done in issue #46 already.