Quiz 2

Learning Objectives

293 words
1 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Learning Objectives - Implement JWT-based authentication - Create login/register components - Protect routes with auth middleware - Flask backend running - User model created - JWT libraries installed ## Step 1: Auth Backend ## Step 2: Auth Frontend Components - Register a user -> verify token returned - Login ->...

Learning Objectives

  • Implement JWT-based authentication
  • Create login/register components
  • Protect routes with auth middleware
  • Flask backend running
  • User model created
  • JWT libraries installed

Step 1: Auth Backend

python
# backend/auth.py
from flask import Blueprint, request, jsonify
from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity
from werkzeug.security import generate_password_hash, check_password_hash
from models import db, User
auth = Blueprint('auth', __name__)
@auth.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    if User.query.filter_by(username=data['username']).first():
        return {'msg': 'Username exists'}, 400
    user = User(
        username=data['username'],
        email=data['email'],
        password_hash=generate_password_hash(data['password'])
    )
    db.session.add(user)
    db.session.commit()
    token = create_access_token(identity=user.id)
    return {'token': token, 'user': user.to_dict()}, 201
@auth.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    user = User.query.filter_by(username=data['username']).first()
    if user and check_password_hash(user.password_hash, data['password']):
        token = create_access_token(identity=user.id)
        return {'token': token, 'user': user.to_dict()}
    return {'msg': 'Invalid credentials'}, 401

Step 2: Auth Frontend Components

jsx
// frontend/src/components/Login.js
import React, { useState } from 'react';
import axios from 'axios';
function Login() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');
  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      const res = await axios.post('/api/login', { username, password });
      localStorage.setItem('token', res.data.token);
      localStorage.setItem('user', JSON.stringify(res.data.user));
      window.location.href = '/dashboard';
    } catch (err) {
      alert('Login failed');
    }
  };
  return (
    <form onSubmit={handleSubmit}>
      <input value={username} onChange={e => setUsername(e.target.value)} />
      <input type="password" value={password} onChange={e => setPassword(e.target.value)} />
      <button type="submit">Login</button>
    </form>
  );
}
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.