mirror of
https://github.com/Fluffy-Bean/GameExpo23.git
synced 2025-05-14 14:22:16 +00:00
Adding login system
This commit is contained in:
parent
36be8993df
commit
27bf6f64ef
6 changed files with 51 additions and 11 deletions
|
@ -1,8 +1,18 @@
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template, request, redirect, flash
|
||||||
|
from flask_login import login_required, login_user, logout_user, current_user
|
||||||
from flask_assets import Bundle
|
from flask_assets import Bundle
|
||||||
from website.extensions import db, migrate, assets
|
from flask_wtf import FlaskForm
|
||||||
|
from wtforms import StringField
|
||||||
|
from wtforms.validators import DataRequired
|
||||||
|
from website.models import Users
|
||||||
|
from website.extensions import db, migrate, login_manager, assets
|
||||||
from website.config import INSTANCE_DIR, MIGRATION_DIR
|
from website.config import INSTANCE_DIR, MIGRATION_DIR
|
||||||
|
|
||||||
|
|
||||||
|
class LoginForm(FlaskForm):
|
||||||
|
uuid = StringField('uuid', validators=[DataRequired()], render_kw={"placeholder": "12345678-ABCD-ABCD-ABCD-123456789EFG"})
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__, instance_path=INSTANCE_DIR)
|
app = Flask(__name__, instance_path=INSTANCE_DIR)
|
||||||
app.config.from_pyfile('config.py')
|
app.config.from_pyfile('config.py')
|
||||||
|
|
||||||
|
@ -18,10 +28,25 @@ scripts = Bundle('js/*.js', filters='jsmin', output='gen/packed.js')
|
||||||
assets.register('scripts', scripts)
|
assets.register('scripts', scripts)
|
||||||
|
|
||||||
|
|
||||||
|
@login_manager.user_loader
|
||||||
|
def load_user(user_id):
|
||||||
|
return Users.get(user_id)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
return render_template('index.html')
|
return render_template('index.html')
|
||||||
|
|
||||||
@app.route('/login')
|
|
||||||
|
@app.route('/login', methods=["GET", "POST"])
|
||||||
def login():
|
def login():
|
||||||
return render_template('login.html')
|
form = LoginForm()
|
||||||
|
|
||||||
|
if form.validate_on_submit():
|
||||||
|
if user := Users.query.filter_by(uuid=str(form.uuid)).first():
|
||||||
|
login_user(user, remember=True)
|
||||||
|
return redirect(index)
|
||||||
|
else:
|
||||||
|
flash("Inncorrect login")
|
||||||
|
|
||||||
|
return render_template('login.html', form=form)
|
||||||
|
|
|
@ -1,3 +1,12 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Purely to make the code a bit more readable
|
||||||
|
def env(key):
|
||||||
|
return os.getenv(key)
|
||||||
|
|
||||||
|
|
||||||
|
SECRET_KEY = env("FLASK_KEY")
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db'
|
SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db'
|
||||||
|
|
||||||
MIGRATION_DIR = "/data/storage/migrations"
|
MIGRATION_DIR = "/data/storage/migrations"
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
|
from flask_login import LoginManager
|
||||||
from flask_caching import Cache
|
from flask_caching import Cache
|
||||||
from flask_assets import Environment
|
from flask_assets import Environment
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
migrate = Migrate()
|
migrate = Migrate()
|
||||||
cache = Cache()
|
login_manager = LoginManager()
|
||||||
|
cache = Cache(config={"CACHE_TYPE": "simple"})
|
||||||
assets = Environment()
|
assets = Environment()
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
Database models for the server
|
Database models for the server
|
||||||
"""
|
"""
|
||||||
from website.extensions import db
|
from website.extensions import db
|
||||||
|
from flask_login import UserMixin
|
||||||
|
|
||||||
|
|
||||||
class Games(db.Model):
|
class Games(db.Model):
|
||||||
|
@ -34,6 +35,6 @@ class Authors(db.Model):
|
||||||
game_id = db.Column(db.Integer, db.ForeignKey('games.id'))
|
game_id = db.Column(db.Integer, db.ForeignKey('games.id'))
|
||||||
|
|
||||||
|
|
||||||
class Users(db.Model):
|
class Users(db.Model, UserMixin):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
uuid = db.Column(db.String, nullable=False)
|
uuid = db.Column(db.String, nullable=False)
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<section class="center">
|
<section class="center">
|
||||||
<div class="login">
|
<div class="login">
|
||||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
{% with messages = get_flashed_messages() %}
|
||||||
{% if messages %}
|
{% if messages %}
|
||||||
{% for message in messages %}
|
{% for message in messages %}
|
||||||
<p>{{ message }}</p>
|
<p>{{ message }}</p>
|
||||||
|
@ -12,8 +12,9 @@
|
||||||
<p>Do not share your UUID</p>
|
<p>Do not share your UUID</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
<form action="{{ url_for('login') }}" method="post">
|
<form action="{{ url_for('login') }}" method="POST">
|
||||||
<input type="text" name="uuid" placeholder="AAAAA-BBBBB-CCCC-12345" required>
|
{{ form.csrf_token }}
|
||||||
|
{{ form.uuid(size=36) }}
|
||||||
<button type="submit">Login</button>
|
<button type="submit">Login</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -5,8 +5,8 @@ services:
|
||||||
image: caddy:alpine
|
image: caddy:alpine
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- 80:80
|
# - "80:80"
|
||||||
- 443:443
|
- "443:443"
|
||||||
volumes:
|
volumes:
|
||||||
- ./Caddy/Caddyfile:/etc/caddy/Caddyfile
|
- ./Caddy/Caddyfile:/etc/caddy/Caddyfile
|
||||||
- ./Caddy/data:/data
|
- ./Caddy/data:/data
|
||||||
|
@ -21,6 +21,8 @@ services:
|
||||||
db:
|
db:
|
||||||
image: postgres:alpine
|
image: postgres:alpine
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
# ports:
|
||||||
|
# - "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- ./Postgres/data:/var/lib/postgresql/data
|
- ./Postgres/data:/var/lib/postgresql/data
|
||||||
environment:
|
environment:
|
||||||
|
|
Loading…
Add table
Reference in a new issue