-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (69 loc) · 3.13 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
82
83
84
85
86
87
88
from flask import Flask, render_template, redirect, url_for, flash
from flask_mysqldb import MySQL
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
import bcrypt
import os
app = Flask(__name__)
app.config['MYSQL_HOST'] = os.getenv('MYSQL_HOST', 'localhost')
app.config['MYSQL_USER'] = os.getenv('MYSQL_USER', 'root')
app.config['MYSQL_PASSWORD'] = os.getenv('MYSQL_PASSWORD', '')
app.config['MYSQL_DB'] = os.getenv('MYSQL_DB', 'bubble_game')
app.secret_key = os.getenv('SECRET_KEY', 'heyybuddy')
mysql = MySQL(app)
class SignupForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
verify_password = PasswordField('Verify Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Signup')
class SigninForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Singin')
@app.route('/')
def home(): # Route for home page
return render_template('bubble.html')
@app.route('/enter')
def enter(): # Changed function name from 'home' to 'enter'
return render_template('enter.html')
@app.route('/game')
def game(): # Renamed function from index to game
return render_template('index.html')
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if form.validate_on_submit():
username = form.username.data
email = form.email.data
password = form.password.data
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
cursor = mysql.connection.cursor()
cursor.execute('INSERT INTO users (username, email, password) VALUES (%s, %s, %s)', (username, email, hashed_password))
mysql.connection.commit()
cursor.close()
flash('You have successfully signed up!', 'success')
return redirect(url_for('enter'))
return render_template('signup.html', form = form)
@app.route('/signin', methods=['GET', 'POST'])
def signin():
form = SigninForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM users WHERE username = %s', (username,))
user = cursor.fetchone()
cursor.close()
if user:
if bcrypt.checkpw(password.encode('utf-8'), user[3].encode('utf-8')): # Ensure correct index for password
flash('You have successfully signed in!', 'success')
return redirect(url_for('enter'))
else:
flash('Invalid username or password!', 'danger')
else:
flash('Invalid username or password!', 'danger')
return render_template('signin.html', form=form)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.getenv('PORT', 5000)), debug=False)