Skip to content

Repository files navigation

Co-App - Nearby Chat Platform

A production-ready Next.js application for location-based chat with AWS Cognito authentication and S3 image storage, deployed on AWS ECS.

Features

  • AWS Cognito Authentication: Complete auth flow with login, register, email verification, and password reset
  • Guest Mode: Allow non-registered users to browse with limited features
  • S3 Image Storage: Secure image upload and storage using AWS S3
  • Location-based Chat: Connect with people nearby (similar to nearby-chat platforms)
  • Real-time Messaging: Chat functionality with nearby users
  • Responsive Design: Beautiful, production-ready UI with Tailwind CSS and shadcn/ui
  • AWS ECS Deployment: Scalable containerized deployment

Architecture

  • Frontend: Next.js 13+ with TypeScript and Tailwind CSS
  • Authentication: AWS Cognito User Pools
  • File Storage: AWS S3 for images
  • Database: PostgreSQL on AWS RDS
  • Container: Docker with multi-stage builds
  • Orchestration: AWS ECS with Fargate
  • Load Balancer: AWS Application Load Balancer
  • CI/CD: GitLab CI/CD pipeline
  • Container Registry: AWS ECR

Prerequisites

  • AWS Account with appropriate permissions
  • GitLab account for CI/CD
  • Docker installed locally
  • AWS CLI configured
  • Node.js 18+ installed

AWS Services Setup

1. AWS Cognito User Pool

Create a Cognito User Pool with the following settings:

  • Sign-in options: Email
  • Password policy: Minimum 8 characters, require uppercase, lowercase, numbers, and special characters
  • MFA: Optional (recommended for production)
  • Email verification: Required
  • App client: Create an app client without client secret

2. AWS S3 Bucket

Create an S3 bucket for image storage:

aws s3 mb s3://co-app-images-bucket --region us-east-1

Configure bucket policy for public read access to uploaded images:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::co-app-images-bucket/*"
    }
  ]
}

3. IAM Roles and Policies

Create IAM roles with appropriate permissions:

  • ECS Task Role: S3 access for image uploads
  • ECS Execution Role: ECR and CloudWatch access

Local Development

  1. Clone and install dependencies:

    npm install
  2. Set up environment variables:

    cp .env.example .env.local
    # Edit .env.local with your AWS configuration
  3. Configure AWS services:

    • Set up Cognito User Pool and get the Pool ID and Client ID
    • Create S3 bucket and configure permissions
    • Update environment variables with your AWS resource IDs
  4. Run development server:

    npm run dev

Environment Variables

Required for Development and Production

# AWS Cognito
NEXT_PUBLIC_COGNITO_USER_POOL_ID=us-east-1_XXXXXXXXX
NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID=XXXXXXXXXXXXXXXXXXXXXXXXXX
NEXT_PUBLIC_AWS_REGION=us-east-1

# AWS S3
NEXT_PUBLIC_S3_BUCKET_NAME=co-app-images-bucket
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key

# Database
DATABASE_URL=postgresql://username:password@your-rds-endpoint:5432/co_app_db

# Application
NODE_ENV=production
NEXT_PUBLIC_API_URL=https://your-domain.com

GitLab CI/CD Setup

Required GitLab Variables

Set these variables in your GitLab project settings:

  • AWS_ACCESS_KEY_ID: AWS access key with ECR and ECS permissions
  • AWS_SECRET_ACCESS_KEY: AWS secret key
  • AWS_ACCOUNT_ID: Your AWS account ID
  • DATABASE_URL: PostgreSQL connection string

Pipeline Stages

  1. Build: Installs dependencies, builds Docker image, pushes to ECR
  2. Deploy: Forces new deployment on ECS service

Key Features Implementation

Authentication Flow

  • Login/Register: Full Cognito integration with email verification
  • Password Reset: Secure password reset with email codes
  • Guest Mode: Browse public content without registration
  • Session Management: Automatic token refresh and session handling

Image Upload

  • S3 Integration: Direct upload to S3 with presigned URLs
  • File Validation: Type and size validation
  • Preview: Real-time image preview before upload
  • Error Handling: Comprehensive error handling and user feedback

Guest Mode Features

  • Browse public conversations
  • View nearby activity (limited)
  • No posting or direct messaging
  • Upgrade prompts to encourage registration

Security Considerations

  • Cognito Security: MFA support, secure password policies
  • S3 Security: Proper bucket policies and IAM roles
  • Environment Variables: Secure handling of sensitive data
  • HTTPS: SSL/TLS encryption for all communications
  • Input Validation: Client and server-side validation

Deployment

Automatic Deployment

Push to main branch triggers automatic build and deployment.

Manual Deployment

chmod +x scripts/deploy.sh
./scripts/deploy.sh

Monitoring and Troubleshooting

  • Health Checks: /api/health endpoint for monitoring
  • CloudWatch Logs: Centralized logging for debugging
  • ECS Service Monitoring: Auto-scaling and health monitoring
  • Error Tracking: Comprehensive error handling and logging

Scaling Considerations

  • ECS Auto Scaling: Configure based on CPU/memory usage
  • RDS Scaling: Read replicas for database scaling
  • S3 Performance: Optimize for high-throughput image uploads
  • Cognito Limits: Monitor authentication rate limits

Cost Optimization

  • Fargate Spot: Use for non-production environments
  • S3 Lifecycle: Configure lifecycle policies for old images
  • RDS Reserved Instances: For predictable workloads
  • CloudWatch Monitoring: Track and optimize resource usage

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages