Bläddra i källkod

adding basic login feature

main
Simon Bowie 1 år sedan
förälder
incheckning
27433de453
4 ändrade filer med 61 tillägg och 2 borttagningar
  1. +2
    -0
      config.env.template
  2. +5
    -1
      web/app/__init__.py
  3. +34
    -1
      web/app/main.py
  4. +20
    -0
      web/app/templates/login.html

+ 2
- 0
config.env.template Visa fil

@@ -4,6 +4,8 @@
FLASK_APP=app/__init__.py
FLASK_RUN_HOST=0.0.0.0
FLASK_DEBUG=1
SITE_PASSWORD=
SECRET_KEY=

# Solr variables
# Hostname for Solr

+ 5
- 1
web/app/__init__.py Visa fil

@@ -7,7 +7,7 @@
# https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
# Config stuff adapted from https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms

from flask import Flask
from flask import Flask, session
from flask_moment import Moment
import os

@@ -17,6 +17,10 @@ moment = Moment()
def create_app():
app = Flask(__name__)

# get the secret key so sessions work
secret = os.getenv('SECRET_KEY')
app.secret_key = secret

moment.init_app(app)

# blueprint for main parts of app (contents and frontmatter)

+ 34
- 1
web/app/main.py Visa fil

@@ -5,17 +5,50 @@
# @purpose: Main route for index page, contents, frontmatter, and other miscellaneous pages
# @acknowledgements:
# https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
# https://www.reddit.com/r/flask/comments/k1bvw4/password_protect_pages/

from flask import Blueprint, render_template
from flask import Blueprint, render_template, session, request, redirect, url_for, flash
import markdown
import os
from functools import wraps

main = Blueprint('main', __name__)

site_password = os.getenv('SITE_PASSWORD')

# custom decorator to check password
def check_pw(func):
@wraps(func)
def decorated_function(*args, **kwargs):
status = session.get('status')
if status != "good":
return redirect(url_for('main.login'))
return func(*args, **kwargs)

return decorated_function

# route for index page
@main.route('/')
@check_pw
def index():
return render_template('index.html')

# route for site login page
@main.route('/login', methods=['GET','POST'])
def login():
if request.method == "POST":
req = request.form
password = req.get("password")
if password != site_password:
flash('wrong password! try again...')
return redirect(request.url)
session["status"] = 'good'
return redirect(url_for("main.index"))

return render_template('login.html')

# route for table of contents page
@main.route('/contents/')
def contents():

+ 20
- 0
web/app/templates/login.html Visa fil

@@ -0,0 +1,20 @@
{% extends "base.html" %}

{% block content %}
<form action="/login" method="post">
<strong>Password:</strong><br>
<input name='password' ><br><br>
<input type="submit" value="Login">
</form>

{% endblock %}

{% block footer %}
<footer class="footer p-3">
<div class="container info" style="display: none" >
<span class="text-muted">© 2023 Julien McHardy & Kat Jungnickel, chapters by respective authors. Licensed under a <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a> license.</span>
<span class="text-muted">Data from the <a href="https://www.politicsofpatents.org/">Politics of Patents</a> research project hosted at Goldsmiths, University of London, and funded by the European Research Council under the European Union’s Horizon 2020 research and innovation programme (#819458).</span>
</div>
</footer>
{% endblock%}

Laddar…
Avbryt
Spara