-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask.zip
67 lines (55 loc) · 2.19 KB
/
flask.zip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL,MySQLdb
import bcrypt
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'flaskdb'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
@app.route('/')
def home():
return render_template("home.html")
@app.route('/login',methods=["GET","POST"])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password'].encode('utf-8')
curl = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
curl.execute("SELECT * FROM users WHERE email=%s",(email,))
user = curl.fetchone()
curl.close()
if len(user) > 0:
if bcrypt.hashpw(password, user["password"].encode('utf-8')) == user["password"].encode('utf-8'):
session['name'] = user['name']
session['email'] = user['email']
return render_template("home.html")
else:
return "Error password and email not match"
else:
return "Error user not found"
else:
return render_template("login.html")
@app.route('/logout', methods=["GET", "POST"])
def logout():
session.clear()
return render_template("home.html")
@app.route('/register', methods=["GET", "POST"])
def register():
if request.method == 'GET':
return render_template("register.html")
else:
name = request.form['name']
email = request.form['email']
password = request.form['password'].encode('utf-8')
hash_password = bcrypt.hashpw(password, bcrypt.gensalt())
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users (name, email, password) VALUES (%s,%s,%s)",(name,email,hash_password,))
mysql.connection.commit()
session['name'] = request.form['name']
session['email'] = request.form['email']
return redirect(url_for('home'))
if __name__ == '__main__':
app.secret_key = "^A%DJAJU^JJ123"
app.run(debug=True)