Skip to content

poetryofcode/SweetFree

Repository files navigation

SweetFree - Sugar Tracking Application

A modern, full-stack web application for tracking daily sugar intake, discovering healthier alternatives, and building healthier habits through community challenges and achievements.

Features

  • User Authentication: Secure signup/login with JWT tokens
  • Sugar Tracking: Log food items with barcode scanning capability
  • Onboarding Quiz: Personalized setup based on health goals
  • Dashboard: Real-time sugar intake tracking with progress visualization
  • Barcode Scanner: Scan product barcodes to quickly log items
  • Smart Swaps: Discover healthier alternatives to high-sugar foods
  • Community Challenges: Join challenges and compete with other users
  • Achievement Badges: Earn badges for reaching milestones
  • Community Feed: See other users' progress and celebrate together

Tech Stack

  • Frontend: Next.js 16 (App Router), React 19, TypeScript
  • Styling: Tailwind CSS v4, shadcn/ui components
  • Backend: Next.js API Routes
  • Database: PostgreSQL (Neon)
  • Authentication: JWT-based auth with localStorage persistence
  • Deployment: Vercel

Getting Started

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Neon PostgreSQL database
  • Vercel account (for deployment)

Installation

  1. Clone the repository: ```bash git clone cd sweetfree ```

  2. Install dependencies: ```bash npm install ```

  3. Set up environment variables in your Vercel project:

  • DATABASE_URL - Your Neon PostgreSQL connection string
  • JWT_SECRET - Secret key for JWT signing (use a strong random string)
  1. Run database migration to set up tables: ```bash npm run db:migrate ```

  2. Start development server: ```bash npm run dev ```

  3. Open http://localhost:3000

Database Setup

The database schema includes tables for:

  • users - User accounts and authentication
  • entries - Daily food logs with sugar content
  • plans - User preferences and targets
  • challenges - Community challenges
  • challenge_members - User participation in challenges
  • badges - Achievement badges
  • user_badges - User's earned badges

Run the migration script to initialize: ```bash npm run db:migrate ```

Project Structure

``` ├── app/ │ ├── api/ # API routes │ │ ├── auth/ # Authentication endpoints │ │ ├── entries/ # Food entry endpoints │ │ ├── swaps/ # Sugar swaps endpoints │ │ ├── challenges/ # Challenge endpoints │ │ └── user-badges/ # Badge endpoints │ ├── (pages)/ # Application pages │ │ ├── page.tsx # Landing page │ │ ├── signup/ # Sign up │ │ ├── login/ # Sign in │ │ ├── onboarding/ # Onboarding quiz │ │ ├── dashboard/ # Main dashboard │ │ ├── profile/ # User profile │ │ └── swaps/ # Swaps & challenges │ └── layout.tsx # Root layout ├── components/ │ ├── ui/ # shadcn/ui components │ ├── auth-provider.tsx # Auth context provider │ ├── protected-route.tsx # Route protection wrapper │ └── ... # Feature components ├── lib/ │ ├── auth-context.ts # Auth context definition │ ├── jwt.ts # JWT utilities │ ├── password.ts # Password hashing │ └── utils.ts # Utility functions ├── scripts/ │ ├── 01-init-schema.sql # Database schema │ ├── 02-seed-data.sql # Initial seed data │ └── ... # Migration scripts └── public/ # Static assets ```

API Endpoints

Authentication

  • POST /api/auth/signup - Create new user account
  • POST /api/auth/login - User login
  • POST /api/auth/verify - Verify JWT token

Entries

  • POST /api/entries/create - Log a food entry
  • GET /api/entries/list - Get user's entries
  • GET /api/entries/daily-summary - Get daily stats

Swaps & Challenges

  • GET /api/swaps/list - Get available sugar swaps
  • GET /api/challenges/list - Get active challenges
  • POST /api/challenges/join - Join a challenge

User

  • GET /api/user-badges/list - Get user's earned badges
  • GET /api/community-feed - Get community activity

Deployment

Deploy to Vercel

  1. Push your code to GitHub: ```bash git push origin main ```

  2. Import project in Vercel dashboard

  3. Add environment variables:

    • DATABASE_URL
    • JWT_SECRET
  4. Deploy

The app will be live at https://your-project.vercel.app

Development

Running Scripts

```bash

Run database migration

npm run db:migrate

Seed sample data

npm run db:seed

Development server

npm run dev

Build for production

npm run build

Start production server

npm start ```

Adding New Features

  1. Create API routes in app/api/
  2. Add components in components/
  3. Create pages in app/
  4. Protect routes using ProtectedRoute component

Authentication Flow

  1. User signs up with email/password
  2. Password is hashed with SHA-256
  3. JWT token generated and stored in localStorage
  4. Token included in Authorization header for API requests
  5. Token verified server-side using JWT_SECRET
  6. Auto-redirect to login if token invalid/expired

Performance Optimizations

  • React Compiler enabled for automatic memoization
  • Next.js Image Optimization
  • CSS-in-JS with Tailwind CSS v4
  • Component-level caching
  • Efficient database queries with indexes
  • Token-based stateless authentication

Security

  • JWT-based authentication with 7-day expiration
  • Password hashing with SHA-256
  • CORS headers for API protection
  • SQL parameterized queries to prevent injection
  • Environment variables for secrets
  • HttpOnly cookies recommended for production

Browser Support

  • Chrome/Edge 90+
  • Firefox 88+
  • Safari 14+
  • Mobile browsers (iOS Safari, Chrome Mobile)

License

MIT License - see LICENSE file for details

Support

For issues or questions:

  1. Check the GitHub issues
  2. Review API documentation
  3. Check deployment logs on Vercel

Contributing

Pull requests welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

Future Enhancements

  • OAuth integration (Google, Apple)
  • Mobile app (React Native)
  • Advanced analytics dashboard
  • Nutrition tracking beyond sugar
  • Integration with fitness apps
  • AI-powered meal recommendations
  • Recipe generation
  • Export data features ```

```json file="" isHidden

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published