From 26811fd766ac50a57ab8b30488f2f57371c275c5 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 27 Jan 2026 12:58:25 +0300 Subject: [PATCH] Added g11-m3-4 --- .DS_Store | Bin 6148 -> 8196 bytes ...ocial Media Feed with Vue.js and APIs.html | 0 .../Use this code.vue | 0 g11-m3-4/README.md | 289 ++++++++++++++++++ 4 files changed, 289 insertions(+) rename 3_Creating a Social Media Feed with Vue.js and APIs.html => g11-m3-2/3_Creating a Social Media Feed with Vue.js and APIs.html (100%) rename Use this code.vue => g11-m3-2/Use this code.vue (100%) create mode 100644 g11-m3-4/README.md diff --git a/.DS_Store b/.DS_Store index 10ae569063236a175e204a5c1a5736b1582d51b7..6d843538ed215f2c5ba7e2ba9e1e2cc456749a65 100644 GIT binary patch delta 711 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aKK$_FxOEqG%}dH zhDC<4Yw}4JdC_icS|=;9nyE@jOT`NaCnY9lrx)dy=A}62=j0bT<&>sIrx#_W1Q#SG zXXd3(Ze~5F5--4CoRMGdnVg>&P?TDhnOd%F{{d(&5GXr%Ll~3O+0?8#fv(hLaAxpl zhyc2>hG8L*(UH-M(T6dVF^n;SF_tloF@5p{ zHsyK@Kjx$x1}Ep|7C`I(lNf4p^Icq^;lSarP2_dLl;e&-l~~lJ5Kw0ZiGzX+f|iB<$99Dzyh bJM(0I5zoo~JRBUr6bVYH44dP5<}d>Q`*M;E delta 374 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50D9Q|y2aA<5q%!C-Br+5*6l^S9 z&a4WO;bO>Ts050ZF(An%l@}Kz<>V&;<#$f56E5VItgbdPFw{{nG%}q0Q&@(vd$O2_ zyj2e_{VB!CIZ65XIY4>r7EJyjV8$#gEi>6rP)sOZfWJ5+zuYr9KQEvtwJbBWd~&eh zIkD*UqRf=wg2d#^ymV#z4}|6H?w-BEqa_44dP5<}d>Qc_d5` diff --git a/3_Creating a Social Media Feed with Vue.js and APIs.html b/g11-m3-2/3_Creating a Social Media Feed with Vue.js and APIs.html similarity index 100% rename from 3_Creating a Social Media Feed with Vue.js and APIs.html rename to g11-m3-2/3_Creating a Social Media Feed with Vue.js and APIs.html diff --git a/Use this code.vue b/g11-m3-2/Use this code.vue similarity index 100% rename from Use this code.vue rename to g11-m3-2/Use this code.vue diff --git a/g11-m3-4/README.md b/g11-m3-4/README.md new file mode 100644 index 0000000..3520d3e --- /dev/null +++ b/g11-m3-4/README.md @@ -0,0 +1,289 @@ +# Flask Authentication Application + +A complete full-stack web application with user authentication built with Flask and SQLite. + +## 📁 Project Structure +flask_app/ +├── app.py # Main Flask application +├── database.py # Database setup and functions +├── users.py # User authentication functions +├── users.db # SQLite database +├── requirements.txt # Python dependencies +├── venv/ # Virtual environment +└── templates/ # HTML templates +├── base.html +├── home.html +├── login.html +└── register.html + +text + +## 🚀 Quick Start + +### 1. Create Project Structure + +```bash +cd Desktop +mkdir flask_app +cd flask_app + +# Create all project files at once +cat > app.py << 'PYEOF' +from flask import Flask, render_template, request, redirect, url_for, session, flash +import database +import users + +app = Flask(__name__) +app.secret_key = 'your-secret-key-change-this-in-production' + +database.init_db() + +@app.route('/') +def home(): + return render_template('home.html') + +@app.route('/register', methods=['GET', 'POST']) +def register(): + if request.method == 'POST': + username = request.form['username'] + email = request.form['email'] + password = request.form['password'] + + success, message = users.register_user(username, email, password) + + if success: + flash('Registration successful! Please login.', 'success') + return redirect(url_for('login')) + else: + flash(message, 'error') + + return render_template('register.html') + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] + + success, result = users.login_user(username, password) + + if success: + session['user_id'] = result['id'] + session['username'] = result['username'] + flash('Login successful!', 'success') + return redirect(url_for('home')) + else: + flash(result, 'error') + + return render_template('login.html') + +@app.route('/logout') +def logout(): + session.clear() + flash('You have been logged out.', 'success') + return redirect(url_for('home')) + +if __name__ == '__main__': + app.run(debug=True) +PYEOF + +cat > database.py << 'DBEOF' +import sqlite3 + +def init_db(): + conn = sqlite3.connect('users.db') + cursor = conn.cursor() + + cursor.execute(''' + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE NOT NULL, + email TEXT UNIQUE NOT NULL, + password TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ) + ''') + + conn.commit() + conn.close() + +def get_db_connection(): + conn = sqlite3.connect('users.db') + conn.row_factory = sqlite3.Row + return conn +DBEOF + +cat > users.py << 'UEOF' +from werkzeug.security import generate_password_hash, check_password_hash +import database + +def register_user(username, email, password): + hashed_password = generate_password_hash(password, method='sha256') + + try: + conn = database.get_db_connection() + conn.execute('INSERT INTO users (username, email, password) VALUES (?, ?, ?)', + (username, email, hashed_password)) + conn.commit() + conn.close() + return True, None + except sqlite3.IntegrityError as e: + return False, 'Username or email already exists!' + +def login_user(username, password): + conn = database.get_db_connection() + user = conn.execute('SELECT * FROM users WHERE username = ?', (username,)).fetchone() + conn.close() + + if user and check_password_hash(user['password'], password): + return True, user + else: + return False, 'Invalid username or password!' +UEOF + +# Create templates folder +mkdir templates + +cat > templates/base.html << 'HTML1EOF' + + + + + + {% block title %}Flask Auth App{% endblock %} + + + + + + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} +
{{ message }}
+ {% endfor %} + {% endif %} + {% endwith %} + + {% block content %}{% endblock %} + + +HTML1EOF + +cat > templates/home.html << 'HTML2EOF' +{% extends "base.html" %} + +{% block title %}Home{% endblock %} + +{% block content %} +

Welcome to Flask Auth App

+{% if session.get('user_id') %} +

Hello, {{ session.get('username') }}!

+

You are logged in.

+

This is your dashboard.

+{% else %} +

Please login or register.

+{% endif %} +{% endblock %} +HTML2EOF + +cat > templates/register.html << 'HTML3EOF' +{% extends "base.html" %} + +{% block title %}Register{% endblock %} + +{% block content %} +

Register

+
+ + + + +
+

Already have an account? Login here

+{% endblock %} +HTML3EOF + +cat > templates/login.html << 'HTML4EOF' +{% extends "base.html" %} + +{% block title %}Login{% endblock %} + +{% block content %} +

Login

+
+ + + +
+

Don't have an account? Register here

+{% endblock %} +HTML4EOF +2. Create Virtual Environment +bash +python -m venv venv +venv\Scripts\Activate.ps1 +If you get an error: + +bash +Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser +venv\Scripts\Activate.ps1 +3. Install Dependencies +bash +pip install flask werkzeug flask-wtf +pip freeze > requirements.txt +4. Run the Application +bash +python app.py +Visit: http://127.0.0.1:5000 + +📋 File Purpose +File Purpose Contains +app.py Main Flask application Routes, request handling, session management +database.py Database operations Connection functions, table creation, SQL queries +users.py User authentication Register, login, password hashing functions +templates/ HTML templates Base template and page-specific templates +requirements.txt Dependencies List of Python packages needed +users.db Database file SQLite database with user data +🔒 Security Notes +This is a learning example, not production-ready + +Passwords are hashed using Werkzeug + +Never store plain text passwords + +Use stronger secret keys in production + +🚀 Next Steps +Add password reset functionality + +Implement email verification + +Add user profile pages + +Create a simple blog system + +Deploy to Heroku or Render + +📚 Resources +Flask Documentation + +SQLite Documentation + +Jinja2 Template Documentation