pywallter/views/loginlogout.py

34 lines
1.4 KiB
Python

from flask import Blueprint, Flask, request, flash, render_template, url_for, session, redirect, abort, make_response, send_file, escape
import sqlite3
from flask_bcrypt import Bcrypt
app = Flask( 'pywallter' )
bcrypt = Bcrypt(app)
loginlogout = Blueprint('loginlogout', __name__, template_folder='templates')
@loginlogout.route( '/login/', methods=['GET','POST'] )
def login() :
if 'username' in session :
return redirect(url_for('filesupload.uploadfiles', _external=True))
else :
if request.method == 'POST' :
conn = sqlite3.connect('base.db') # Connexion à la base de donnée
cursor = conn.cursor() # Création de l'objet "curseur"
cursor.execute("""SELECT name, passwd FROM users""")
users = cursor.fetchall()
conn.close()
password = request.form['passwd']
for i in users:
if i[0] == request.form['user'] and bcrypt.check_password_hash(i[1], password) is True:
session['username'] = request.form['user']
return redirect(url_for('filesupload.uploadfiles', _external=True))
return redirect(url_for('loginlogout.login', _external=True))
else:
return render_template('login.html')
@loginlogout.route( '/logout/' )
def logout():
session.pop('username', None) # Supprimer username de la session s'il s'y trouve
return redirect(url_for('index'))