diff --git a/.DS_Store b/.DS_Store index 10ae569..6d84353 100644 Binary files a/.DS_Store and b/.DS_Store differ 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' + + +
+ + +You are logged in.
+This is your dashboard.
+{% else %} + +{% endif %} +{% endblock %} +HTML2EOF + +cat > templates/register.html << 'HTML3EOF' +{% extends "base.html" %} + +{% block title %}Register{% endblock %} + +{% block content %} +Already have an account? Login here
+{% endblock %} +HTML3EOF + +cat > templates/login.html << 'HTML4EOF' +{% extends "base.html" %} + +{% block title %}Login{% endblock %} + +{% block content %} +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