Skip to content

A full-stack MERN habit tracking application with authentication and REST API.

Notifications You must be signed in to change notification settings

UltraInstinct21/fullstack-habit-tracker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

HabitTracker

A full-stack web application for tracking daily habits and building positive routines. Built with Express.js, MongoDB, and React.

πŸ“‹ Table of Contents

✨ Features

  • User Authentication: Secure registration and login with JWT tokens
  • Habit Management: Create, read, update, and delete habits
  • Habit Logging: Track habit completion with timestamps
  • User Dashboard: View all habits and their completion status
  • Protected Routes: Secure endpoints with JWT middleware

πŸ›  Tech Stack

Backend

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB
  • Authentication: JWT (JSON Web Tokens)
  • Password Hashing: bcrypt

Frontend

  • Framework: React 18
  • Build Tool: Vite
  • Styling: Tailwind CSS
  • UI Components: Chakra UI
  • State Management: React hooks

πŸ“‚ Project Structure

HabitTracker/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ config/              # Configuration files
β”‚   β”œβ”€β”€ controllers/         # Request handlers
β”‚   β”œβ”€β”€ middlewares/         # Express middlewares
β”‚   β”œβ”€β”€ models/             # MongoDB schemas
β”‚   β”œβ”€β”€ repositories/       # Data access layer
β”‚   β”œβ”€β”€ routes/             # API routes
β”‚   β”œβ”€β”€ services/           # Business logic
β”‚   β”œβ”€β”€ utils/              # Utility functions
β”‚   β”œβ”€β”€ server.js           # Main server file
β”‚   β”œβ”€β”€ package.json
β”‚   └── .env                # Environment variables
β”‚
β”œβ”€β”€ frontend/
β”‚   └── habbit-tracker/
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ pages/      # Page components
β”‚       β”‚   β”œβ”€β”€ components/ # Reusable components
β”‚       β”‚   β”œβ”€β”€ lib/        # Utility functions
β”‚       β”‚   β”œβ”€β”€ assets/     # Static assets
β”‚       β”‚   β”œβ”€β”€ App.jsx
β”‚       β”‚   └── main.jsx
β”‚       β”œβ”€β”€ package.json
β”‚       β”œβ”€β”€ vite.config.js
β”‚       β”œβ”€β”€ tailwind.config.js
β”‚       └── tsconfig.json
β”‚
└── README.md

πŸ“¦ Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn
  • MongoDB (running locally or via cloud)

πŸš€ Installation

Backend Setup

  1. Navigate to the backend directory:
cd backend
  1. Install dependencies:
npm install
  1. Install dotenv (if not already installed):
npm install dotenv

Frontend Setup

  1. Navigate to the frontend directory:
cd frontend/habbit-tracker
  1. Install dependencies:
npm install

βš™οΈ Configuration

Backend Environment Variables

Create a .env file in the backend/ directory with the following variables:

# MongoDB Connection
MONGODB_URI=mongodb://127.0.0.1:27017/test2

# JWT Secret Key
JWT_SECRET=your_secret_key_here

# Server Port
PORT=3000

Important: Replace your_secret_key_here with a strong, random secret key for production.

Frontend Configuration

The frontend is configured to connect to the backend at http://localhost:3000. Update the API base URL in the frontend components if your backend runs on a different port.

▢️ Running the Application

Start the Backend Server

cd backend

# Development mode with auto-reload
npm run dev

# Production mode
npm start

The backend server will run on http://localhost:3000

Start the Frontend Development Server

cd frontend/habbit-tracker

npm run dev

The frontend will run on http://localhost:5173

πŸ“‘ API Documentation

Authentication Endpoints

Register User

POST /auth/register
Content-Type: application/json

{
  "username": "john_doe",
  "email": "john@example.com",
  "password": "secure_password"
}

Login User

POST /auth/login
Content-Type: application/json

{
  "email": "john@example.com",
  "password": "secure_password"
}

Response:
{
  "token": "jwt_token",
  "user": { ... }
}

Habit Endpoints

All habit endpoints require authentication via JWT token in the Authorization header:

Authorization: Bearer <jwt_token>

Create Habit

POST /api/habits
{
  "title": "Morning Exercise",
  "description": "30 minutes of running"
}

Get All Habits

GET /api/habits

Get Habit by ID

GET /api/habits/:habitId

Update Habit

PUT /api/habits/:habitId
{
  "title": "Morning Yoga",
  "description": "1 hour yoga session"
}

Delete Habit

DELETE /api/habits/:habitId

Log Habit (Mark as completed)

POST /api/habits/:habitId/log
{
  "date": "2024-02-11"
}

🎨 Frontend Components

  • Navbar: Navigation component
  • HabitCard: Display individual habit cards
  • HabitForm: Form for creating/editing habits
  • TypeText: Text animation component
  • TargetCursor: Custom cursor component
  • And more UI components in ui/ folder

πŸ“ Database Models

User Model

  • username
  • email
  • password (hashed)
  • createdAt

Habit Model

  • userId (reference to User)
  • title
  • description
  • createdAt
  • updatedAt

Habit Log Model

  • habitId (reference to Habit)
  • userId (reference to User)
  • date
  • completed
  • createdAt

πŸ”’ Security Features

  • Password hashing with bcrypt
  • JWT token-based authentication
  • Protected routes with middleware
  • Environment variables for sensitive data
  • CORS configuration

Last Updated: February 2024

About

A full-stack MERN habit tracking application with authentication and REST API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published