First login done

This commit is contained in:
victor
2024-12-06 00:27:27 +01:00
parent 28ac497c57
commit 13fe482956
4 changed files with 60 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.venvManjaro/
app/__pycache__/
app/models/__pycache__/
app/codes/__pycache__/

19
app/__init__.py Normal file
View File

@@ -0,0 +1,19 @@
from flask import Flask
from dotenv import load_dotenv
# Cargar las variables de entorno desde .env
def create_app():
app = Flask(__name__)
load_dotenv()
app.config['SECRET_KEY'] = 'mi_secreto'
# Registro de blueprints
from app.codes.user import login
app.register_blueprint(login, url_prefix='/')
return app

22
app/codes/user.py Normal file
View File

@@ -0,0 +1,22 @@
from flask import Blueprint, request, jsonify, current_app
import datetime
import jwt
login = Blueprint('login', __name__)
@login.route('/login', methods=['POST'])
def login_view():
data = request.get_json()
username = data.get('username')
password = data.get('password')
# Aquí se validan las credenciales (en este caso un ejemplo simple)
if username == 'admin' and password == 'password':
# Generar token
token = jwt.encode({
'username': username,
'exp': datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(hours=1)
}, current_app.config['SECRET_KEY'], algorithm='HS256') # Usamos current_app para acceder a la configuración
return jsonify({'token': token})
return jsonify({'message': 'Credenciales inválidas'}), 401

15
server.py Normal file
View File

@@ -0,0 +1,15 @@
from app import create_app
from flask import render_template, session, redirect
app = create_app()
@app.route('/', methods = ['GET'])
def index():
userId = session.get('userId')
if userId:
return "test"
else:
return "test"
if __name__ == "__main__":
app.run(debug=True, port="8080")