Exemplo n.º 1
0
def login():
    login_form = LoginForm()
    context = {'login_form': login_form}
    if login_form.is_submitted():
        user_id = login_form.username.data
        password = login_form.password.data

        user_doc = get_user(user_id)
        if user_doc.to_dict() is not None:
            password_from_db = user_doc.to_dict()['password']

            if check_password_hash(password_from_db, password):
                user_name = user_doc.to_dict()['user']
                user_data = UserData(user_id, user_name, password)
                user = UserModel(user_data)
                login_user(user)
                if (not existKey(user_id)):
                    return redirect(url_for('auth.keygen'))
                return redirect(url_for('index'))
            else:
                flash('Contraseña invalida')
        else:
            flash('El nombre de usuario No existe Intente de nuevo')

    return render_template('login.html', **context)
Exemplo n.º 2
0
def homepage():

    if not session.new:
        redirect(url_for('chart'))

    form = InputForm()
    entry = LoginForm()

    if request.method == 'POST' and form.is_submitted():
        try:
            name = request.form.get('nome')
            email = request.form.get('email')
            password = request.form.get('pass')
            vpass = request.form.get('confirmPass')
            income = request.form.get('salario')
            savings = request.form.get('poupanca')
            debt = request.form.get('divida')
            dtype = request.form.get('tpDivida')
            interestrate = request.form.get('juros')
            obj = request.form.get('objetivo')
            rent = request.form.get('aluguel')
            transport = request.form.get('transporte')
            food = request.form.get('alimentacao')
            bills = request.form.get('contas')
            shopping = request.form.get('compras')
            extra = request.form.get('outros')

            if password != vpass:
                raise ValueError(
                    "Os campos senha e confirmação de senha não batem.")

            hashed_password = pwcrypt.generate_password_hash(password).decode(
                'utf-8')

            newuser = User(username=name,
                           email=email,
                           password=hashed_password,
                           income=income,
                           savings=savings,
                           debt=debt,
                           debttype=dtype,
                           interestrate=interestrate,
                           objective=obj,
                           rent=rent,
                           transport=transport,
                           food=food,
                           bills=bills,
                           shopping=shopping,
                           extra=extra)

            db.session.add(newuser)
            db.session.commit()
            redirect(url_for('chart'))

        except Exception:
            pass

        finally:
            return render_template("homepage.html", form=form, entry=entry)

    elif request.method == 'POST' and entry.is_submitted():
        try:
            uname = request.entry.get('nome')
            pwcheck = request.entry.get('pass')
            user = User.query.filter_by(email=form.email.data).first()
            dbpass = User.query.filter_by(username=uname).password

            if user and pwcrypt.check_password_hash(dbpass, pwcheck):
                login_user(user)
                redirect(url_for('handle_form'))
            else:
                return render_template("homepage.html", form=form, entry=entry)

        except Exception:
            pass

    return render_template("homepage.html", form=form, entry=entry)