Major update 2 / login
This commit is contained in:
48
app.py
48
app.py
@@ -1,33 +1,67 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from flask import Flask, request, jsonify, redirect, url_for
|
||||
from flask_cors import CORS
|
||||
from db import init_db
|
||||
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
||||
from db import init_db, get_db
|
||||
from routes.api import api
|
||||
from routes.dashboard import dashboard
|
||||
from routes.auth import auth
|
||||
|
||||
# Initialise DB schema (e.g. creating newly added tables) when the app starts
|
||||
# Initialise DB schema when the app starts
|
||||
init_db()
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = 'grc-super-secret-key-change-in-production'
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Flask-Login setup
|
||||
# ----------------------------------------------------------------
|
||||
login_manager = LoginManager()
|
||||
login_manager.init_app(app)
|
||||
login_manager.login_view = 'auth.login'
|
||||
login_manager.login_message = 'Παρακαλώ συνδεθείτε για να συνεχίσετε.'
|
||||
|
||||
class User(UserMixin):
|
||||
def __init__(self, id, username):
|
||||
self.id = id
|
||||
self.username = username
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
conn = get_db()
|
||||
row = conn.execute('SELECT id, username FROM users WHERE id = ?', (user_id,)).fetchone()
|
||||
conn.close()
|
||||
if row:
|
||||
return User(row['id'], row['username'])
|
||||
return None
|
||||
|
||||
# Make current_user available in all templates
|
||||
@app.context_processor
|
||||
def inject_user():
|
||||
return dict(current_user=current_user)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# CORS
|
||||
# ----------------------------------------------------------------
|
||||
CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=False)
|
||||
|
||||
# Belt-and-suspenders: inject CORS headers on every response,
|
||||
# including error responses that flask-cors might miss.
|
||||
@app.after_request
|
||||
def add_cors_headers(response):
|
||||
response.headers['Access-Control-Allow-Origin'] = '*'
|
||||
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, DELETE, OPTIONS'
|
||||
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
|
||||
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization, X-Clan-Key'
|
||||
return response
|
||||
|
||||
# Explicitly handle OPTIONS preflight for all routes
|
||||
@app.route('/', defaults={'path': ''}, methods=['OPTIONS'])
|
||||
@app.route('/<path:path>', methods=['OPTIONS'])
|
||||
def handle_options(path):
|
||||
return jsonify({}), 200
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Blueprints
|
||||
# ----------------------------------------------------------------
|
||||
app.register_blueprint(api)
|
||||
app.register_blueprint(dashboard)
|
||||
app.register_blueprint(auth)
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("✅ Grepolis Remote — DB initialised")
|
||||
|
||||
Reference in New Issue
Block a user