-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (79 loc) · 2.55 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask, render_template, request, redirect, url_for, session
from flask_session import Session
import mysql.connector
app = Flask(__name__)
# MySQL Configuration
mysql_config = {
'host': 'localhost',
'user': 'root',
'password': '',
'database': 'monument_db'
}
# Session Configuration
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SECRET_KEY'] = 'your_secret_key'
Session(app)
# Function to establish MySQL connection
def connect_to_mysql():
try:
conn = mysql.connector.connect(**mysql_config)
return conn
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
# Route for homepage
@app.route('/')
def index():
return redirect(url_for('login'))
# Route for login page
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
conn = connect_to_mysql()
if conn:
cursor = conn.cursor(dictionary=True)
cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s", (username, password))
user = cursor.fetchone()
cursor.close()
conn.close()
if user:
session['username'] = username
return redirect(url_for('dashboard'))
else:
return "Invalid username or password"
else:
return "Unable to connect to database"
return render_template('login.html')
# Route for registration page
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
conn = connect_to_mysql()
if conn:
cursor = conn.cursor()
cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (username, password))
conn.commit()
cursor.close()
conn.close()
return render_template('login.html')
else:
return "Unable to connect to database"
return render_template('register.html')
# Route for dashboard page
@app.route('/dashboard')
def dashboard():
if 'username' in session:
return render_template('dashboard.html', username=session['username'])
else:
return redirect(url_for('login'))
# Logout
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(debug=True)