ciclos

commit 54a83aafeab88c80564ea0ddf4cc7bd3ed1455a2

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Handle login and registration of users

 core/blueprints/auth.py | 68 +++++++++++++++++++++++++++++++++++++++
 core/templates/login.html | 32 ++++++++++++++++++
 core/templates/sign_up.html | 45 +++++++++++++++++++++++++


diff --git a/core/blueprints/auth.py b/core/blueprints/auth.py
new file mode 100644
index 0000000000000000000000000000000000000000..29f3365dd688809443208dd70ea7a88e30003bb8
--- /dev/null
+++ b/core/blueprints/auth.py
@@ -0,0 +1,68 @@
+from flask import Blueprint, render_template, url_for, redirect, request, flash
+from ..types.user import User
+from werkzeug.security import generate_password_hash, check_password_hash
+from flask_login import login_user, current_user, logout_user
+
+from core.database import db
+
+auth = Blueprint('auth', __name__, url_prefix = "/auth")
+
+@auth.route('/login', methods = ['GET', 'POST'])
+def login():
+    if request.method == 'GET':
+        if current_user.is_authenticated:
+            return redirect(url_for('basic.index'))
+        else:
+            return render_template('login.html', title = "Entrar - Feira Virtual Bem da Terra")
+    else:
+        email = request.form.get('email')
+        password = request.form.get('password')
+
+        user = User.query.filter_by(email = email).first()
+
+        if not user or not check_password_hash(user.password, password):
+            flash('User not found or credentials does not match our records...')
+
+            return redirect(url_for('auth.login'))
+
+        # User exists and works!
+        login_user(user, remember = False)
+        return redirect(url_for('basic.index'))
+
+@auth.route('/sign_up', methods = ['GET', 'POST'])
+def sign_up():
+    if request.method == 'GET':
+        return render_template('sign_up.html', title = "Cadastrar - Feira Virtual Bem da Terra")
+    else:
+        email = request.form.get('email')
+        user = User.query.filter_by(email = email).first()
+
+        if user:
+            flash('You already have an account, perhaps log in?')
+            return redirect(url_for('auth.login'))
+        else:
+            username   = request.form.get('username')
+            password   = request.form.get('password')
+            first_name = request.form.get('first_name')
+            last_name  = request.form.get('last_name')
+
+            new_user = User(
+                    email,
+                    generate_password_hash(password, method = 'sha256'),
+                    username,
+                    first_name,
+                    last_name
+            )
+
+            db.add(new_user)
+            db.commit()
+
+            flash('Account created successfully')
+            return redirect(url_for('auth.login'))
+
+@auth.route('/logout')
+def logout():
+    logout_user()
+
+    return redirect(url_for('basic.index'))
+




diff --git a/core/templates/login.html b/core/templates/login.html
new file mode 100644
index 0000000000000000000000000000000000000000..e44cddf9b6aa7c9d85a728b93a9c8410a99612c5
--- /dev/null
+++ b/core/templates/login.html
@@ -0,0 +1,32 @@
+{% extends 'layout.html' %}
+
+{% block body %}
+  {% with messages = get_flashed_messages() %}
+  {% if messages %}
+    <section>
+      {{ messages[0] }}
+    </section>
+  {% endif %}
+  {% endwith %}
+  <section>
+    <h2> Entrar na sua conta </h2>
+    <p> Use suas credenciais para entrar na sua conta </p>
+  </section>
+
+  <form method="POST" action="/auth/login">
+    <div class='form-section'>
+      <label for="email">Email</label>
+      <input type="email" placeholder="seu@email.com" name="email" />
+    </div>
+
+    <div class='form-section'>
+      <label for="password">Senha</label>
+      <input type="password" placeholder="******" name="password" />
+    </div>
+
+    <div class='form-action'>
+      <button type="submit" rel="button"> Entrar </button>
+    </div>
+  </form>
+{% endblock %}
+




diff --git a/core/templates/sign_up.html b/core/templates/sign_up.html
new file mode 100644
index 0000000000000000000000000000000000000000..0c8a2e85392e4d7da7484a503ac6225478dc40bf
--- /dev/null
+++ b/core/templates/sign_up.html
@@ -0,0 +1,45 @@
+{% extends 'layout.html' %}
+
+{% block body %}
+  <form method="POST" action="/auth/sign_up">
+    <section>
+      <h2> Comece a consumir responsávelmente </h2>
+      <p>
+        Ao se cadastrar você poderá <mark>fazer pedidos</mark> na feira, acompanhar os ciclos
+        e contribuir para uma alimentação <mark>saudável</mark>, <mark>responsável</mark> e <mark>consciente</mark>.
+      </p>
+    </section>
+
+    <div class='form-section'>
+      <label for="username">Username</label>
+      <input type="text" placeholder="Username" name="username" />
+    </div>
+
+    <div class='form-section'>
+      <label for="email">Email</label>
+      <input type="email" placeholder="email@host.com" name="email" />
+    </div>
+
+    <div class='form-section'>
+      <label for="password">Password</label>
+      <input type="password" placeholder="********" name="password" />
+    </div>
+
+    <div class='form-section'>
+      <label for="first_name">First Name</label>
+      <input type="text" placeholder="John Doe" name="first_name" />
+    </div>
+
+    <div class='form-section'>
+      <label for="last_name"> Last Name </label>
+      <input type="text" placeholder="Doe" name="last_name" />
+    </div>
+
+    <div class='form-action'>
+      <button type="submit"> Sign Up </button>
+    </div>
+
+  </form>
+
+{% endblock %}
+