diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3d72e4e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,56 @@ +# Dependencies +node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* +# Note: package-lock.json is needed for npm ci in Docker + +# Environment variables +.env +.env.local +.env.*.local + +# Logs +logs/ +*.log + +# Testing +coverage/ +.nyc_output/ + +# IDE and editor files +.vscode/ +.idea/ +*.swp +*.swo +*~ +.DS_Store + +# Git +.git/ +.gitignore +.gitattributes + +# CI/CD +.github/ +.gitlab-ci.yml + +# Docker +Dockerfile +docker-compose.yml +.dockerignore + +# Documentation +README.md +CHANGELOG.md +LICENSE +CODE_OF_CONDUCT.md + +# Misc +.husky/ +.commitlintrc* +.eslintrc* +.prettierrc* +eslint.config.js +commitlint.config.js +*.md \ No newline at end of file diff --git a/.env.example b/.env.example index 6d413b9..a00570b 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -MONGO_URI= -JWT_SECRET= +MONGO_URI=mongodb+srv://mukeshdhadhariya1_db_user:sKk4J4Rsmt1MzOEL@cluster0.fc8cebb.mongodb.net/ +JWT_SECRET=dsfgh NODE_ENV= -PORT=mongodb+srv://mukeshdhadhariya1_db_user:sKk4J4Rsmt1MzOEL@cluster0.fc8cebb.mongodb.net/ \ No newline at end of file +PORT=3001 \ No newline at end of file diff --git a/Docker-commands-guide.md b/Docker-commands-guide.md new file mode 100644 index 0000000..9243661 --- /dev/null +++ b/Docker-commands-guide.md @@ -0,0 +1,171 @@ +# Docker Commands Guide + +This guide provides step-by-step instructions for building images, running containers, stopping them, viewing logs, and managing Docker resources. + +--- + +## 🚀 1. Build Docker Images + +Navigate to the folder containing your `Dockerfile` and `docker-compose.yml`. + +### Using Docker Compose (Recommended) + +```bash +docker-compose build +``` + +This builds two images: + +* `smart-energy-frontend` +* `smart-energy-backend` + +--- + +## 🚀 2. Start Containers + +```bash +docker-compose up +``` + +Run in background: + +```bash +docker-compose up -d +``` + +Access: + +* Frontend → [http://localhost:5173](http://localhost:5173) +* Backend → [http://localhost:5000](http://localhost:5000) + +--- + +## 🚀 3. Stop Containers + +```bash +docker-compose down +``` + +This stops and removes containers (not images). + +--- + +## 🚀 4. View Running Containers + +```bash +docker ps +``` + +View all containers (running + stopped): + +```bash +docker ps -a +``` + +--- + +## 🚀 5. View Images + +```bash +docker images +``` + +--- + +## 🚀 6. Run a Single Service + +Run only frontend: + +```bash +docker-compose up frontend +``` + +Run only backend: + +```bash +docker-compose up backend +``` + +--- + +## 🚀 7. Build Image Manually (Without Compose) + +```bash +docker build -t smart-energy-image . +``` + +--- + +## 🚀 8. Run Image Manually + +Expose port and run: + +```bash +docker run -p 5173:5173 smart-energy-image +``` + +--- + +## 🚀 9. Stop a Running Container + +Find container ID: + +```bash +docker ps +``` + +Then stop: + +```bash +docker stop +``` + +--- + +## 🚀 10. Remove a Container + +```bash +docker rm +``` + +--- + +## 🚀 11. Remove an Image + +```bash +docker rmi +``` + +--- + +## 🚀 12. Restart Containers + +```bash +docker-compose restart +``` + +--- + +## 🚀 13. View Logs + +Frontend logs: + +```bash +docker-compose logs frontend +``` + +Backend logs: + +```bash +docker-compose logs backend +``` + +Follow logs live: + +```bash +docker-compose logs -f +``` + +--- + +This `.md` file gives you everything needed to work with Docker images and containers efficiently. \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..48666a7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Simple Node.js dev container +FROM node:22-alpine + +WORKDIR /app + +# Copy only package.json first for caching +COPY package*.json ./ + +# Install dependencies +RUN npm install + +# Copy full project +COPY . . + +# Expose BOTH ports (frontend + backend) +EXPOSE 5173 +EXPOSE 3001 + +# Default CMD (overwritten in docker-compose) +CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"] \ No newline at end of file diff --git a/README.Docker.md b/README.Docker.md new file mode 100644 index 0000000..4c630ec --- /dev/null +++ b/README.Docker.md @@ -0,0 +1,58 @@ +# Docker Setup for Smart Energy Tracker (Development) + +This project uses Docker to run **frontend** and **backend** separately, +each inside its own container, without using nginx or a production build. + +--- + +## Files Included + +### 1. Dockerfile +A single minimal Node.js image used for **both frontend and backend**. + +- Installs dependencies +- Copies project files +- Exposes ports 5173 (frontend) and 5000 (backend) +- The command is overridden by docker-compose + +### 2. docker-compose.yml +Handles running two separate services: + +#### `frontend` service +- Runs `npm run dev` +- Exposes port **5173** +- Supports live reload using file volume mounts + +#### `backend` service +- Runs `npm start` +- Exposes port **5000** +- Also uses volume mounts for hot reload + +### Why Docker Compose? +Because we need: + +- Two containers (frontend + backend) +- Each running different commands +- Shared code with hot reload +- Easy `up`, `down`, and log management + +--- + +## How to Run + +```bash +docker-compose up --build +``` + +--- + +## runing url +- Frontend → http://localhost:5173 +- Backend → http://localhost:5000 + +--- + +## How to stop +```bash +docker-compose down +``` \ No newline at end of file diff --git a/README.md b/README.md index 6b948cf..28c56a4 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,21 @@ Quality checks - Quick Setup & Run (point-wise commands) +## Environment Setup (.env configuration) +Before running the project, create an environment file. +1. Create a `.env` file in the root directory. +2. Copy the content from `.env.example` and paste it into the `.env` file. +```bash +cp .env.example .env +``` + +## Demo Login Credentials + +Use the following demo account to log in: + +- **Email:** `a@gmail.com` +- **Password:** `123` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..27d3197 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: "3.8" + +services: + + backend: + build: . + container_name: smart-energy-backend + command: npm start + ports: + - "3001:3001" + environment: + MONGO_URI: "mongodb+srv://mukeshdhadhariya1_db_user:sKk4J4Rsmt1MzOEL@cluster0.fc8cebb.mongodb.net/" + JWT_SECRET: "dsfgh" + NODE_ENV: "development" + PORT: "3001" + volumes: + - .:/app + - /app/node_modules + + frontend: + build: . + container_name: smart-energy-frontend + command: npm run dev + ports: + - "5173:5173" + volumes: + - .:/app + - /app/node_modules + environment: + - CHOKIDAR_USEPOLLING=true + depends_on: + - backend diff --git a/src/context/SocketContext.jsx b/src/context/SocketContext.jsx index 45d38f5..de997db 100644 --- a/src/context/SocketContext.jsx +++ b/src/context/SocketContext.jsx @@ -15,7 +15,7 @@ export function SocketProvider({ children }) { const isAuthenticated = !!user; useEffect(() => { - + console.log(user) if (!isAuthenticated) { if (socket) { socket.disconnect(); @@ -130,7 +130,7 @@ export function SocketProvider({ children }) { setSocket(newSocket); return () => newSocket.close(); - }, [dispatch]); + }, [dispatch,user]); const value = { socket, diff --git a/src/main.jsx b/src/main.jsx index b9a1a6d..2d684c7 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -4,7 +4,8 @@ import './index.css' import App from './App.jsx' createRoot(document.getElementById('root')).render( - - - , + // + // + // , + ) diff --git a/src/pages/Login.jsx b/src/pages/Login.jsx index b3d331c..9b5d0e9 100644 --- a/src/pages/Login.jsx +++ b/src/pages/Login.jsx @@ -9,7 +9,7 @@ const Login = () => { const [loading, setLoading] = useState(false); const navigate = useNavigate(); const { state } = useApp(); - const { loginUser } = useAuth(); // 👈 get loginUser from context + const { loginUser } = useAuth(); const handleChange = (e) => { setCredentials({ ...credentials, [e.target.name]: e.target.value }); @@ -22,19 +22,18 @@ const Login = () => { const response = await fetch("http://localhost:3001/api/login", { method: "POST", headers: { "Content-Type": "application/json" }, - credentials: "include", // ✅ send/receive cookies + credentials: "include", body: JSON.stringify(credentials), }); - console.log("click1") + const data = await response.json(); - console.log("click1") + if (response.ok) { - // ✅ Save user data to AuthContext and localStorage + loginUser(data.user || { email: credentials.email }); toast.success("Login successful! 🔋", { autoClose: 2000 }); - console.log("click1") - // ✅ Redirect to dashboard + setTimeout(() => navigate("/dashboard"), 1000); } else { toast.error(data.message || "Invalid email or password."); diff --git a/vite.config.js b/vite.config.js index ea4efea..6e3d6b2 100644 --- a/vite.config.js +++ b/vite.config.js @@ -7,4 +7,8 @@ export default defineConfig({ plugins: [react(), tailwindcss(), ], -}) + server: { + host: true, + port: 5173 + } +}) \ No newline at end of file