Skip to content

Commit c74df6a

Browse files
authored
Merge pull request #43 from UMC-7th-CAU-NodeJS/dev
Dev
2 parents 4d4376f + 590f2ca commit c74df6a

27 files changed

+2189
-19
lines changed

package-lock.json

+449-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"start": "node src/index.js",
10-
"dev": "nodemon --exec node src/index.js"
10+
"dev": "nodemon -e js,json,prisma --exec \"prisma generate && node src/index.js\""
1111
},
1212
"author": "",
1313
"license": "ISC",
1414
"dependencies": {
15-
"express": "^5.0.0-0"
15+
"@prisma/client": "^5.21.1",
16+
"@quixo3/prisma-session-store": "^3.1.13",
17+
"cors": "^2.8.5",
18+
"dotenv": "^16.4.5",
19+
"express": "^5.0.0-0",
20+
"express-session": "^1.18.1",
21+
"http-status-codes": "^2.3.0",
22+
"mysql2": "^3.11.3",
23+
"passport": "^0.7.0",
24+
"passport-github2": "^0.1.12",
25+
"passport-google-oauth20": "^2.0.0",
26+
"prisma": "^5.21.1",
27+
"swagger-autogen": "^2.23.7",
28+
"swagger-ui-express": "^5.0.1"
1629
},
1730
"devDependencies": {
1831
"nodemon": "^3.1.4"

prisma/schema.prisma

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
}
4+
5+
datasource db {
6+
provider = "mysql"
7+
url = env("DATABASE_URL")
8+
}
9+
10+
model User {
11+
id Int @id @default(autoincrement())
12+
email String @unique @db.VarChar(20)
13+
password String? @db.VarChar(15)
14+
nickname String? @db.VarChar(10)
15+
name String @map("username") @db.VarChar(10)
16+
gender String @db.VarChar(10)
17+
birth DateTime @map("birthdate") @db.Date
18+
address String @db.VarChar(30)
19+
detailAddress String @map("detail_address") @db.VarChar(50)
20+
phoneNumber String @map("phone_num") @db.VarChar(20)
21+
preferences Json? //Json 타입으로 변경
22+
score Int?
23+
currentRegionId Int? @map("current_region")
24+
status String? @db.VarChar(10)
25+
inactiveDate DateTime? @map("inactive_date")
26+
createdAt DateTime? @map("created_at") @default(now()) @db.DateTime
27+
updatedAt DateTime? @map("updated_at") @db.DateTime
28+
29+
reviews Review[]
30+
userFavorCategories UserFood[]
31+
userRestaurants UserRestaurant[]
32+
userMissions UserMission[]
33+
inquiries Inquiry[]
34+
}
35+
36+
model Food {
37+
id Int @id @default(autoincrement())
38+
name String @db.VarChar(20)
39+
genre String @db.VarChar(10)
40+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
41+
updatedAt DateTime @map("updated_at") @db.DateTime
42+
43+
userFavorCategories UserFood[]
44+
foodRestaurants FoodRestaurant[]
45+
}
46+
47+
model UserFood {
48+
id Int @id @default(autoincrement())
49+
userId Int @map("user_id")
50+
foodId Int @map("food_id")
51+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
52+
updatedAt DateTime @updatedAt @map("updated_at") @db.DateTime
53+
54+
user User @relation(fields: [userId], references: [id])
55+
food Food @relation(fields: [foodId], references: [id])
56+
}
57+
58+
model Restaurant {
59+
id Int @id @default(autoincrement())
60+
name String @db.VarChar(20)
61+
ownerId Int? @map("owner_id")
62+
type String @db.VarChar(10)
63+
address String @db.VarChar(30)
64+
currentRegionId Int? @map("current_region")
65+
commentId Int? @map("comment_id")
66+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
67+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
68+
69+
owner Owner? @relation(fields: [ownerId], references: [id])
70+
currentRegion Region? @relation(fields: [currentRegionId], references: [id])
71+
reviews Review[]
72+
userRestaurants UserRestaurant[]
73+
missions Mission[]
74+
foodRestaurants FoodRestaurant[]
75+
}
76+
77+
model Owner {
78+
id Int @id @default(autoincrement())
79+
name String @db.VarChar(10)
80+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
81+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
82+
83+
restaurants Restaurant[]
84+
}
85+
86+
model Review {
87+
id Int @id @default(autoincrement())
88+
userId Int @map("user_id")
89+
restaurantId Int @map("restaurant_id")
90+
rate Int
91+
content String @db.Text
92+
image Bytes? @db.Blob
93+
reply String? @db.Text
94+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
95+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
96+
97+
user User @relation(fields: [userId], references: [id])
98+
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
99+
}
100+
101+
model Region {
102+
id Int @id @default(autoincrement())
103+
regionName String @map("region_name") @db.VarChar(15)
104+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
105+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
106+
restaurants Restaurant[]
107+
}
108+
109+
model Mission {
110+
id Int @id @default(autoincrement())
111+
restaurantId Int @map("restaurant_id")
112+
description String @db.Text
113+
score Int
114+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
115+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
116+
117+
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
118+
userMissions UserMission[]
119+
}
120+
121+
model UserRestaurant {
122+
id Int @id @default(autoincrement())
123+
userId Int @map("user_id")
124+
restaurantId Int @map("restaurant_id")
125+
visited Boolean
126+
visitedDate DateTime? @map("visited_date")
127+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
128+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
129+
130+
user User @relation(fields: [userId], references: [id])
131+
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
132+
}
133+
134+
model UserMission {
135+
id Int @id @default(autoincrement())
136+
missionId Int @map("mission_id")
137+
userId Int @map("user_id")
138+
status String @db.VarChar(10)
139+
deadline DateTime
140+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
141+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
142+
143+
mission Mission @relation(fields: [missionId], references: [id])
144+
user User @relation(fields: [userId], references: [id])
145+
}
146+
147+
model FoodRestaurant {
148+
id Int @id @default(autoincrement())
149+
foodId Int @map("food_id")
150+
restaurantId Int @map("restaurant_id")
151+
createdAt DateTime @map("created_at") @default(now()) @db.DateTime
152+
updatedAt DateTime @map("updated_at") @updatedAt @db.DateTime
153+
154+
food Food @relation(fields: [foodId], references: [id])
155+
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
156+
}
157+
158+
model Inquiry {
159+
id Int @id @default(autoincrement())
160+
userId Int @map("user_id")
161+
title String @db.VarChar(30)
162+
type String @db.VarChar(10)
163+
content String @db.Text
164+
image Bytes? @db.Blob
165+
reply String? @db.Text
166+
createdAt DateTime @default(now()) @db.DateTime
167+
updatedAt DateTime @updatedAt @db.DateTime
168+
169+
user User @relation(fields: [userId], references: [id])
170+
}
171+
172+
173+
model Session {
174+
id String @id
175+
sid String @unique
176+
data String @db.VarChar(1024)
177+
expiresAt DateTime @map("expires_at")
178+
179+
@@map("session")
180+
}

