Skip to content

Commit

Permalink
Register and Login new user
Browse files Browse the repository at this point in the history
  • Loading branch information
yamini27 committed Mar 27, 2020
1 parent 8d885b6 commit eb8fccd
Showing 1 changed file with 112 additions and 5 deletions.
117 changes: 112 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,119 @@
from flask import Flask,render_template
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
from functools import wraps

app = Flask(__name__)


@app.route('/')
def hello_world():
return render_template('basic.html')
# Config MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'your_mysql_password'
app.config['MYSQL_DB'] = 'myflaskapp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# init MYSQL
mysql = MySQL(app)


# Register Form Class
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=20)])
username = StringField('Username', [validators.Length(6)])
email = StringField('Email', [validators.Length(min=10, max=40)])
password = PasswordField('Password', [validators.DataRequired(),validators.EqualTo('confirm', message='Passwords do not match')])




# User Register
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))

# Create cursor
cur = mysql.connection.cursor()

# Execute query
cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))

# Commit to DB
mysql.connection.commit()

# Close connection
cur.close()

flash('You are now registered and can log in.', 'success')

return redirect(url_for('login'))
return render_template('register.html', form=form)


# User login
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Get Form Fields
username = request.form['username']
password_candidate = request.form['password']

# Create cursor
cur = mysql.connection.cursor()

# Get user by username
result = cur.execute("SELECT * FROM users WHERE username = %s", [username])

if result > 0:
# Get stored hash
data = cur.fetchone()
password = data['password']

# Compare Passwords
if sha256_crypt.verify(password_candidate, password):
# Passed
session['logged_in'] = True
session['username'] = username

flash('You are now logged in', 'success')
return redirect(url_for('dashboard'))
else:
error = 'Invalid login'
return render_template('login.html', error=error)
# Close connection
cur.close()
else:
error = 'Username not found.'
return render_template('login.html', error=error)

return render_template('login.html')

# Check if user logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized! Please login.', 'danger')
return redirect(url_for('login'))
return wrap

# Logout
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out!', 'success')
return redirect(url_for('login'))




if __name__ == '__main__':
app.run()
app.run(debug=True)

0 comments on commit eb8fccd

Please sign in to comment.