-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (63 loc) · 2.32 KB
/
app.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from flask import Flask, render_template, request, redirect, url_for, session
import pymysql
app = Flask (__name__)
app.secret_key = '1234'
#MYSQL Configuration
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'maestro'
app.config['MYSQL_DB'] = 'user_sys'
mysql = pymysql.connect(
host=app.config['MYSQL_HOST'],
user=app.config['MYSQL_USER'],
password=app.config['MYSQL_PASSWORD'],
db=app.config['MYSQL_DB']
)
@app.route('/check')
def check_db():
if mysql is not None:
return "Database is connected!"
else:
return "Database connection failed."
@app.route('/')
def home():
if 'username' in session:
return render_template('home.html', username=session['username'])
else:
return render_template('home.html')
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cur = mysql.cursor()
cur.execute("SELECT username, password FROM user WHERE username = %s",(username,))
user = cur.fetchone()
cur.close()
if user and password == user[1]:
session['username'] = user[0]
return redirect(url_for('home'))
else:
return render_template('login.html', error= 'invalid username or password')
return render_template('login.html')
@app.route('/logout')
def logout():
# Clear the session to log the user out
session.pop('username', None)
return redirect(url_for('home'))
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
name = request.form['name']
cursor = mysql.cursor()
cursor.execute("INSERT INTO user (username, email, password, name) VALUES (%s, %s, %s, %s)", (username, email, password, name))
mysql.commit()
cursor.close()
# flash('Account created successfully', 'success')
return redirect(url_for('login')) # You can redirect to your login page after successful signup
return render_template('signup.html')
if __name__ == '__main__':
app.run(debug=True, port=5050)