diff --git a/src/app/api/user/userlist/route.tsx b/src/app/api/user/userlist/route.tsx new file mode 100644 index 00000000..509b50b2 --- /dev/null +++ b/src/app/api/user/userlist/route.tsx @@ -0,0 +1,32 @@ +import { NextResponse } from "next/server"; +import { NextRequest } from "next/server"; +import { NextApiRequest } from "next"; + +import clientPromise from "../../../../lib/mongodb"; + +export async function GET(request: NextApiRequest) { + try { + const client = await clientPromise; + const db = client.db("linkle"); + const collection = db.collection("userdata"); + + const { limit, offset } = request.query; + const limitNumber = parseInt(limit as string) || 10; + const offsetNumber = parseInt(offset as string) || 0; + + const users = await collection + .find({}) + .project({ password: 0, _id: 0, dateCreate: 0, data: 0, calendar: 0 }) + .skip(offsetNumber) + .limit(limitNumber) + .toArray(); + return NextResponse.json({ message: "success", users }, { status: 200 }); + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : "Unknown error"; + return NextResponse.json( + { message: "Failed to find data", error: errorMessage }, + { status: 500 }, + ); + } +} diff --git a/src/components/providers/React-Query-Provider.tsx b/src/components/providers/React-Query-Provider.tsx index a1014324..5abbb86c 100644 --- a/src/components/providers/React-Query-Provider.tsx +++ b/src/components/providers/React-Query-Provider.tsx @@ -11,7 +11,13 @@ type ReactQueryProviderProps = { export default function ReactQueryProvider({ children, }: ReactQueryProviderProps) { - const queryClient = new QueryClient(); + const queryClient = new QueryClient({ + defaultOptions: { + queries: { + staleTime: 1000 * 60 * 60, + }, + }, + }); return ( diff --git a/src/middleware.ts b/src/middleware.ts index 45bf5bda..549a4eae 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -2,40 +2,36 @@ import { NextRequest, NextResponse } from "next/server"; export function middleware(request: NextRequest) { - // const token = request.cookies.get("token"); + const token = request.cookies.get("token"); - // 비로그인 사용자가 특정 페이지에 접근하면 intro 페이지로 리다이렉트 - // if ( - // !token && - // request.nextUrl.pathname !== "/login" && - // request.nextUrl.pathname !== "/join" && - // request.nextUrl.pathname !== "/intro" - // ) { - // return NextResponse.redirect(new URL("/intro", request.url)); - // } - // - // // 로그인된 사용자가 로그인, 회원가입, intro 페이지에 접근 시 메인 페이지로 리다이렉트 - // if ( - // token && - // (request.nextUrl.pathname === "/login" || - // request.nextUrl.pathname === "/join" || - // request.nextUrl.pathname === "/intro") - // ) { - // return NextResponse.redirect(new URL("/", request.url)); - // } + if ( + !token && + request.nextUrl.pathname !== "/login" && + request.nextUrl.pathname !== "/join" && + request.nextUrl.pathname !== "/intro" + ) { + return NextResponse.redirect(new URL("/", request.url)); + } + + // 로그인된 사용자가 로그인, 회원가입, intro 페이지에 접근 시 메인 페이지로 리다이렉트 + if ( + token && + (request.nextUrl.pathname === "/login" || + request.nextUrl.pathname === "/join" || + request.nextUrl.pathname === "/intro") + ) { + return NextResponse.redirect(new URL("/main", request.url)); + } - // 모든 조건을 통과하면 다음으로 요청을 진행 return NextResponse.next(); } // 인증이 필요한 페이지 설정 export const config = { matcher: [ - "/", // 메인 페이지 "/profile/:path*", // 프로필 페이지와 하위 경로 "/admin/:path*", // 관리자 페이지와 하위 경로 "/login", // 로그인 페이지 "/join", // 회원가입 페이지 - "/intro", // 소개 페이지 ], };