diff --git a/flash_auth_app/project/auth.py b/flash_auth_app/project/auth.py index 178166e..94d602b 100644 --- a/flash_auth_app/project/auth.py +++ b/flash_auth_app/project/auth.py @@ -1,4 +1,6 @@ -from flask import Blueprint +from flask import Blueprint, render_template, redirect, url_for, request +from werkzeug.security import generate_password_hash, check_password_hash +from .models import User from . import db auth = Blueprint('auth', __name__) @@ -10,14 +12,40 @@ as right now `login`, `signup`, and `logout` only return text There will also be routes for handling POST requests from login and signup ''' + @auth.route('/login') def login(): - return 'Login' + # return render_template('login.html') + return 'Login' # placeholder + @auth.route('/signup') def signup(): - return 'Signup' + # return render_template('signup.html') + return 'Signup' # placeholder + + +@auth.route('/signup', methods=['POST']) +def signup_post(): + # validate and add user to db + email = request.form.get('email') + name = request.form.get('name') + password = request.form.get('password') + + user = User.query.filter_by(email=email).first() # Check if the email exists + if user: # redirect back to sign-up page + return(redirect(url_for('auth.signup'))) + + # Create new user with form data + new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256')) + + # Add new user to db + db.session.add(new_user) + db.session.commit() + + return redirect(url_for('auth.login')) + @auth.route('/logout') def logout(): - return 'Logout' \ No newline at end of file + return 'Logout' # placeholder