<gh_stars>1-10
from flask import Blueprint, render_template, request, flash, redirect, url_for
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash
from . import db
from flask_login import login_user, login_required, logout_user, current_user


auth = Blueprint('auth', __name__)


@auth.route('/login',methods=['GET', 'POST'])
def login():
    data = request.form
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')

        user = User.query.filter_by(email=email).first()
        if user:
            if check_password_hash(user.password, password):
                flash('Logged in Successfully!', category='success')
                login_user(user, remember=True)
                return redirect(url_for('views.home'))
            else:
                flash('Incorrect Password, try again!', category='error')
        else:
           flash("Email Does not exist!", category='error') 


    return render_template("login.html", user=current_user)#, text="Test", user="Gaurav")


@auth.route('logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('auth.login'))


@auth.route('/sign-up',methods=['GET', 'POST'])
def sign_up():
    if request.method == "POST":
        email = request.form.get('email')
        firstName = request.form.get('firstName')
        password1 = request.form.get('password1')
        password2 = request.form.get('<PASSWORD>')

        user = User.query.filter_by(email=email).first()
        if user:
            flash("Email Already exists", category='error')


        if len(email) < 4:
            flash('Email Must Be Greater Than 4 Characters', category='error')
        elif len(firstName) < 2:
            flash('First Name Must Be Greater Than 1 Characters', category='error')
        elif password1 != password2:
            flash('Password 1 and Password 2 don\'t match', category='error')
        elif len(password1) < 7:
            flash('Password Must Be At Least 7 Characters', category='error')
        else:
            new_user = User(email=email,firstName=firstName,password=generate_password_hash(<PASSWORD>, method='sha256'))
            db.session.add(new_user)
            db.session.commit() 
            # login_user(user, remember=True)         
            login_user(new_user, remember=True)  
            flash('Account Created!', category='success')
            return redirect(url_for('views.home'))


    return render_template("sign_up.html", user=current_user)


from flask import Blueprint, render_template, request, flash, redirect, url_for
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash
from . import db
from flask_login import login_user, login_required, logout_user, current_user


auth = Blueprint('auth', __name__)


@auth.route('/login',methods=['GET', 'POST'])
def login():
    data = request.form
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')

        user = User.query.filter_by(email=email).first()
        if user:
            if check_password_hash(user.password, password):
                flash('Logged in Successfully!', category='success')
                login_user(user, remember=True)
                return redirect(url_for('views.home'))
            else:
                flash('Incorrect Password, try again!', category='error')
        else:
           flash("Email Does not exist!", category='error') 


    return render_template("login.html", user=current_user)#, text="Test", user="Gaurav")


@auth.route('logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('auth.login'))


@auth.route('/sign-up',methods=['GET', 'POST'])
def sign_up():
    if request.method == "POST":
        email = request.form.get('email')
        firstName = request.form.get('firstName')
        password1 = request.form.get('password1')
        password2 = request.form.get('<PASSWORD>')

        user = User.query.filter_by(email=email).first()
        if user:
            flash("Email Already exists", category='error')


        if len(email) < 4:
            flash('Email Must Be Greater Than 4 Characters', category='error')
        elif len(firstName) < 2:
            flash('First Name Must Be Greater Than 1 Characters', category='error')
        elif password1 != password2:
            flash('Password 1 and Password 2 don\'t match', category='error')
        elif len(password1) < 7:
            flash('Password Must Be At Least 7 Characters', category='error')
        else:
            new_user = User(email=email,firstName=firstName,password=generate_password_hash(<PASSWORD>, method='sha256'))
            db.session.add(new_user)
            db.session.commit() 
            # login_user(user, remember=True)         
            login_user(new_user, remember=True)  
            flash('Account Created!', category='success')
            return redirect(url_for('views.home'))


    return render_template("sign_up.html", user=current_user)
#, text="Test", user="Gaurav") # login_user(user, remember=True)