product listing #1
177
auth-jwt-api.postman_collection.json
Normal file
177
auth-jwt-api.postman_collection.json
Normal file
|
@ -0,0 +1,177 @@
|
|||
{
|
||||
"info": {
|
||||
"_postman_id": "441e5d69-a9f4-41d9-bb4d-99529b680807",
|
||||
"name": "auth-jwt-api",
|
||||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
|
||||
"_exporter_id": "44557177"
|
||||
},
|
||||
"item": [
|
||||
{
|
||||
"name": "Register",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"username\": \"yash\",\n \"email\": \"yash@gmail.com\",\n \"password\": \"Yash@123\"\n}\n",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:5000/api/auth/register",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "5000",
|
||||
"path": [
|
||||
"api",
|
||||
"auth",
|
||||
"register"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "Login",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"email\": \"yash@gmail.com\",\n \"password\": \"Yash@123\"\n}\n",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:5000/api/auth/login",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "5000",
|
||||
"path": [
|
||||
"api",
|
||||
"auth",
|
||||
"login"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "Token",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InNheWFrIiwiZW1haWwiOiJzYXlha0BleGFtcGxlLmNvbSIsImlhdCI6MTc0NjA4NDkxOCwiZXhwIjoxNzQ4Njc2OTE4fQ.71XTEyrGfH8cmFbA-sEVOBmR4SU5XiOvKt8TWjJMDIk\"\n}\n",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:5000/api/auth/token",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "5000",
|
||||
"path": [
|
||||
"api",
|
||||
"auth",
|
||||
"token"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "Protected Route",
|
||||
"protocolProfileBehavior": {
|
||||
"disableBodyPruning": true
|
||||
},
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [
|
||||
{
|
||||
"key": "Authorization",
|
||||
"value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Inlhc2giLCJlbWFpbCI6Inlhc2hAZ21haWwuY29tIiwiaWF0IjoxNzQ2MDk1NjUxLCJleHAiOjE3NDYwOTY1NTF9.qysDHyQSQsM9t9jyUXL_fc-uSes3ndjU_2TKPCxuMXI",
|
||||
"type": "text"
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InByYWJoam90IiwiZW1haWwiOiJwcmFiaGpvdEBleGFtcGxlLmNvbSIsImlhdCI6MTc0NjA4NDEwNiwiZXhwIjoxNzQ4Njc2MTA2fQ.4QpCLuoZFNR9D9_QZgsceNuuMnixFrBRF5UfB89wRYY\"\n}\n",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:5000/api/auth/protected",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "5000",
|
||||
"path": [
|
||||
"api",
|
||||
"auth",
|
||||
"protected"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "Products",
|
||||
"protocolProfileBehavior": {
|
||||
"disableBodyPruning": true
|
||||
},
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [
|
||||
{
|
||||
"key": "Authorization",
|
||||
"value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Inlhc2giLCJlbWFpbCI6Inlhc2hAZ21haWwuY29tIiwiaWF0IjoxNzQ2MDk1NjUxLCJleHAiOjE3NDYwOTY1NTF9.qysDHyQSQsM9t9jyUXL_fc-uSes3ndjU_2TKPCxuMXI",
|
||||
"type": "text"
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "\n",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:5000/api/products",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "5000",
|
||||
"path": [
|
||||
"api",
|
||||
"products"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
]
|
||||
}
|
|
@ -1,15 +1,36 @@
|
|||
// const express = require('express');
|
||||
// const router = express.Router();
|
||||
// const Product = require('../models/Product');
|
||||
// const auth = require('../middleware/auth');
|
||||
|
||||
// // GET /api/products - Returns all products (JWT protected)
|
||||
// router.get('/', auth, async (req, res) => {
|
||||
// try {
|
||||
// const products = await Product.find();
|
||||
// res.json(products);
|
||||
// } catch (err) {
|
||||
// res.status(500).json({ error: 'Failed to fetch products' });
|
||||
// }
|
||||
// });
|
||||
|
||||
// module.exports = router;
|
||||
|
||||
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Product = require('../models/Product');
|
||||
const auth = require('../middleware/auth');
|
||||
const authenticateToken = require('../middleware/auth'); // adjust path if needed
|
||||
|
||||
// GET /api/products - Returns all products (JWT protected)
|
||||
router.get('/', auth, async (req, res) => {
|
||||
// GET /api/products?category=Electronics
|
||||
router.get('/', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const products = await Product.find();
|
||||
const category = req.query.category;
|
||||
const filter = category ? { category } : {};
|
||||
const products = await Product.find(filter);
|
||||
res.json(products);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: 'Failed to fetch products' });
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -1,19 +1,41 @@
|
|||
require('dotenv').config();
|
||||
const mongoose = require('mongoose');
|
||||
const Product = require('../models/Product');
|
||||
require("dotenv").config();
|
||||
const mongoose = require("mongoose");
|
||||
const Product = require("../models/Product");
|
||||
|
||||
const seedProducts = [
|
||||
{ name: 'iPhone 14', category: 'Electronics', price: 999 },
|
||||
{ name: 'Nike Air Max', category: 'Footwear', price: 120 },
|
||||
{ name: 'Sony Headphones', category: 'Electronics', price: 199 }
|
||||
// { name: 'iPhone 14', category: 'Electronics', price: 999 },
|
||||
// { name: 'Nike Air Max', category: 'Footwear', price: 120 },
|
||||
// { name: 'Sony Headphones', category: 'Electronics', price: 199 }
|
||||
|
||||
{ name: "iPhone 14", category: "Electronics", price: 999 },
|
||||
{ name: "Nike Air Max", category: "Footwear", price: 120 },
|
||||
{ name: "Sony Headphones", category: "Electronics", price: 199 },
|
||||
|
||||
{ name: "Samsung Galaxy S23", category: "Electronics", price: 899 },
|
||||
{ name: "Dell XPS 13", category: "Computers", price: 1299 },
|
||||
{ name: "Apple Watch Series 8", category: "Wearables", price: 399 },
|
||||
{ name: "Logitech MX Master 3", category: "Accessories", price: 99 },
|
||||
{ name: "Kindle Paperwhite", category: "Electronics", price: 139 },
|
||||
{ name: "Canon EOS M50", category: "Electronics", price: 649 },
|
||||
{ name: "Bose QuietComfort 45", category: "Electronics", price: 329 },
|
||||
{ name: "Asus ROG Strix G16", category: "Computers", price: 1599 },
|
||||
{ name: "Google Pixel 8", category: "Electronics", price: 799 },
|
||||
{ name: "Anker PowerCore 20000", category: "Accessories", price: 59 },
|
||||
{ name: "Samsung Galaxy Watch 6", category: "Wearables", price: 349 },
|
||||
{ name: "Razer BlackWidow V3", category: "Accessories", price: 139 },
|
||||
{ name: "HP Spectre x360", category: "Computers", price: 1399 },
|
||||
{ name: "Meta Quest 3", category: "Wearables", price: 499 },
|
||||
{ name: "Beats Studio Pro", category: "Electronics", price: 349 }
|
||||
|
||||
];
|
||||
|
||||
mongoose.connect(process.env.MONGODB_URI)
|
||||
mongoose
|
||||
.connect(process.env.MONGODB_URI)
|
||||
.then(async () => {
|
||||
console.log('MongoDB connected');
|
||||
console.log("MongoDB connected");
|
||||
await Product.deleteMany(); // Optional: clears old data
|
||||
await Product.insertMany(seedProducts);
|
||||
console.log('Sample products inserted');
|
||||
console.log("Sample products inserted");
|
||||
process.exit();
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
.catch((err) => console.log(err));
|
||||
|
|
Loading…
Reference in a new issue