<gh_stars>0
# App Imports Starts Here
import MySQLdb.cursors
import re
from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
# App Imports Ends Here

# Main App Variables Starts here
app = Flask(__name__)
# Main App Variables Ends here

# App SECRET_KEY Starts here
app.config['SECRET_KEY'] = 'DB4D6FF8D69918A95BE1F138944DD'
# App SECREY_KEY Ends here
# MySQL Configuration Starts here
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'devaforpython'
app.config['MYSQL_PASSWORD'] = '<PASSWORD>'
app.config['MYSQL_DB'] = 'login'
# MySQL Configuartion Ends here

# App Variables Starts here
mysql = MySQL(app)
# App Variables Ends here


# App Routes Starts here
@app.route('/')
@app.route('/login', methods =['GET', 'POST'])
def login():
	msg = ''
	if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
		username = request.form['username']
		password = request.form['password']
		cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
		cursor.execute('SELECT * FROM accounts WHERE username = % s AND password = % s', (username, password, ))
		account = cursor.fetchone()
		if account:
			session['loggedin'] = True
			session['id'] = account['id']
			session['username'] = account['username']
			msg = 'Logged in successfully !'
			return redirect(url_for('index'))
		else:
			msg = 'Incorrect username / password !'
	return render_template('login.html', msg = msg)

@app.route('/logout')
def logout():
	session.pop('loggedin', None)
	session.pop('id', None)
	session.pop('username', None)
	return redirect(url_for('login'))


@app.route("/index")
def index():
	if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
	 username = request.form['username']
	 password = request.form['password']
	 cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
	 cursor.execute('SELECT * FROM accounts WHERE username = % s AND password = % s', (username, password, ))
	 account = cursor.fetchone()
	 if account:
		session['loggedin'] = True
		session['id'] = account['id']
		session['username'] = account['username']
		msg = 'Welcome'
	return render_template('index.html', msg = msg)


@app.route('/register', methods =['GET', 'POST'])
def register():
	msg = ''
	if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form :
		username = request.form['username']
		password = request.form['password']
		email = request.form['email']
		cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
		cursor.execute('SELECT * FROM accounts WHERE username = % s', (username, ))
		account = cursor.fetchone()
		if account:
			msg = 'Account already exists !'
		elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
			msg = 'Invalid email address !'
		elif not re.match(r'[A-Za-z0-9]+', username):
			msg = 'Username must contain only characters and numbers !'
		elif not username or not password or not email:
			msg = 'Please fill out the form !'
		else:
			cursor.execute('INSERT INTO accounts VALUES (NULL, % s, % s, % s)', (username, password, email, ))
			mysql.connection.commit()
			msg = 'You have successfully registered !'
	elif request.method == 'POST':
		msg = 'Please fill out the form !'
	return render_template('register.html', msg = msg)
   # App Routes End Here

# App Loop
if __name__ == "__main__":
 app.run(debug=True, host="0.0.0.0", port=1234)