A full‑stack AI chatbot inspired by ChatGPT. Users can sign up, log in, chat with an AI model (OpenAI), and have their messages securely stored and managed in MongoDB. Authentication uses signed, HTTP‑only cookies with JWTs.
- Backend: Node.js, Express, TypeScript, MongoDB (Mongoose)
- Frontend: React, TypeScript, Vite, MUI
- AI: OpenAI Chat Completions (gpt-3.5-turbo)
- JWT auth with signed HTTP‑only cookies (secure by default)
- User signup/login/logout & auth status
- Persisted chat history per user (view and delete)
- OpenAI chat completion integration
- Request validation with express-validator
- Structured controllers/routes with TypeScript
AI-SaaS-Chat-Bot/
backend/ # Express + TS API server
src/
app.ts
index.ts
config/openai-config.ts
controllers/
db/connection.ts
models/User.ts
routes/
utils/
package.json
frontend/ # React + Vite web app
src/
main.tsx # axios baseURL and withCredentials
...
package.json
README.md # You are here
- Node.js 18+ and npm
- A MongoDB connection string
- OpenAI API key (and optionally Organization ID)
- Backend
- Copy environment variables and fill your values:
# from project root
Copy-Item backend/.env.example backend/.env- Install deps and start the API server:
cd backend
npm install
npm run devBy default the API listens on http://localhost:5000.
- Frontend
In a new terminal:
cd frontend
npm install
npm run devOpen http://localhost:5173 in your browser.
In backend/src/app.ts the CORS origin is currently set to https://ai-mern-chatbot.netlify.app. For local development you should change it to your Vite URL (default http://localhost:5173) or to an array of allowed origins.
Example change in app.ts:
app.use(
cors({ origin: "http://localhost:5173", credentials: true })
);Alternatively, you can refactor it to read from an environment variable (e.g., FRONTEND_ORIGIN).
Also ensure the frontend sends cookies: in frontend/src/main.tsx we already set axios.defaults.withCredentials = true.
Create backend/.env with:
# Server
PORT=5000
COOKIE_SECRET=replace-with-random-long-string
JWT_SECRET=replace-with-strong-secret
# Database
MONGODB_URL=mongodb+srv://<user>:<pass>@cluster0.xxx.mongodb.net/your-db
# OpenAI
OPEN_AI_SECRET=sk-...
# Optional, only if your account uses an organization
OPEN_AI_ORGANIZATION=org_...
Notes:
COOKIE_SECRETsigns cookies for tamper-proofing.JWT_SECRETsigns your JWTs stored in the cookie namedauth_token.MONGODB_URLmust be reachable from your machine (IP allowlist if using Atlas).
Backend (from backend/):
npm run dev— TypeScript watch + nodemon on compiled JSnpm run build— TypeScript build todist/npm start— Run compiled server fromdist/index.js
Frontend (from frontend/):
npm run dev— Vite dev servernpm run build— TypeScript check + Vite buildnpm run preview— Preview built files
Base URL: http://localhost:5000/api/v1
Auth (cookie-based):
- POST
/user/signup— body:{ name, email, password }→ sets auth cookie; returns{ name, email } - POST
/user/login— body:{ email, password }→ sets auth cookie; returns{ name, email } - GET
/user/auth-status— requires cookie →{ name, email } - GET
/user/logout— clears auth cookie
Chats (require cookie):
- POST
/chat/new— body:{ message }→ returns{ chats }(array of messages{ id, role, content }) - GET
/chat/all-chats— returns{ message: "OK", chats } - DELETE
/chat/delete— clears saved chats →{ message: "OK" }
- Frontend Axios base URL is set in
frontend/src/main.tsx:axios.defaults.baseURL = 'http://localhost:5000/api/v1'- Update this when deploying or proxying through a different domain.
- Cookies are set for domain
localhostby the backend; adjust for production as needed.
- CORS error / cookies not sent:
- Update
origininbackend/src/app.tsto match your frontend URL. - Keep
axios.defaults.withCredentials = trueon the frontend.
- Update
- 401 "Token not provided" or "token expired":
- Ensure you logged in/signup first and cookies are present.
- Verify
COOKIE_SECRET/JWT_SECRETare set and consistent.
- MongoDB connection failures:
- Check
MONGODB_URL, network access/IP allowlist, and DNS.
- Check
- OpenAI errors (401/429):
- Verify
OPEN_AI_SECRET, organization (if required), and rate limits.
- Verify
ISC (see package.json files).
- OpenAI API
- React + Vite
- Express + Mongoose