Added SQL, dao and app - not functional

This commit is contained in:
Tesouraria CRSP
2024-11-26 10:57:25 -03:00
parent 158ec60b58
commit d6357803b8
25 changed files with 1580 additions and 0 deletions

27
functions/auth.py Normal file
View File

@@ -0,0 +1,27 @@
from flask import Flask, request, jsonify
import jwt
from datetime import datetime, timedelta
from werkzeug.security import generate_password_hash, check_password_hash
from dao import get_user_by_email
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
def create_token(user_id):
payload = {
'user_id': user_id,
'exp': datetime.utcnow() + timedelta(hours=1)
}
return jwt.encode(payload, app.config['SECRET_KEY'], algorithm='HS256')
@app.route('/login', methods=['POST'])
def login():
data = request.json
if not data or 'email' not in data or 'senha' not in data:
return jsonify({'message': 'Missing email or password'}), 400
user = get_user_by_email(data['email'])
if user and check_password_hash(user['senha_hash'], data['senha']):
token = create_token(user['id'])
return jsonify({'token': token})
return jsonify({'message': 'Invalid credentials'}), 401