Авторизация

+ интеграция с другими запросами
This commit is contained in:
Anton Kamalov
2024-10-22 01:29:32 +03:00
parent 339a55f8da
commit d1f513e106
7 changed files with 75 additions and 7 deletions

28
App/Auth/auth_route.py Normal file
View File

@@ -0,0 +1,28 @@
from flask import request, Blueprint, render_template, session, redirect, url_for
from os import path
from db.sql_provider import SQLProvider
from db.select import select_list
import json
sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql'))
auth_bp = Blueprint('auth_bp', __name__, template_folder='templates')
with open(path.join(path.dirname(__file__), 'config.json')) as f:
config = json.load(f)
@auth_bp.route('/', methods=['GET', 'POST'])
def auth():
if request.method == 'GET':
return render_template('auth.html')
else:
data = dict(request.form)
print(data)
result = select_list(config, sql_provider.get('auth.sql', data))
if result[1]:
session['user_id'] = result[1][0]['user_ID']
session['role'] = result[1][0]['role']
session['db_config'] = config
session.permanent = True
return redirect(url_for('index'))
else:
return 'Неправильный логин или пароль'

7
App/Auth/config.json Normal file
View File

@@ -0,0 +1,7 @@
{
"host": "localhost",
"port": 3306,
"user": "manager",
"password": "ilikepizza",
"db": "sklad"
}

4
App/Auth/sql/auth.sql Normal file
View File

@@ -0,0 +1,4 @@
SELECT user_ID, role FROM user_table
WHERE 1=1
AND login = '$login'
AND password = '$password';

View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Authorization</title>
</head>
<body>
<form action="" method="post">
<label for="login">Login: </label>
<input type="text" name="login"><br>
<label for="password">Password: </label>
<input type="password" name="password"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>