diff --git a/app/auth/login/page.tsx b/app/(auth)/login/page.tsx similarity index 100% rename from app/auth/login/page.tsx rename to app/(auth)/login/page.tsx diff --git a/app/(auth)/signup/page.tsx b/app/(auth)/signup/page.tsx new file mode 100644 index 0000000..677b48c --- /dev/null +++ b/app/(auth)/signup/page.tsx @@ -0,0 +1,92 @@ +"use client"; + +import axios from "axios"; +import { useForm, SubmitHandler } from "react-hook-form"; + +type Inputs = { + name: string; + email: string; + password: string; + confirmPassword: string; +}; + +export default function Signup() { + const { + register, + handleSubmit, + formState: { errors }, + } = useForm(); + const onSubmit: SubmitHandler = async (data) => { + console.log(data); + + if (data.password !== data.confirmPassword) { + console.log("password not matched"); + } + + try { + const response = await axios.post("/api/user/signup", { + data, + }); + console.log(response); + } catch (error) {} + }; + + return ( +
+
+ + {errors.name && Name required} + + {errors.email && Email required} + + {errors.password && ( + Password required + )} + + {errors.confirmPassword && ( + Confirm password required + )} + +
+
+ ); +} diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 38348c4..e21de82 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -96,7 +96,7 @@ export const authOptions: NextAuthOptions = { strategy: "jwt", }, pages: { - signIn: "/auth/login", + signIn: "/login", }, }; diff --git a/app/api/product/list/route.ts b/app/api/product/list/route.ts index bfc9c44..a1fdbbe 100644 --- a/app/api/product/list/route.ts +++ b/app/api/product/list/route.ts @@ -3,12 +3,7 @@ import { NextRequest, NextResponse } from "next/server"; import { authOptions } from "../../auth/[...nextauth]/route"; import { db } from "@/lib/firebaseConfig"; import { collection, getDocs, query, where } from "firebase/firestore"; - -interface Product { - product: string; - userId: string; - id: string; -} +import { Product } from "@/types/types"; export async function POST(req: NextRequest) { const session = await getServerSession(authOptions); diff --git a/app/api/product/route.ts b/app/api/product/route.ts index 4ccbb39..f144d6e 100644 --- a/app/api/product/route.ts +++ b/app/api/product/route.ts @@ -1,7 +1,6 @@ import { getServerSession } from "next-auth"; import { NextRequest, NextResponse } from "next/server"; import { authOptions } from "../auth/[...nextauth]/route"; -import connectDB from "@/lib/db"; import { db } from "@/lib/firebaseConfig"; import { addDoc, @@ -10,13 +9,7 @@ import { serverTimestamp, where, } from "firebase/firestore"; - -connectDB(); - -interface Product { - productName: string; - userId: string; -} +import { Product } from "@/types/types"; export async function POST(req: NextRequest) { const session = await getServerSession(authOptions); @@ -32,14 +25,15 @@ export async function POST(req: NextRequest) { snapshot.docs.forEach((doc) => { const productData = doc.data(); const currentProduct: Product = { - productName: productData.product, + product: productData.product, userId: productData.userId, + id: productData.id, }; products.push(currentProduct); }); const productExist = products.find( - (p) => p.productName === product && p.userId === userId + (p) => p.product === product && p.userId === userId ); if (productExist) { return NextResponse.json( diff --git a/app/api/purchase/list/route.ts b/app/api/purchase/list/route.ts index 070019a..11180e5 100644 --- a/app/api/purchase/list/route.ts +++ b/app/api/purchase/list/route.ts @@ -3,23 +3,7 @@ import { NextRequest, NextResponse } from "next/server"; import { authOptions } from "../../auth/[...nextauth]/route"; import { collection, getDocs } from "firebase/firestore"; import { db } from "@/lib/firebaseConfig"; -import PurchaseEntry from "@/models/purchase"; - -interface Items { - itemName: string; - price: string; - quantity: string; -} - -export interface Purchase { - id: string; - date: string; - totalAmount: number; - userId: string; - vendorId: string; - vendorName: string; - items: Items[]; -} +import { Purchase } from "@/types/types"; export async function POST(req: NextRequest) { const session = await getServerSession(authOptions); diff --git a/app/api/purchase/route.ts b/app/api/purchase/route.ts index 227ee0a..9b34620 100644 --- a/app/api/purchase/route.ts +++ b/app/api/purchase/route.ts @@ -4,7 +4,7 @@ import { authOptions } from "../auth/[...nextauth]/route"; import PurchaseEntry from "@/models/purchase"; import { addDoc, collection, getDocs } from "firebase/firestore"; import { db } from "@/lib/firebaseConfig"; -import { Purchase } from "@/app/api/purchase/list/route"; +import { Purchase } from "@/types/types"; // add new purchase export async function POST(req: NextRequest) { diff --git a/app/api/vendor/list/route.ts b/app/api/vendor/list/route.ts index 365ab54..f64c699 100644 --- a/app/api/vendor/list/route.ts +++ b/app/api/vendor/list/route.ts @@ -3,13 +3,7 @@ import { NextRequest, NextResponse } from "next/server"; import { authOptions } from "../../auth/[...nextauth]/route"; import { collection, getDocs, query, where } from "firebase/firestore"; import { db } from "@/lib/firebaseConfig"; - -interface Vendor { - id: string; - vendorName: string; - userId: string; - mobile: string; -} +import { Vendor } from "@/types/types"; export async function POST(req: NextRequest) { const session = await getServerSession(authOptions); diff --git a/app/api/vendor/route.ts b/app/api/vendor/route.ts index e80039f..7bee114 100644 --- a/app/api/vendor/route.ts +++ b/app/api/vendor/route.ts @@ -8,12 +8,7 @@ import { serverTimestamp, } from "firebase/firestore"; import { db } from "@/lib/firebaseConfig"; - -interface Vendor { - mobile: string; - userId: string; - vendorName: string; -} +import { Vendor } from "@/types/types"; export async function POST(req: NextRequest) { const session = await getServerSession(authOptions); @@ -33,6 +28,7 @@ export async function POST(req: NextRequest) { vendorName: vendorData.vendorName, mobile: vendorData.mobile, userId: vendorData.userId, + id: vendorData.id, }; vendors.push(currentVendor); }); diff --git a/app/auth/signup/page.tsx b/app/auth/signup/page.tsx deleted file mode 100644 index 6446517..0000000 --- a/app/auth/signup/page.tsx +++ /dev/null @@ -1,90 +0,0 @@ -"use client"; - -import axios from "axios"; -import { useForm, SubmitHandler } from "react-hook-form"; - -type Inputs = { - name: string; - email: string; - password: string; - confirmPassword: string; -}; - -export default function Signup() { - const { - register, - handleSubmit, - formState: { errors }, - } = useForm(); - const onSubmit: SubmitHandler = async (data) => { - console.log(data); - - if (data.password !== data.confirmPassword) { - console.log("password not matched"); - } - - try { - const response = await axios.post("/api/user/signup", { - data, - }); - console.log(response); - } catch (error) {} - }; - - return ( -
- - {errors.name && Name required} - - {errors.email && Email required} - - {errors.password && ( - Password required - )} - - {errors.confirmPassword && ( - Confirm password required - )} - -
- ); -} diff --git a/lib/db.ts b/lib/db.ts deleted file mode 100644 index 734b553..0000000 --- a/lib/db.ts +++ /dev/null @@ -1,13 +0,0 @@ -import mongoose from "mongoose"; - -const connectDB = async () => { - try { - const conn = await mongoose.connect(process.env.MONGO_URI!); - console.log(`MongoDB Connected: ${conn.connection.host}`); - } catch (error: any) { - console.error(`Error connecting to database: ${error.message}`); - process.exit(1); - } -}; - -export default connectDB; diff --git a/middleware.ts b/middleware.ts index 5b4aa07..d74644f 100644 --- a/middleware.ts +++ b/middleware.ts @@ -6,7 +6,7 @@ export default withAuth( const pathname = req.nextUrl.pathname; const protectedRoutes = config.matcher; if (protectedRoutes.includes(pathname) && !req.nextauth.token) { - return NextResponse.rewrite(new URL("/auth/login")); + return NextResponse.rewrite(new URL("/login")); } }, { diff --git a/models/product.ts b/models/product.ts deleted file mode 100644 index 4a7dd60..0000000 --- a/models/product.ts +++ /dev/null @@ -1,21 +0,0 @@ -import mongoose from "mongoose"; - -const productModel = new mongoose.Schema( - { - product: { - type: String, - required: [true, "Product name required."], - }, - userId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Users", - requried: true, - }, - }, - { timestamps: true } -); - -const Product = - mongoose.models.products || mongoose.model("products", productModel); - -export default Product; diff --git a/models/purchase.ts b/models/purchase.ts deleted file mode 100644 index b22e629..0000000 --- a/models/purchase.ts +++ /dev/null @@ -1,58 +0,0 @@ -import mongoose from "mongoose"; - -interface Item { - itemName: string; - quantity: number; - price: number; -} - -const purchaseModel = new mongoose.Schema( - { - date: { - type: Date, - default: Date.now, - required: true, - }, - userId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Users", - required: true, - }, - vendorId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Vendors", - required: true, - }, - vendorName: { - type: String, - required: true, - }, - totalAmount: { - type: Number, - required: true, - }, - items: { - type: [ - { - itemName: String, - quantity: Number, - price: Number, - }, - ], - validate: [ - { - validator: function (items: Item[]) { - return items.length > 0; - }, - }, - ], - }, - }, - { timestamps: true } -); - -const PurchaseEntry = - mongoose.models.PurchaseEntry || - mongoose.model("PurchaseEntry", purchaseModel); - -export default PurchaseEntry; diff --git a/models/user.ts b/models/user.ts deleted file mode 100644 index 96642b6..0000000 --- a/models/user.ts +++ /dev/null @@ -1,19 +0,0 @@ -import mongoose from "mongoose"; - -const userSchema = new mongoose.Schema( - { - email: { - type: String, - unique: [true, "Email already exists."], - required: [true, "Email is required."], - }, - name: { - type: String, - required: [true, "Name is required."], - }, - }, - { timestamps: true } -); - -const User = mongoose.models.users || mongoose.model("users", userSchema); -export default User; diff --git a/models/vendor.ts b/models/vendor.ts deleted file mode 100644 index c4ae7ee..0000000 --- a/models/vendor.ts +++ /dev/null @@ -1,26 +0,0 @@ -import mongoose from "mongoose"; - -const vendorModel = new mongoose.Schema( - { - vendorName: { - type: String, - required: [true, "Vendor name required."], - }, - mobile: { - type: String, - required: [true, "Mobile is required."], - }, - userId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Users", - required: true, - }, - description: String, - }, - { timestamps: true } -); - -const Vendor = - mongoose.models.vendors || mongoose.model("vendors", vendorModel); - -export default Vendor; diff --git a/types/types.ts b/types/types.ts new file mode 100644 index 0000000..f3f1443 --- /dev/null +++ b/types/types.ts @@ -0,0 +1,28 @@ +export interface Vendor { + id: string; + vendorName: string; + userId: string; + mobile: string; +} + +export interface Product { + product: string; + userId: string; + id: string; +} + +export interface Items { + itemName: string; + price: string; + quantity: string; +} + +export interface Purchase { + id: string; + date: string; + totalAmount: number; + userId: string; + vendorId: string; + vendorName: string; + items: Items[]; +}