Allow users to login

This commit is contained in:
2018-06-29 08:03:49 +02:00
parent e5e93c60b3
commit b68788f65c
7 changed files with 116 additions and 3 deletions

View File

@@ -18,26 +18,36 @@ import os
from flask import Flask, render_template
from flask_babel import Babel
from flask_security import SQLAlchemyUserDatastore, current_user
class CalendarSocialApp(Flask):
def __init__(self, name, config=None):
from app.models import db
from app.models import db, User, Role
from app.security import security
Flask.__init__(self, name)
config_name = os.environ.get('ENV', config or 'dev')
self.config.from_pyfile(f'config_{config_name}.py', True)
# Make sure we look up users both by their usernames and email addresses
self.config['SECURITY_USER_IDENTITY_ATTRIBUTES'] = ('username', 'email')
db.init_app(self)
babel = Babel(app=self)
user_store = SQLAlchemyUserDatastore(db, User, Role)
security.init_app(self, datastore=user_store)
app = CalendarSocialApp(__name__)
@app.route('/')
def hello():
return render_template('welcome.html')
if not current_user.is_authenticated:
return render_template('welcome.html')
return render_template('index.html')
if __name__ == '__main__':

View File

@@ -2,3 +2,6 @@ DEBUG = True
ENV = 'dev'
SQLALCHEMY_DATABASE_URI = 'sqlite:///local.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = 'ThisIsNotSoSecret'
SECURITY_PASSWORD_HASH = 'bcrypt'
SECURITY_PASSWORD_SALT = SECRET_KEY

3
app/security.py Normal file
View File

@@ -0,0 +1,3 @@
from flask_security import Security
security = Security()

View File

@@ -16,6 +16,16 @@
<body>
<header>
<h1>Calendar.social</h1>
<nav class="menu">
{{ _('Logged in as %(username)s', username=current_user.username) }}
<ul>
{% if not current_user.is_authenticated %}
<li><a href="{{ url_for('security.login') }}">{% trans %}Login{% endtrans %}</a></li>
{% else %}
<li><a href="{{ url_for('security.logout') }}">{% trans %}Logout{% endtrans %}</a></li>
{% endif %}
</ul>
</nav>
</header>
{% block content %}{% endblock %}
<footer>

5
app/templates/index.html Normal file
View File

@@ -0,0 +1,5 @@
{% extends 'base.html' %}
{% block content %}
{{ _('Welcome to Calendar.social, %(username)s!', username=current_user.username) }}
{% endblock content %}