src/auth.config.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import dotenv from "dotenv";
2+
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
3+
import { Strategy as GithubStrategy } from "passport-github2";
4+
import { prisma } from "./db.config.js";
5+
6+
dotenv.config();
7+
8+
export const googleStrategy = new GoogleStrategy(
9+
{
10+
clientID: process.env.PASSPORT_GOOGLE_CLIENT_ID,
11+
clientSecret: process.env.PASSPORT_GOOGLE_CLIENT_SECRET,
12+
callbackURL: process.env.PASSPORT_GOOGLE_CALLBACK_URL,
13+
scope: ["email", "profile"],
14+
state: true,
15+
},
16+
(accessToken, refreshToken, profile, cb) => {
17+
return googleVerify(profile)
18+
.then((user) => cb(null, user))
19+
.catch((err) => cb(err));
20+
}
21+
);
22+
23+
24+
const googleVerify = async (profile) => {
25+
const email = profile.emails?.[0]?.value;
26+
if (!email) {
27+
throw new Error(`profile.email was not found: ${profile}`);
28+
}
29+
30+
const user = await prisma.user.findFirst({ where: { email } });
31+
if (user !== null) {
32+
return { id: user.id, email: user.email, name: user.name };
33+
}
34+
35+
const created = await prisma.user.create({
36+
data: {
37+
email,
38+
name: profile.displayName,
39+
gender: "non",
40+
birth: new Date(1970, 0, 1),
41+
address: "non",
42+
detailAddress: "non",
43+
phoneNumber: "non",
44+
},
45+
});
46+
47+
return { id: created.id, email: created.email, name: created.name };
48+
};
49+
50+
51+
//githubVeirify 함수를 만들어서 사용자 정보를 저장하거나 로직을 처리!!!!!!!!!!!!!!!!!
52+
export const githubStrategy = new GithubStrategy(
53+
{
54+
clientID: process.env.PASSPORT_GITHUB_CLIENT_ID,
55+
clientSecret: process.env.PASSPORT_GITHUB_CLIENT_SECRET,
56+
callbackURL: process.env.PASSPORT_GITHUB_CALLBACK_URL,
57+
scope: ["email", "profile"],
58+
state: true,
59+
},
60+
(accessToken, refreshToken, profile, cb) => {
61+
return githubVeirify(profile)
62+
.then((user) => cb(null, user))
63+
.catch((err) => cb(err));
64+
}
65+
);
66+
67+
const githubVeirify = async (profile) => {
68+
const email = profile.emails?.[0]?.value;
69+
if (!email) {
70+
throw new Error(`Github email is not public or accessible: ${JSON.stringify(profile)}`);
71+
}
72+
73+
const user = await prisma.user.findFirst({ where: { email } });
74+
if (user !== null) {
75+
return { id: user.id, email: user.email, name: user.name };
76+
}
77+
78+
const created = await prisma.user.create({
79+
data: {
80+
email,
81+
name: profile.displayName,
82+
gender: "non",
83+
birth: new Date(1970, 0, 1),
84+
address: "non",
85+
detailAddress: "non",
86+
phoneNumber: "non",
87+
},
88+
});
89+
90+
return { id: created.id, email: created.email, name: created.name };
91+
};

0 commit comments

Comments
 (0)