diff --git a/middleware.ts b/src/Middleware/AuthMiddleware.ts similarity index 57% rename from middleware.ts rename to src/Middleware/AuthMiddleware.ts index 421fb49..6eafe08 100644 --- a/middleware.ts +++ b/src/Middleware/AuthMiddleware.ts @@ -1,34 +1,31 @@ import { NextResponse } from "next/server"; -import type { NextRequest } from "next/server"; import jwt from "jsonwebtoken"; import User from "@/models/user"; import { mongoDB } from "@/lib/MongoDB"; -export async function middleware(req: NextRequest) { +export async function AuthMiddleware(req: any) { try { - const token : any = req.cookies.get('accessToken') || req.headers.get('Authorization')?.replace('Bearer ', ''); + const token = req.cookies.get('accessToken') || req.headers.get('Authorization')?.replace('Bearer ', ''); - if (!token) { + if (!token || token === undefined) { return NextResponse.json({ error: 'Unauthorized Access!' }, { status: 401 }); } - const decodedToken : any = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET as string); + const decodedToken:any = jwt.verify(token, process.env.NEXTAUTH_SECRET as string); await mongoDB(); const user = await User.findById(decodedToken._id).select('-password -refreshToken'); - if (!user) return NextResponse.json({ error: 'Unauthorized Access!' }, { status: 401 }); + if (!user) { + return NextResponse.json({ error: 'Unauthorized Access!' }, { status: 401 }); + } // Pass user data in request headers for further processing in API route req.headers.set('user', JSON.stringify(user)); - + return NextResponse.next(); } catch (err) { console.log(err); return NextResponse.json({ message: 'Error occurred!' }, { status: 500 }); } } - -export const config = { - matcher: ["/dashboard"], -}; diff --git a/src/app/api/files/create/route.ts b/src/app/api/files/create/route.ts new file mode 100644 index 0000000..2dc1e39 --- /dev/null +++ b/src/app/api/files/create/route.ts @@ -0,0 +1,35 @@ +import { mongoDB } from "@/lib/MongoDB"; +import { AuthMiddleware } from "@/Middleware/AuthMiddleware"; +import FileModel from "@/models/file"; +import { ApiUser } from "@/types/types"; +import { NextResponse } from "next/server"; + +export const POST = async (req: Request) => { + + const result = await AuthMiddleware(req); + + if (result instanceof NextResponse) { + + try { + const { fileName, filePrivate } = await req.json(); + + await mongoDB(); + + const user: ApiUser = JSON.parse(req.headers.get("user") || "{}"); + + const file = await FileModel.create({ + fileName, + filePrivate, + createdBy:user._id, + readBy:[user._id], + writtenBy:[user._id] + }); + + return NextResponse.json({ status: 200 }); + } catch (err) { + return NextResponse.json(`Err : ${err}`, {status:500}); + } + } else { + return result; + } +}; diff --git a/src/app/api/files/private/route.ts b/src/app/api/files/private/route.ts index d114b20..39f3a42 100644 --- a/src/app/api/files/private/route.ts +++ b/src/app/api/files/private/route.ts @@ -22,4 +22,4 @@ export const PUT = async(req: Request) => { } catch (err) { console.log(err) } -} +} \ No newline at end of file diff --git a/src/app/api/health/route.ts b/src/app/api/health/route.ts new file mode 100644 index 0000000..99a27af --- /dev/null +++ b/src/app/api/health/route.ts @@ -0,0 +1,19 @@ +import { AuthMiddleware } from "@/Middleware/AuthMiddleware"; +import { NextRequest } from "next/server"; +import { NextResponse } from "next/server"; + +export const GET = async (req: NextRequest, res: NextResponse) => { + try { + const result = await AuthMiddleware(req); + // If middleware returns NextResponse.next(), proceed with API logic + if (result.status==200) { + return NextResponse.json({ status: "UP" }, { status: 200 }); + } else { + // Handle any errors from the middleware + return result; + } + } catch (error) { + console.error("Error in health endpoint:", error); + return NextResponse.json({ message: 'Error occurred!' }, { status: 500 }); + } +}; \ No newline at end of file diff --git a/src/app/dashboard/layout.tsx b/src/app/dashboard/layout.tsx index 6d740c5..1baf6a7 100644 --- a/src/app/dashboard/layout.tsx +++ b/src/app/dashboard/layout.tsx @@ -13,14 +13,14 @@ import { useMutation } from "convex/react"; import Image from "next/image"; import { SessionProvider, useSession } from "next-auth/react"; import Link from "next/link"; -import { logIn } from "../Redux/Auth/auth-slice"; + function DashboardLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { - const {data:session,status} = useSession(); + const { data: session, status } = useSession(); const createTeam = useMutation(api.teams.createTeam); const convex = useConvex(); const [fileList_, setFileList_] = useState(); @@ -30,22 +30,6 @@ function DashboardLayout({ const [hasCheckedTeam, setHasCheckedTeam] = useState(false); const dispatch = useDispatch(); - // useEffect(() => { - // if (session) { - // dispatch( - // logIn({ - // id: session.user.id, - // accessToken: session.user.accessToken, - // refreshToken: session.user.refreshToken, - // email: session.user.email, - // firstName: session.user.firstName, - // lastName: session.user.lastName, - // image:session.user.image - // }) - // ); - // } - // }, [session]); - useEffect(() => { if (session && !hasCheckedTeam) { checkTeam(); @@ -88,40 +72,38 @@ function DashboardLayout({ />
To access this page, please{" "}
Don't have an account? - - Signup - + Signup