diff --git a/__pycache__/app.cpython-312.pyc b/__pycache__/app.cpython-312.pyc new file mode 100644 index 0000000..4220d63 Binary files /dev/null and b/__pycache__/app.cpython-312.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000..36e6da2 --- /dev/null +++ b/app.py @@ -0,0 +1,67 @@ +from flask import Flask, render_template, redirect, url_for, request +from flask_sqlalchemy import SQLAlchemy +from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user +from werkzeug.security import generate_password_hash, check_password_hash + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'your-secret-key-here' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////Users/leoan/Developer/Personal_Projects/web-login/instance/users.db' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +db = SQLAlchemy(app) +login_manager = LoginManager(app) +login_manager.login_view = 'login' + +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)) + +@login_manager.user_loader +def load_user(user_id): + return User.query.get(int(user_id)) + +@app.route('/') +def home(): + return redirect(url_for('login')) + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + email = request.form.get('email') + password = request.form.get('password') + user = User.query.filter_by(email=email).first() + + if user and check_password_hash(user.password, password): + login_user(user) + return redirect(url_for('dashboard')) + + return 'Invalid credentials' + return render_template('login.html') + +@app.route('/register', methods=['GET', 'POST']) +def register(): + if request.method == 'POST': + email = request.form.get('email') + password = generate_password_hash(request.form.get('password')) + + if User.query.filter_by(email=email).first(): + return 'Email already registered' + + new_user = User(email=email, password=password) + db.session.add(new_user) + db.session.commit() + + return redirect(url_for('login')) + return render_template('register.html') + +@app.route('/dashboard') +@login_required +def dashboard(): + return render_template('dashboard.html') + +@app.route('/logout') +@login_required +def logout(): + logout_user() + return redirect(url_for('login')) diff --git a/instance/users.db b/instance/users.db new file mode 100644 index 0000000..716fe6e Binary files /dev/null and b/instance/users.db differ diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..e51a359 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,26 @@ + + + + + + {% block title %}{% endblock %} + + + + + {% block content %}{% endblock %} + + + diff --git a/templates/dashboard.html b/templates/dashboard.html new file mode 100644 index 0000000..bb7871c --- /dev/null +++ b/templates/dashboard.html @@ -0,0 +1,34 @@ +{% extends "base.html" %} + +{% block title %}Dashboard{% endblock %} + +{% block content %} +
+
+
+

Dashboard

+ + Logout + +
+
+
+ Welcome back, {{ current_user.email }}! +
+
+
Account Details
+
    +
  • + Registered Email: + {{ current_user.email }} +
  • +
  • + Account Created: + Just now +
  • +
+
+
+
+
+{% endblock %} diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..8c18d12 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,29 @@ +{% extends "base.html" %} + +{% block title %}Login{% endblock %} + +{% block content %} +
+
+
+
+

Sign In

+
+
+ + +
+
+ + +
+ +
+
+ Don't have an account? Register here +
+
+
+
+
+{% endblock %} diff --git a/templates/register.html b/templates/register.html new file mode 100644 index 0000000..67bda33 --- /dev/null +++ b/templates/register.html @@ -0,0 +1,29 @@ +{% extends "base.html" %} + +{% block title %}Register{% endblock %} + +{% block content %} +
+
+
+
+

Create Account

+
+
+ + +
+
+ + +
+ +
+
+ Already have an account? Login here +
+
+
+
+
+{% endblock %}