added a better way to show if username or email is taken
This commit is contained in:
@@ -3,6 +3,7 @@ from functools import wraps
|
||||
from flask import Flask, render_template, redirect, request, url_for, session, flash
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -25,28 +26,40 @@ def login_required(view_func):
|
||||
@wraps(view_func)
|
||||
def wrapped(*args, **kwargs):
|
||||
if 'user_id' not in session:
|
||||
flash('Please sign in to view that page.')
|
||||
flash('sign in to view that page.')
|
||||
return redirect(url_for('signin'))
|
||||
return view_func(*args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
@app.route('/submit-form', methods=['POST'])
|
||||
def handle_submission():
|
||||
form_username = request.form.get('username')
|
||||
form_email = request.form.get('email')
|
||||
form_password = request.form.get('password')
|
||||
form_username = (request.form.get('username') or '').strip()
|
||||
form_email = (request.form.get('email') or '').strip().lower()
|
||||
form_password = request.form.get('password') or ''
|
||||
|
||||
if not form_username or not form_email or not form_password:
|
||||
return render_template('signup.html', error="All fields are required"), 400
|
||||
|
||||
if len(form_password) < 8:
|
||||
return render_template('signup.html', error="Password must be at least 8 characters"), 400
|
||||
|
||||
if User.query.filter_by(username=form_username).first():
|
||||
return "<h3>Username taken!</h3>", 400
|
||||
return render_template('signup.html', user_taken="Username has already been taken"), 400
|
||||
if User.query.filter_by(email=form_email).first():
|
||||
return "<h3>Email already registered!</h3>", 400
|
||||
return render_template('signup.html', email_taken="Email has already been used"), 400
|
||||
|
||||
hashed_password = generate_password_hash(form_password)
|
||||
|
||||
new_user = User(username=form_username, email=form_email, password_hash=hashed_password)
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
|
||||
try:
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
return render_template('signup.html', error="Username or email already taken"), 400
|
||||
|
||||
return render_template('signup.html', success_message="Account successfully created")
|
||||
|
||||
|
||||
+19
-1
@@ -12,12 +12,30 @@
|
||||
<input type="password" id="password" name="password" required><br><br>
|
||||
|
||||
<button type="submit" class="submitbutton" style="font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; border-color: gray; border-radius: 4px; border-width: 4px;">Create account</button>
|
||||
{% if success_message %}
|
||||
|
||||
{% if success_message %}
|
||||
<p style="color: green; margin-top: 15px; font-weight: bold;">
|
||||
{{ success_message }}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if user_taken %}
|
||||
<p style="color: rgb(128, 0, 0); margin-top: 15px; font-weight: bold;">
|
||||
{{ user_taken }}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if email_taken %}
|
||||
<p style="color: rgb(128, 0, 0); margin-top: 15px; font-weight: bold;">
|
||||
{{ email_taken }}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if error %}
|
||||
<p style="color: rgb(128, 0, 0); margin-top: 15px; font-weight: bold;">
|
||||
{{ error }}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
</form>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user