Skip to content

Commit

Permalink
implemented is_authenticated middleware
Browse files Browse the repository at this point in the history
Co-authored-by: Trisha Reddy <105570654+trishavreddy@users.noreply.github.com>
Co-authored-by: Chloe <138538887+ChL4859@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 16, 2023
1 parent 6a7b813 commit 2e937a7
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions server/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask import request, jsonify

import functools

from models.session import Session


def is_authenticated(f):
def not_authenticated():
res = {"status": "Not authenticated."}
return jsonify(res), 401

@functools.wraps(f)
def wrapper(*args, **kwargs):
token = request.cookies.get("session")

from app import app, db

with app.app_context():
session = db.session.query(Session).filter_by(token=token).first()

if session is None:
return not_authenticated()

return f(*args, **kwargs)

return wrapper

0 comments on commit 2e937a7

Please sign in to comment.