from flask import Flask, request, render_template, redirect, url_for from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user import psycopg2 app = Flask(__name__, static_folder="./resource/") app.secret_key = 'your_secret_key' login_manager = LoginManager() login_manager.init_app(app) class Cert(): def __init__(self): self.db = psycopg2.connect("host=localhost dbname=dislocker user=suti7 password=testing") def register(self, username, userid): cursor = self.db.cursor() insert_sql = "INSERT INTO user_list (discord_username, discord_userid) VALUES (%s, %s);" cursor.execute(insert_sql, (username, userid)) self.db.commit() def login(self, username, password): cursor = self.db.cursor() get_sql = "SELECT * FROM web_auth WHERE username = %s AND password = %s " cursor.execute(get_sql, (username, password)) result = cursor.fetchall() print(result) if result == []: return 1 elif result[0][1] == username and result[0][2] == password: return 0 cert = Cert() class User(UserMixin): def __init__(self, id): self.id = id def get_id(self): return self.id @login_manager.user_loader def load_user(user_id): return User(user_id) @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': user_id = request.form.get('user_id') password = request.form.get('password') # ユーザーIDとパスワードの検証を行う # ここでは省略しますが、実際にはデータベースなどからユーザー情報を取得し、パスワードを検証します if cert.login(user_id, password) == 0: user = User(user_id) login_user(user) return redirect(url_for('dacs')) else: print("WEB UI LOGIN ERROR") return redirect(url_for("damedesu")) return render_template('login.html') @app.route('/logout') @login_required def logout(): logout_user() return redirect(url_for('login')) @app.route('/dacs', methods=["GET", "POST"]) @login_required def dacs(): if request.method == "POST": discord_userid = request.form.get("discord_userid") discord_username = request.form.get("discord_username") cert.register(discord_username, discord_userid) return render_template("dacs.html") @app.route("/damedesu") def damedesu(): return render_template("damedesu.html") @app.route("/") def hello_world(): return "

Hello world!

" if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)