commit af5478994ccb1895119fd325e96bc8f440f6cde9 Author: Llloooggg Date: Wed Jul 14 01:32:23 2021 +0300 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ee5e1a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.vscode + +__pycache__ +venv + +db.sqlite \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..11282a3 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Dyxless + +Based on [tutorial](https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login) from DigitalOcean + +### Database +At first run **init_db.py** for database initialization + +### Start app: +on Windows run +>**startserver.bat** \ No newline at end of file diff --git a/dyxless/__init__.py b/dyxless/__init__.py new file mode 100644 index 0000000..347e6ae --- /dev/null +++ b/dyxless/__init__.py @@ -0,0 +1,36 @@ +from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_login import LoginManager + +db = SQLAlchemy() + + +def create_app(): + app = Flask("__name__", template_folder="dyxless/templates") + + app.config["SECRET_KEY"] = "monastyrka-says-helloworld" + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite" + + app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False + + db.init_app(app) + + login_manager = LoginManager() + login_manager.login_view = "auth.login" + login_manager.init_app(app) + + from .models import User + + @login_manager.user_loader + def load_user(user_id): + return User.query.get(int(user_id)) + + from .auth import auth as auth_blueprint + + app.register_blueprint(auth_blueprint) + + from .main import main as main_blueprint + + app.register_blueprint(main_blueprint) + + return app diff --git a/dyxless/auth.py b/dyxless/auth.py new file mode 100644 index 0000000..3f9cccd --- /dev/null +++ b/dyxless/auth.py @@ -0,0 +1,70 @@ +from flask import Blueprint, render_template, redirect, url_for, request, flash +from werkzeug.security import generate_password_hash, check_password_hash +from flask_login import current_user, login_user, logout_user + +from .models import User +from . import db + +auth = Blueprint("auth", __name__) + + +@auth.route("/login", methods=["GET", "POST"]) +def login(): + + if current_user.is_authenticated: + return redirect(url_for("main.index")) + + elif request.method == "GET": + return render_template("login.html") + + elif request.method == "POST": + email = request.form.get("email") + password = request.form.get("password") + remember = True if request.form.get("remember") else False + + user = User.query.filter_by(email=email).first() + + if not user or not check_password_hash(user.password, password): + flash("Please check your login details and try again.") + return redirect(url_for("auth.login")) + + login_user(user, remember=remember) + return redirect(url_for("main.profile")) + + +@auth.route("/signup", methods=["GET", "POST"]) +def signup(): + + if current_user.is_authenticated: + return redirect(url_for("main.index")) + + elif request.method == "GET": + return render_template("signup.html") + + elif request.method == "POST": + email = request.form.get("email") + name = request.form.get("name") + password = request.form.get("password") + + user = User.query.filter_by(email=email).first() + + if user: + flash("Email address already exists") + return redirect(url_for("auth.signup")) + + new_user = User( + email=email, + name=name, + password=generate_password_hash(password, method="sha256"), + ) + + db.session.add(new_user) + db.session.commit() + + return redirect(url_for("auth.login")) + + +@auth.route("/logout") +def logout(): + logout_user() + return redirect(url_for("main.index")) diff --git a/dyxless/main.py b/dyxless/main.py new file mode 100644 index 0000000..f8e9e57 --- /dev/null +++ b/dyxless/main.py @@ -0,0 +1,15 @@ +from flask import Blueprint, render_template +from flask_login import login_required + +main = Blueprint("main", __name__) + + +@main.route("/") +def index(): + return render_template("index.html") + + +@main.route("/profile") +@login_required +def profile(): + return render_template("profile.html") diff --git a/dyxless/models.py b/dyxless/models.py new file mode 100644 index 0000000..abf4188 --- /dev/null +++ b/dyxless/models.py @@ -0,0 +1,10 @@ +from flask_login import UserMixin + +from . import db + + +class User(UserMixin, db.Model): + id = db.Column(db.Integer, primary_key=True) + email = db.Column(db.String(100), unique=True) + password = db.Column(db.String(100)) + name = db.Column(db.String(1000)) diff --git a/dyxless/templates/base.html b/dyxless/templates/base.html new file mode 100644 index 0000000..4c441d0 --- /dev/null +++ b/dyxless/templates/base.html @@ -0,0 +1,57 @@ + + + + + + + + Flask Auth Example + + + + +
+ +
+ +
+ +
+
+ {% block content %} + {% endblock %} +
+
+
+ + + \ No newline at end of file diff --git a/dyxless/templates/index.html b/dyxless/templates/index.html new file mode 100644 index 0000000..ce45b56 --- /dev/null +++ b/dyxless/templates/index.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} + +{% block content %} +

+ Flask Login Example +

+

+ Easy authentication and authorization in Flask. +

+{% endblock %} \ No newline at end of file diff --git a/dyxless/templates/login.html b/dyxless/templates/login.html new file mode 100644 index 0000000..6ec1a6f --- /dev/null +++ b/dyxless/templates/login.html @@ -0,0 +1,36 @@ +{% extends "base.html" %} + +{% block content %} +
+

Login

+
+ {% with messages = get_flashed_messages() %} + {% if messages %} +
+ {{ messages[0] }} +
+ {% endif %} + {% endwith %} +
+
+
+ +
+
+ +
+
+ +
+
+
+ +
+ +
+
+
+{% endblock %} \ No newline at end of file diff --git a/dyxless/templates/profile.html b/dyxless/templates/profile.html new file mode 100644 index 0000000..a5b2d53 --- /dev/null +++ b/dyxless/templates/profile.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block content %} +

+ Welcome, {{ current_user.name }}! +

+{% endblock %} \ No newline at end of file diff --git a/dyxless/templates/signup.html b/dyxless/templates/signup.html new file mode 100644 index 0000000..d3f5b8a --- /dev/null +++ b/dyxless/templates/signup.html @@ -0,0 +1,37 @@ +{% extends "base.html" %} + +{% block content %} +
+

Sign Up

+
+ {% with messages = get_flashed_messages() %} + {% if messages %} +
+ {{ messages[0] }}. Go to login page. +
+ {% endif %} + {% endwith %} +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ + +
+
+
+{% endblock %} \ No newline at end of file diff --git a/init_db.py b/init_db.py new file mode 100644 index 0000000..d76d8a8 --- /dev/null +++ b/init_db.py @@ -0,0 +1,3 @@ +from dyxless import db, create_app + +db.create_all(app=create_app()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..21c315b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +click==8.0.1 +colorama==0.4.4 +Flask==2.0.1 +Flask-Login==0.5.0 +Flask-SQLAlchemy==2.5.1 +greenlet==1.1.0 +itsdangerous==2.0.1 +Jinja2==3.0.1 +MarkupSafe==2.0.1 +SQLAlchemy==1.4.20 +Werkzeug==2.0.1 diff --git a/startserver.bat b/startserver.bat new file mode 100644 index 0000000..7c7b9b1 --- /dev/null +++ b/startserver.bat @@ -0,0 +1,5 @@ +@echo off +set FLASK_APP=dyxless +set FLASK_ENV=development + +flask run \ No newline at end of file