Skip to content

TanmayShil/next-prisma-mongodb-api-routes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Next.js Auth + Product CRUD App

A full-stack application built using Next.js App Router, implementing JWT Authentication and Product CRUD operations using modern technologies like Prisma, MongoDB, Redux Toolkit, and TanStack Query.


πŸ“Œ Tech Stack

  • ⚑ Next.js (App Router + API Routes)
  • 🟦 TypeScript
  • πŸ—„οΈ MongoDB Atlas
  • πŸ”— Prisma ORM
  • 🧠 Redux Toolkit (Auth State)
  • πŸ“¦ TanStack Query (Server State)
  • 🎨 Tailwind CSS + shadcn/ui

πŸ“ Project Structure

β”œβ”€β”€ πŸ“ prisma/
β”‚   └── πŸ“„ schema.prisma
β”œβ”€β”€ πŸ“ public/
β”‚   β”œβ”€β”€ πŸ–ΌοΈ file.svg
β”‚   β”œβ”€β”€ πŸ–ΌοΈ globe.svg
β”‚   β”œβ”€β”€ πŸ–ΌοΈ next.svg
β”‚   β”œβ”€β”€ πŸ–ΌοΈ vercel.svg
β”‚   └── πŸ–ΌοΈ window.svg
β”œβ”€β”€ πŸ“ src/
β”‚   β”œβ”€β”€ πŸ“ app/
β”‚   β”‚   β”œβ”€β”€ πŸ“ (auth)/
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ login/
β”‚   β”‚   β”‚   β”‚   └── πŸ“„ page.tsx
β”‚   β”‚   β”‚   └── πŸ“ register/
β”‚   β”‚   β”‚       └── πŸ“„ page.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“ admin/
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ layout.tsx
β”‚   β”‚   β”‚   └── πŸ“„ page.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“ api/
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ auth/
β”‚   β”‚   β”‚   β”‚   └── πŸ“ [...nextauth]/
β”‚   β”‚   β”‚   β”‚       └── πŸ“„ route.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ login/
β”‚   β”‚   β”‚   β”‚   └── πŸ“„ route.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ products/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ [id]/
β”‚   β”‚   β”‚   β”‚   β”‚   └── πŸ“„ route.ts
β”‚   β”‚   β”‚   β”‚   └── πŸ“„ route.ts
β”‚   β”‚   β”‚   └── πŸ“ register/
β”‚   β”‚   β”‚       └── πŸ“„ route.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“„ favicon.ico
β”‚   β”‚   β”œβ”€β”€ 🎨 globals.css
β”‚   β”‚   β”œβ”€β”€ πŸ“„ layout.tsx
β”‚   β”‚   └── πŸ“„ page.tsx
β”‚   β”œβ”€β”€ πŸ“ components/
β”‚   β”‚   β”œβ”€β”€ πŸ“ ui/     #shadcn UI Components
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ button.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ card.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ dialog.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ input.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ label.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ magic-card.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ pagination.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ switch.tsx
β”‚   β”‚   β”‚   └── πŸ“„ table.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“„ DynamicInput.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ“„ ProductDialog.tsx
β”‚   β”‚   └── πŸ“„ Table.tsx
β”‚   β”œβ”€β”€ πŸ“ generated/
β”‚   β”œβ”€β”€ πŸ“ hooks/
β”‚   β”‚   └── πŸ“„ useProducts.ts
β”‚   β”œβ”€β”€ πŸ“ layout/
β”‚   β”‚   └── πŸ“„ Navbar.tsx
β”‚   β”œβ”€β”€ πŸ“ lib/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ auth.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“„ axios.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“„ jwt.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“„ prisma.ts
β”‚   β”‚   └── πŸ“„ utils.ts
β”‚   β”œβ”€β”€ πŸ“ reducer/
β”‚   β”‚   └── πŸ“„ product.rducer.ts
β”‚   β”œβ”€β”€ πŸ“ services/
β”‚   β”‚   β”œβ”€β”€ πŸ“ helpers/
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ provider/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ QueryProvider.tsx
β”‚   β”‚   β”‚   β”‚   └── πŸ“„ ReduxProvider.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ global.helper.ts
β”‚   β”‚   β”‚   └── πŸ“„ redux.ts
β”‚   β”‚   β”œβ”€β”€ πŸ“ jsons/
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ fields/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ auth.input.ts
β”‚   β”‚   β”‚   β”‚   └── πŸ“„ product.input.ts
β”‚   β”‚   β”‚   └── πŸ“ lottie/
β”‚   β”‚   └── πŸ“ validatons/
β”‚   β”‚       β”œβ”€β”€ πŸ“„ loginSchema.ts
β”‚   β”‚       β”œβ”€β”€ πŸ“„ productSchema.ts
β”‚   β”‚       └── πŸ“„ registerSchema.ts
β”‚   β”œβ”€β”€ πŸ“ store/
β”‚   β”‚   β”œβ”€β”€ πŸ“ slices/
β”‚   β”‚   β”‚   └── πŸ“„ auth.slice.ts
β”‚   β”‚   └── πŸ“„ store.ts
β”‚   β”œβ”€β”€ πŸ“ type/
β”‚   β”‚   β”œβ”€β”€ πŸ“ interface/
β”‚   β”‚   └── πŸ“ type/
β”‚   β”‚       β”œβ”€β”€ πŸ“„ auth.type.ts
β”‚   β”‚       β”œβ”€β”€ πŸ“„ components.type.ts
β”‚   β”‚       β”œβ”€β”€ πŸ“„ global.type.ts
β”‚   β”‚       β”œβ”€β”€ πŸ“„ product.type.ts
β”‚   β”‚       └── πŸ“„ redux.type.ts
β”‚   └── πŸ“„ proxy.ts
β”œβ”€β”€ βš™οΈ .gitignore
β”œβ”€β”€ πŸ“ README.md
β”œβ”€β”€ βš™οΈ components.json
β”œβ”€β”€ πŸ“„ eslint.config.mjs
β”œβ”€β”€ πŸ“„ next.config.ts
β”œβ”€β”€ βš™οΈ package-lock.json
β”œβ”€β”€ βš™οΈ package.json
β”œβ”€β”€ πŸ“„ postcss.config.mjs
└── βš™οΈ tsconfig.json


