Skip to content

CalebG6/Memora

Repository files navigation

Memora API

A secure, token-authenticated FastAPI backend for a flashcard application. This API allows users to register, log in, and perform full CRUD (Create, Read, Update, Delete) operations on their own flashcard sets and individual flashcards.

Features

  • User Authentication: Secure user registration and login using JWT (JSON Web Tokens).
  • Password Hashing: Passwords are securely hashed using bcrypt.
  • Protected Endpoints: API routes are protected, requiring a valid JWT for access.
  • CRUD for Flashcard Sets: Users can create, retrieve, update, and delete their own flashcard sets.
  • CRUD for Flashcards: Users can add and remove individual flashcards from their sets.
  • Ownership and Authorization: Users can only access and modify the flashcard sets they own.
  • MongoDB Integration: Uses MongoDB as the database to store user and flashcard data.
  • Pydantic Data Validation: Leverages Pydantic for robust data validation and serialization.
  • Modular Structure: The application is organized into logical modules (routers, models, security).

Baackend

Project Structure

.
├── .env
├── create_admin.py
├── main.py
├── backend_requirements.txt
├── database.py
├── models.py
├── security.py
└── routers
    ├── users.py
    ├── admin.py
    └── flashcards.py
  • main.py: The entry point for the FastAPI application. It initializes the app and includes the routers.
  • backend_requirements.txt: A list of all the Python dependencies required for the project.
  • database.py: Handles the connection to the MongoDB database and exposes collection objects.
  • models.py: Defines the Pydantic models for data shapes and validation (e.g., User, FlashcardSet).
  • security.py: Manages all security-related logic, including password hashing, JWT creation, and token decoding.
  • routers/users.py: Contains the API endpoints related to user management (/register, /token).
  • routers/flashcards.py: Contains all API endpoints for managing flashcard sets and flashcards.
  • .env: (You need to create this) Stores environment variables like database connection strings and security keys.

Setup and Installation

Prerequisites

  • Python 3.8+
  • A running MongoDB instance (either local or on a service like MongoDB Atlas)

1. Clone the Repository

git clone https://github.com/CalebG6/Memora
cd Memora

2. Create a Virtual Environment

It's highly recommended to use a virtual environment to manage project dependencies.

# For Unix/macOS
python3 -m venv venv
source venv/bin/activate

# For Windows
python -m venv venv
.\venv\Scripts\activate

3. Install Dependencies

Install all the required packages from the backend_requirements.txt file.

pip install -r backend_requirements.txt

4. Configure Environment Variables

Create a file named .env in the root directory of the project. This file will store your sensitive configuration details.

Important: Add .env to your .gitignore file to prevent committing secrets to version control.

Populate the .env file with the following content, replacing the placeholder values with your own:

# A strong, randomly generated secret key for JWT encoding.
# You can generate one using: openssl rand -hex 32
SECRET_KEY="your_very_strong_secret_key"

# The algorithm used for JWT encoding.
ALGORITHM="HS256"

# The MongoDB connection string.
MONGO_URL="mongodb://localhost:27017/" # Or your MongoDB Atlas connection string

# The duration in minutes for which an access token is valid.
ACCESS_TOKEN_EXPIRE_MINUTES=30

5. Run the Application

Once the dependencies are installed and the .env file is configured, you can start the development server using Uvicorn.

uvicorn main:app --reload

The --reload flag enables hot-reloading, which automatically restarts the server when you make changes to the code.

The API will now be running at http://127.0.0.1:8000.

API Endpoints

You can access the interactive API documentation (provided by Swagger UI) by navigating to http://127.0.0.1:8000/docs in your browser.


Users (/users)

POST /users/register

  • Description: Registers a new user.
  • Body:
    {
      "username": "newuser",
      "password": "strongpassword"
    }
  • Response: The newly created user object (without the password).

POST /users/token

  • Description: Authenticates a user and returns a JWT access token.
  • Body: application/x-www-form-urlencoded with username and password.
  • Response:
    {
      "access_token": "your_jwt_token",
      "token_type": "bearer"
    }

Flashcards (/flashcards)

Note: All flashcard endpoints are protected and require a valid JWT in the Authorization header (Authorization: Bearer <your_token>).

POST /flashcards/flashcard-sets

  • Description: Creates a new flashcard set for the authenticated user.
  • Response: The newly created flashcard set.

GET /flashcards/flashcard-sets

  • Description: Retrieves all flashcard sets owned by the authenticated user.
  • Response: A list of flashcard sets.

PUT /flashcards/flashcard-sets/{set_id}

  • Description: Updates an existing flashcard set. The user must be the owner.
  • Response: The updated flashcard set.

DELETE /flashcards/flashcard-sets/{set_id}

  • Description: Deletes a flashcard set. The user must be the owner.
  • Response: A success message.

POST /flashcards/flashcard-sets/{set_id}/flashcards

  • Description: Adds a new flashcard to a specific set. The user must be the owner.
  • Body:
    {
      "question": "What is FastAPI?",
      "answer": "A modern, fast web framework for building APIs."
    }
  • Response: The updated flashcard set with the new flashcard.

DELETE /flashcards/flashcard-sets/{set_id}/flashcards/{flashcard_index}

  • Description: Deletes a flashcard from a set by its index. The user must be the owner.
  • Response: The updated flashcard set with the flashcard removed.

Frontend

Features

  • Modern, responsive landing page
  • Login and sign-up pages
  • Ready to connect to Flask/FastAPI backend
  • MongoDB integration support
  • Form validation and error handling

Getting Started

  1. Install dependencies:
npm install
  1. Run the development server:
npm run dev
  1. Open http://localhost:3000 in your browser

Project Structure

├── app/                        # Next.js App Router
  │   ├── page.tsx                # Landing / home page
  │   ├── layout.tsx              # Root layout
  │   ├── globals.css             # Global styles
  │   ├── api/                    # API routes (app/api)
  │   │   └── study-sets/
  │   │       └── [id]/
  │   ├── dashboard/              # Auth'd dashboard pages
  │   │   └── page.tsx
  │   ├── login/
  │   │   └── page.tsx
  │   ├── signup/
  │   │   └── page.tsx
  │   └── study/
  │       └── [id]/
  │           └── edit/
  │               └── page.tsx
  ├── components/                 # Shared components
  │   ├── icons.tsx
  │   └── ui/                     # Small UI building blocks
  │       ├── avatar.tsx
  │       ├── badge.tsx
  │       ├── button.tsx
  │       ├── card.tsx
  │       ├── input.tsx
  │       ├── label.tsx
  │       └── progress.tsx
  ├── lib/
  │   └── utils.ts                # Utility helpers
  ├── data/
  ├── kitties/                    # image assets used in project
  │   └── kitty2.avif
  ├── public/
  │   └── kitties/
  └── package.json

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •