ciclos

commit 9d4d51e2d9628e3a93654649f6adf07a4adbfb8a

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

Handle session login auth

 requirements.txt | 1 +
 src/__init__.py | 12 +++++++++++-
 src/blueprints/auth.py | 22 +++++++++++++++++++---
 src/blueprints/basic.py | 6 ++++++
 src/types/user.py | 3 ++-


diff --git a/requirements.txt b/requirements.txt
index c9cd11f2cad819cf2383292ffe17217aa18b252c..028e77069ab4ce65dd8d47393eae7db8de3c3dee 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,7 @@
 alembic==1.3.1
 Click==7.0
 Flask==1.1.1
+Flask-Login==0.4.1
 Flask-SQLAlchemy==2.4.1
 itsdangerous==1.1.0
 Jinja2==2.10.3




diff --git a/src/__init__.py b/src/__init__.py
index c20b176d5ac18ce2f41bb93e3bd1b137eadf7c55..193b864c547019c53596ea67487d5cd2a5dd1024 100644
--- a/src/__init__.py
+++ b/src/__init__.py
@@ -1,5 +1,6 @@
 from flask import Flask, render_template
 from flask_sqlalchemy import SQLAlchemy
+from flask_login import LoginManager
 
 def create_app():
     app = Flask(__name__)
@@ -7,9 +8,18 @@
     # Database
     app.config['SECRET_KEY'] = '123456asckjnsac'
     app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/ciclos_dev'
-    
+
+    login_manager = LoginManager()
+    login_manager.login_view = 'auth.login'
+    login_manager.init_app(app)
+
     from .types import db
     db.init_app(app)
+    from .types.user import User
+
+    @login_manager.user_loader
+    def user_loader(user_id):
+        return User.query.get(int(user_id))
 
     # Blueprints
     from .blueprints.basic import basic




diff --git a/src/blueprints/auth.py b/src/blueprints/auth.py
index 7ce6e6d21dcdad0ba2a8f0c33354bf68f8d012c3..8438ab16738832e09617fb797e5bb2b121dc463f 100644
--- a/src/blueprints/auth.py
+++ b/src/blueprints/auth.py
@@ -1,15 +1,31 @@
 from flask import Blueprint, render_template, url_for, redirect, request, flash
 from ..types.user import User
-from werkzeug.security import generate_password_hash
+from werkzeug.security import generate_password_hash, check_password_hash
+from flask_login import login_user, current_user
 
 auth = Blueprint('auth', __name__, url_prefix = "/auth")
 
 @auth.route('/login', methods = ['GET', 'POST'])
 def login():
     if request.method == 'GET':
-        return render_template('login.html', title = "Entrar - Feira Virtual Bem da Terra")
+        if current_user:
+            return redirect(url_for('basic.profile'))
+        else:
+            return render_template('login.html', title = "Entrar - Feira Virtual Bem da Terra")
     else:
-        return 'work'
+        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.profile'))
 
 @auth.route('/sign_up', methods = ['GET', 'POST'])
 def sign_up():




diff --git a/src/blueprints/basic.py b/src/blueprints/basic.py
index d7a112501001f0c612d65024603733d94c932bf1..54eebbb6fdbaac17362d15e4164cae63d3b2f7c5 100644
--- a/src/blueprints/basic.py
+++ b/src/blueprints/basic.py
@@ -1,4 +1,5 @@
 from flask import Blueprint, render_template
+from flask_login import current_user, login_required
 basic = Blueprint('basic', __name__)
 
 @basic.route('/')
@@ -8,3 +9,8 @@
 @basic.route('/about')
 def about():
     return render_template('about.html', title = "Sobre a Feira Virtual Bem da Terra")
+
+@basic.route('/profile')
+@login_required
+def profile():
+    return "Your profile {}".format(current_user.email)




diff --git a/src/types/user.py b/src/types/user.py
index 2f41189b3f11125987dd77139ada034f0b4b69ea..50e7dc72fc33e604f1603a7fb03da40cc1df49c3 100644
--- a/src/types/user.py
+++ b/src/types/user.py
@@ -1,7 +1,8 @@
 from . import db
+from flask_login import UserMixin
 from datetime import datetime as dt
 
-class User(db.Model):
+class User(UserMixin, db.Model):
     __tablename__ = "users"
     id = db.Column(db.Integer, primary_key = True)
     username = db.Column(db.String(80), unique = True, nullable = False)