From 9e65eb55eb80961fb4f4d1d7e38fcf72fc4ad809 Mon Sep 17 00:00:00 2001 From: ada <> Date: Mon, 23 Oct 2023 13:03:55 +0200 Subject: [PATCH] strictly files --- __init__.py | 37 ++++++++++++++++++++++++ auth.py | 63 +++++++++++++++++++++++++++++++++++++++++ db.sqlite | Bin 0 -> 3072 bytes main.py | 15 ++++++++++ models.py | 10 +++++++ templates/base.html | 59 ++++++++++++++++++++++++++++++++++++++ templates/index.html | 12 ++++++++ templates/login.html | 38 +++++++++++++++++++++++++ templates/profile.html | 9 ++++++ templates/signup.html | 39 +++++++++++++++++++++++++ 10 files changed, 282 insertions(+) create mode 100644 __init__.py create mode 100644 auth.py create mode 100644 db.sqlite create mode 100644 main.py create mode 100644 models.py create mode 100644 templates/base.html create mode 100644 templates/index.html create mode 100644 templates/login.html create mode 100644 templates/profile.html create mode 100644 templates/signup.html diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..2426574 --- /dev/null +++ b/__init__.py @@ -0,0 +1,37 @@ +# init.py + +from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_login import LoginManager + +# init SQLAlchemy so we can use it later in our models +db = SQLAlchemy() + +def create_app(): + app = Flask(__name__) + + app.config['SECRET_KEY'] = '9OLWxND4o83j4K4iuopO' + app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' + + 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): + # since the user_id is just the primary key of our user table, use it in the query for the user + return User.query.get(int(user_id)) + + # blueprint for auth routes in our app + from .auth import auth as auth_blueprint + app.register_blueprint(auth_blueprint) + + # blueprint for non-auth parts of app + from .main import main as main_blueprint + app.register_blueprint(main_blueprint) + + return app \ No newline at end of file diff --git a/auth.py b/auth.py new file mode 100644 index 0000000..c814b8b --- /dev/null +++ b/auth.py @@ -0,0 +1,63 @@ +# auth.py + +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 login_user, logout_user, login_required +from .models import User +from . import db + +auth = Blueprint('auth', __name__) + +@auth.route('/login') +def login(): + return render_template('login.html') + +@auth.route('/login', methods=['POST']) +def login_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() + + # check if user actually exists + # take the user supplied password, hash it, and compare it to the hashed password in database + 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')) # if user doesn't exist or password is wrong, reload the page + + # if the above check passes, then we know the user has the right credentials + login_user(user, remember=remember) + return redirect(url_for('main.profile')) + +@auth.route('/signup') +def signup(): + return render_template('signup.html') + +@auth.route('/signup', methods=['POST']) +def signup_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 this returns a user, then the email already exists in database + + if user: # if a user is found, we want to redirect back to signup page so user can try again + flash('Email address already exists') + return redirect(url_for('auth.signup')) + + # create new user with the form data. Hash the password so plaintext version isn't saved. + new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256')) + + # add the new user to the database + db.session.add(new_user) + db.session.commit() + + return redirect(url_for('auth.login')) + +@auth.route('/logout') +@login_required +def logout(): + logout_user() + return redirect(url_for('main.index')) \ No newline at end of file diff --git a/db.sqlite b/db.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..6fa609435e1f6436b560ff8c1df22178abb2c86d GIT binary patch literal 3072 zcmeH{PjAyO6u_N$Dzws$)ilAunkvyE%HqUM9C6sf6hTEPYt|y|RJpN3BQ;5vq_7^# ziI2pWnD`KU0&W~h+7(UOVONmyr1xw;|9SS$dhcS`4|9ovB#n5E46p|j1)L%T0D!kz zG#gP0yLa0!n{xb+K+yg5eINb?Dm()42>y6zLp5I!__qkGpDT|JnoZ@amGkLbE;E^y zs-AhSGjh?$dEIwW=|SyAJrt-n7`bm;4-Gzy&|uu}zd((;jCeRlA06-Pt>d+^rgcj1 zf@j&6Bo+52Vjju6&h7GZulL^ZF3~&p6179IIbuBM4aY8OZvu20oo8z8;8jxrVJzfz zb~P_@d%~A_vKddxTTXCk%iMlka^5dgg@*usD@-L7fr`MEK%~~d@w$7+<9wFHtJ6 + + + + + + + + + Flask Auth Example + + + + +
+ +
+ +
+ +
+
+ {% block content %} + {% endblock %} +
+
+
+ + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..af298cb --- /dev/null +++ b/templates/index.html @@ -0,0 +1,12 @@ + + +{% extends "base.html" %} + +{% block content %} +

+ Flask Login Example +

+

+ Easy authentication and authorization in Flask. +

+{% endblock %} \ No newline at end of file diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..f78f4e7 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,38 @@ + + +{% 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/templates/profile.html b/templates/profile.html new file mode 100644 index 0000000..c2c49fd --- /dev/null +++ b/templates/profile.html @@ -0,0 +1,9 @@ + + +{% extends "base.html" %} + +{% block content %} +

+ Welcome, {{ name }}! +

+{% endblock %} \ No newline at end of file diff --git a/templates/signup.html b/templates/signup.html new file mode 100644 index 0000000..ce68a11 --- /dev/null +++ b/templates/signup.html @@ -0,0 +1,39 @@ + + +{% 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