feat: login support

This commit is contained in:
Mylloon 2023-12-21 22:46:18 +01:00
parent c54591149c
commit 80fdbd2c08
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
7 changed files with 106 additions and 28 deletions

View file

@ -1,11 +1,14 @@
from glob import glob from glob import glob
from os import urandom
from flask import Flask from flask import Flask
app = Flask(__name__, static_url_path="/") app = Flask(__name__, static_url_path="/")
# Import all routes # import all routes
for file in glob("src/routes/*.py"): for file in glob("src/routes/*.py"):
module = file.replace("/", ".").split(".")[-2] module = file.replace("/", ".").split(".")[-2]
exec(f"from routes.{module} import router as {module}") exec(f"from routes.{module} import router as {module}")
exec(f"app.register_blueprint({module})") exec(f"app.register_blueprint({module})")
app.secret_key = urandom(12)

View file

@ -1,5 +1,7 @@
from os import environ as envar from os import environ as envar
from flask import session
VAR_USERNAME = "TD_USERNAME" VAR_USERNAME = "TD_USERNAME"
VAR_USERPASS = "TD_USERPASS" VAR_USERPASS = "TD_USERPASS"
@ -25,3 +27,20 @@ class Config:
# user # user
user = User user = User
# where is stored the password info
_session_login = "logged_in"
@staticmethod
def is_logged() -> bool:
"""where the info about connection is stored"""
return (
session[Config._session_login]
if Config._session_login in session
else False
)
@staticmethod
def loggin_in() -> None:
"""where the info about connection is stored"""
session[Config._session_login] = True

View file

@ -11,14 +11,27 @@ router = Blueprint(name, __name__)
@router.route(f"/{name}") @router.route(f"/{name}")
def login() -> str: def login() -> str | Response:
"""Login page""" """login page"""
return render_template("login.html", config=Config, page_name=name) if Config.is_logged():
flash("already logged.")
return redirect("/")
return render_template(
"login.html", config=Config, page_name=name, login_page=True, print=print
)
@router.route(f"/{name}", methods=["POST"]) @router.route(f"/{name}", methods=["POST"])
def check() -> Response: def check() -> Response:
"""Login logic""" """login logic"""
# TODO: Check password with the configuration if not Config.is_logged():
# TODO: Store a cookie if request.form["password"] == Config.user.password:
return jsonify({"pass": Config.user.password}) Config.loggin_in()
flash("logged.")
else:
flash("wrong password.")
else:
flash("already logged.")
return redirect("/")

View file

@ -2,7 +2,7 @@ html {
font-family: "Courier New", Courier, monospace; font-family: "Courier New", Courier, monospace;
} }
/* Align title with button */ /* align title with button */
header { header {
display: flex; display: flex;
align-items: center; align-items: center;
@ -10,6 +10,38 @@ header {
/* SVG centered on the line */ /* SVG centered on the line */
header svg { header svg {
margin-left: 0.5em; margin-left: 0.4em;
vertical-align: middle; vertical-align: middle;
} }
/* align button in login form */
span#login-form {
display: flex;
align-items: center;
}
/* button stylised as text */
span#login-form button {
margin-left: 0.4em;
background: none;
border: none;
cursor: pointer;
font-family: unset;
color: inherit;
font-size: unset;
}
/* link stylised as text */
span#login-form a {
color: inherit;
text-decoration: none;
}
span#login-form a::after {
content: " |";
}
span#login-form input {
display: block;
margin-bottom: 1em;
}

View file

@ -1,6 +1,7 @@
<header> <header>
<h1>txtdiary{{ " of " + config.user.name if config.user.name else "" }}</h1> <h1>txtdiary{{ " of " + config.user.name if config.user.name else "" }}</h1>
<form action="/login" method="post">
{% if not (login_page or config.is_logged()) -%}
<a href="/login"> <a href="/login">
<svg width="30px" fill="none" viewBox="0 0 24 24"> <svg width="30px" fill="none" viewBox="0 0 24 24">
<path <path
@ -8,5 +9,5 @@
d="M5 21a7 7 0 1 1 14 0M16 7a4 4 0 1 1-8 0 4 4 0 0 1 8 0Z" d="M5 21a7 7 0 1 1 14 0M16 7a4 4 0 1 1-8 0 4 4 0 0 1 8 0Z"
/></svg /></svg
></a> ></a>
</form> {% endif %}
</header> </header>

View file

@ -7,11 +7,16 @@
{% include "header.html" %} {% include "header.html" %}
<body> <body>
<p>Welcome to the index page</p> <aside>
<!-- TODO: list of clickable posts -->
<!-- TODO: side block for posting something new when connected --> <!-- TODO: side block for posting something new when connected -->
{% include "post.html" %} {% include "post.html" %}
</aside>
<main>
<p>{{ "".join(get_flashed_messages()) }}</p>
<h3>posts</h3>
<!-- TODO: list of clickable posts -->
</main>
</body> </body>
</html> </html>

View file

@ -5,11 +5,16 @@
</head> </head>
{% include "header.html" %} {% include "header.html" %}
<body> <body>
<p>Welcome to the login page</p> <main>
<h3>login page</h3>
<!-- TODO: Form --> <span id="login-form">
<form action="/login" method="post"> <form action="/login" method="post">
<button type="submit">Se connecter</button> <input type="password" name="password" placeholder="password" />
</form> <a href="/">Cancel</a>
<button type="submit">Login</button>
</form></span
>
</main>
</body> </body>
</html> </html>