Skip to content

Commit

Permalink
feat: 회원가입 페이지 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunseo11 committed May 18, 2024
1 parent cac2da7 commit 4715e16
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 23 deletions.
19 changes: 19 additions & 0 deletions weekly-mission/next-project/db/dbConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MongoClient } from "mongodb";

const url =
"mongodb+srv://hs0727:hsjh1004!@cluster0.aeptwaq.mongodb.net/check-email?retryWrites=true&w=majority&appName=Cluster0";
const options: any = { useNewUrlParser: true };

let cachedClient: MongoClient | null = null;

export async function connectDB(): Promise<MongoClient> {
if (cachedClient) {
return cachedClient;
}

const client = new MongoClient(url, options);
await client.connect();

cachedClient = client;
return client;
}
10 changes: 10 additions & 0 deletions weekly-mission/next-project/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ declare module "*.module.css" {
export default classes;
}

// global.d.ts

import type { MongoClient } from "mongodb";

declare global {
namespace globalThis {
var _mongo: Promise<MongoClient>;
}
}

declare module "*.png";
declare module "*.jpg";
declare module "*.svg";
191 changes: 187 additions & 4 deletions weekly-mission/next-project/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions weekly-mission/next-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"axios": "^1.6.8",
"mongoose": "^8.4.0",
"next": "14.2.3",
"react": "^18",
"react-dom": "^18",
Expand Down
33 changes: 33 additions & 0 deletions weekly-mission/next-project/pages/api/check-email.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { connectDB } from "db/dbConnect";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
// 데이터베이스 연결
const client = await connectDB();
const db = client.db();

if (req.method !== "POST") {
return res.status(405).json({ message: "Method not allowed" });
}

const { email } = req.body;

// 이메일이 이미 존재하는지 확인
const existingEmail = await db.collection("users").findOne({ email });
if (existingEmail) {
return res.status(409).json({ message: "이미 사용 중인 이메일입니다." });
}

// 이메일이 존재하지 않으면 새로 추가
await db.collection("users").insertOne({ email });

return res.status(200).json({ message: "사용 가능한 이메일입니다." });
} catch (error) {
console.error("Error:", error);
return res.status(500).json({ message: "서버 오류 발생" });
}
}
17 changes: 6 additions & 11 deletions weekly-mission/next-project/pages/api/sign-in.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "POST") {
const { email, password } = req.body;
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { email, password } = req.body;

if (email === "test@codeit.com" && password === "sprint101") {
res.status(200).json({ success: true });
} else {
res.status(401).json({ success: false, message: "로그인 실패" });
}
if (email === "test@codeit.com" && password === "sprint101") {
res.status(200).json({ success: true });
} else {
res.status(401).json({ success: false, message: "로그인 오류" });
}
}
Loading

0 comments on commit 4715e16

Please sign in to comment.