-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.py
30 lines (24 loc) · 1019 Bytes
/
token.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
30
from functools import wraps
from flask import request, jsonify
import jwt
from config.config import Config
from app import create_app
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
# Verificar se o token foi enviado no cabeçalho da requisição
if 'Authorization' in request.headers:
token = request.headers['Authorization'].split(" ")[1] # "Bearer <token>"
if not token:
return jsonify({'message': 'Token é necessário!'}), 401
try:
# Decodificar o token
data = jwt.decode(token, Config.SECRET_KEY, algorithms=["HS256"])
current_user_id = data['user_id'] # Pegar o ID do usuário do token
except jwt.ExpiredSignatureError:
return jsonify({'message': 'Token expirou!'}), 401
except jwt.InvalidTokenError:
return jsonify({'message': 'Token inválido!'}), 401
return f(current_user_id, *args, **kwargs)
return decorated