-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.py
29 lines (24 loc) · 933 Bytes
/
middlewares.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
from functools import wraps
from flask import request, jsonify, g
import jwt
from sqlalchemy.exc import NoResultFound
from db.models.user import User
def auth_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
token = request.headers.get("x-access-token", None)
secret = os.environ.get("SESSION_SECRET")
if token:
try:
payload = jwt.decode(token, secret, algorithms=["HS256"])
user_id = payload["id"]
if user_id:
g.user = User.query.filter(User.id == user_id).one()
return func(*args, **kwargs)
except NoResultFound:
return jsonify({"error": "No user found with provided token"}), 403
except Exception as e:
return func(*args, **kwargs)
return func(*args, **kwargs)
return wrapper