πŸ” Features

Authentication

  • User Registration
  • User Login
  • JWT Token Generation
  • Secure Route Handling

Product CRUD

  • Create Product
  • Get All Products
  • Update Product
  • Delete Product

πŸ—„οΈ Database Models

πŸ‘€ User

  • name
  • email (unique)
  • phone
  • password

πŸ“¦ Product

  • name
  • desc
  • price

βš™οΈ MongoDB Atlas

βœ… Why MongoDB?

  • NoSQL database β†’ flexible schema
  • Easy to scale
  • Fast for CRUD operations
  • Works well with JSON-based apps

πŸ”§ Setup Steps

  1. Go to MongoDB Atlas
  2. Create a free cluster
  3. Create database user (username & password)
  4. Allow IP access (0.0.0.0/0 for development)
  5. Copy connection string
DATABASE_URL="mongodb+srv://user:pass@cluster/db"

βš™οΈ Prisma ORM

βœ… Why Prisma?

  • Type-safe queries
  • Auto-generated database client
  • Cleaner and readable code
  • Reduces runtime errors

πŸ”§ Setup Steps

1. Install Prisma

npm install prisma @prisma/client
npx prisma init

2. Configure Environment

DATABASE_URL="your_mongodb_connection_string"

3. Define Schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id       String @id @default(auto()) @map("_id") @db.ObjectId
  name     String
  email    String @unique
  phone    String
  password String

   products Product[]
}

model Product {
 id          String   @id @default(auto()) @map("_id") @db.ObjectId
 name        String
 price       Float
 description String
 isPublish   Boolean  @default(false)

 createdAt   DateTime @default(now())

 userId String   @db.ObjectId
 user   User     @relation(fields: [userId], references: [id])
}

4. Generate Client

npx prisma generate

πŸ‘‰ Prisma works as a bridge between your API and MongoDB, making database operations simple and type-safe.


πŸ”Œ API Routes (Next.js Backend)

βœ… Why API Routes?

  • Built-in backend inside Next.js
  • No need for separate server
  • Easy integration with frontend
  • Scalable structure

πŸ“‚ API Structure

/api/register
/api/login
/api/products
/api/products/[id]

πŸ” Auth APIs

➀ POST /api/register

  • Create new user
  • Store hashed password
  • Return success message

➀ POST /api/login

  • Validate user credentials
  • Generate JWT token
  • Return token + status Example Response:
{
  "statusCode": 200,
  "message": "Login successful",
  "token": "JWT_TOKEN",
  "user: {} //user details
}

πŸ“¦ Product APIs

➀ GET /api/products

  • Fetch all products

➀ POST /api/products

  • Create new product

➀ PUT /api/products/:id

  • Update product

➀ DELETE /api/products/:id

  • Delete product

🧠 State Management

Redux Toolkit (Authentication)

  • Stores user data & JWT token
  • Handles login/logout globally
  • Centralized state management

TanStack Query (Products)

  • Fetch & cache API data
  • Auto refetching
  • Keeps UI in sync with server

🎨 UI & Styling

  • Tailwind CSS β†’ Utility-first styling
  • shadcn/ui β†’ Reusable UI components

πŸ” Middleware (proxy.ts)

  • Protects private routes
  • Validates JWT token
  • Redirects unauthorized users

πŸ™‹β€β™‚οΈ Author

Made with ❀️ by Tanmay Shil GitHub: @TanmayShil

About

A full-stack Next.js application using Prisma ORM with MongoDB and API Routes. Includes scalable backend architecture, database integration, CRUD operations, TypeScript support, and modern development practices for building secure and performant web applications.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors