commit 9e65eb55eb80961fb4f4d1d7e38fcf72fc4ad809 Author: ada <> Date: Mon Oct 23 13:03:55 2023 +0200 strictly files 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 0000000..6fa6094 Binary files /dev/null and b/db.sqlite differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..74ab797 --- /dev/null +++ b/main.py @@ -0,0 +1,15 @@ +# main.py + +from flask import Blueprint, render_template +from flask_login import login_required, current_user + +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', name=current_user.name) \ No newline at end of file diff --git a/models.py b/models.py new file mode 100644 index 0000000..0593b12 --- /dev/null +++ b/models.py @@ -0,0 +1,10 @@ +# models.py + +from flask_login import UserMixin +from . import db + +class User(UserMixin, db.Model): + id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy + email = db.Column(db.String(100), unique=True) + password = db.Column(db.String(100)) + name = db.Column(db.String(1000)) \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..032260a --- /dev/null +++ b/templates/base.html @@ -0,0 +1,59 @@ + + + + + + + + + + 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