2023-02-11 08:37:10 -05:00
|
|
|
from flask import Flask, render_template, request, redirect, session
|
|
|
|
from flask_session import Session
|
|
|
|
import utils
|
|
|
|
import database
|
2023-02-12 06:29:18 -05:00
|
|
|
import mail
|
2023-02-10 08:17:47 -05:00
|
|
|
|
|
|
|
app = Flask(__name__);
|
2023-02-11 08:37:10 -05:00
|
|
|
app.config["SESSION_PERMANENT"] = False
|
|
|
|
app.config["SESSION_TYPE"] = "filesystem"
|
|
|
|
Session(app)
|
2023-02-10 08:17:47 -05:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def home():
|
2023-02-11 08:37:10 -05:00
|
|
|
return (render_template("home.html"));
|
|
|
|
|
|
|
|
@app.route("/connected")
|
|
|
|
def connected():
|
|
|
|
if (not session.get("email")):
|
|
|
|
return (redirect("/login"))
|
|
|
|
return (render_template("connected.html"));
|
|
|
|
|
|
|
|
@app.route("/login")
|
|
|
|
def login():
|
2023-02-12 06:29:18 -05:00
|
|
|
if (session.get("email")):
|
|
|
|
return (redirect("/connected"))
|
2023-02-11 08:37:10 -05:00
|
|
|
return (render_template("login.html"));
|
|
|
|
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
|
|
def login_post():
|
|
|
|
email = request.form.get('email')
|
|
|
|
password = request.form.get('password')
|
|
|
|
if (not database.user_exist(email)):
|
|
|
|
return (render_template("login.html", error="Email ou mot de passe faux"))
|
|
|
|
if (not database.check_password(email, password)):
|
|
|
|
return (render_template("login.html", error="Email ou mot de passe faux"))
|
|
|
|
session["email"] = email
|
|
|
|
return (redirect("/connected"))
|
|
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
|
|
|
session.pop('email', None)
|
|
|
|
return redirect("/login")
|
|
|
|
|
|
|
|
@app.route("/signin")
|
|
|
|
def signin():
|
2023-02-12 06:29:18 -05:00
|
|
|
if (session.get("email")):
|
|
|
|
return (redirect("/connected"))
|
2023-02-11 08:37:10 -05:00
|
|
|
return (render_template("signin.html"));
|
|
|
|
|
|
|
|
@app.route('/signin', methods=['POST'])
|
2023-02-12 06:29:18 -05:00
|
|
|
def signin_post():
|
2023-02-11 08:37:10 -05:00
|
|
|
email = request.form.get('email')
|
|
|
|
password = request.form.get('password')
|
|
|
|
repassword = request.form.get('repassword')
|
|
|
|
if (password != repassword):
|
|
|
|
return (render_template("signin.html", error="Les deux mots de passe sont differents"))
|
|
|
|
if (utils.check_email(email)):
|
|
|
|
return (render_template("signin.html", error="Votre email n'est pas valdie"))
|
|
|
|
if (database.user_exist(email)):
|
|
|
|
return (render_template("signin.html", error="Email deja utilisé"))
|
|
|
|
database.add_user(email, password);
|
|
|
|
return (redirect("/connected"))
|
|
|
|
|
|
|
|
@app.route("/forgot")
|
|
|
|
def forgot():
|
|
|
|
return (render_template("forgot.html"));
|
|
|
|
|
2023-02-12 06:29:18 -05:00
|
|
|
@app.route("/forgot", methods=["POST"])
|
|
|
|
def forgot_post():
|
|
|
|
email = request.form.get('email')
|
|
|
|
if (not database.email_exist(email)):
|
|
|
|
return (render_template("forgot.html", error="Ce compte n'existe pas'"))
|
|
|
|
uuid = database.create_reset_code_by_email(email);
|
|
|
|
mail.send_mail(email, "Password recovery", render_template("mails/password_recovery.html", code=uuid))
|
|
|
|
return (redirect(f"/reset/{uuid}"))
|
|
|
|
|
2023-02-11 08:37:10 -05:00
|
|
|
@app.route("/reset/<uuid>")
|
|
|
|
def reset(uuid):
|
2023-02-12 06:29:18 -05:00
|
|
|
if (not database.reset_code_exist(uuid)):
|
|
|
|
return (render_template("error.html", error="code inconnu"))
|
|
|
|
email = database.get_email_by_reset_code(uuid)
|
|
|
|
email = email["email"]
|
|
|
|
return (render_template("reset.html", email=email))
|
|
|
|
|
|
|
|
@app.route("/reset/<uuid>", methods=['POST'])
|
|
|
|
def reset_post(uuid):
|
|
|
|
if (not database.reset_code_exist(uuid)):
|
|
|
|
return (render_template("error.html", error="code inconnu"))
|
|
|
|
email = database.get_email_by_reset_code(uuid)
|
|
|
|
password = request.form.get('password')
|
|
|
|
repassword = request.form.get('repassword')
|
|
|
|
if (password != repassword):
|
|
|
|
return (render_template("reset.html", error="Les deux mots de passe sont differents"))
|
|
|
|
database.change_user_password(email, password);
|
|
|
|
database.remove_reset_code_by_code(uuid);
|
|
|
|
return (redirect("/login"))
|
2023-02-11 08:37:10 -05:00
|
|
|
|
|
|
|
@app.route("/join/<uuid>")
|
|
|
|
def join(uuid):
|
|
|
|
return ("bozo")
|
2023-02-10 08:17:47 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-02-11 08:37:10 -05:00
|
|
|
app.run(host='0.0.0.0', port=5000, debug=1)
|