Skip to content

Commit e322e69

Browse files
authored
Merge pull request #35 from codeit-moving/안재민
[안재민] 회원가입(고객,기사), 유저수정 패스워드 해싱 / 컨트롤러 이미지 처리 추가, 이메일과 전화번호 중복조회 api추가
2 parents 98108f5 + a277723 commit e322e69

File tree

4 files changed

+111
-21
lines changed

4 files changed

+111
-21
lines changed

src/controllers/authController.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Router } from "express";
22
import authService from "../services/authService";
33
import { asyncHandle } from "../utils/asyncHandler";
44
import cookieConfig from "../config/cookie.config";
5-
import createToken from "../utils/token.utils";
5+
import createToken, { Payload } from "../utils/token.utils";
66
import upload from "../utils/multer";
7+
import passport from "passport";
78

89
const router = Router();
910

@@ -75,18 +76,19 @@ router.post(
7576
upload.single("imageUrl"),
7677
asyncHandle(async (req, res, next) => {
7778
try {
78-
const SignUpCustomer: SignUpCustomer = {
79+
const signUpCustomer: SignUpCustomer = {
7980
...req.body,
8081
imageUrl: req.file!,
8182
services: Array.isArray(req.body.services)
82-
? req.body.services
83-
: JSON.parse(req.body.services),
83+
? req.body.services.map(Number)
84+
: JSON.parse(req.body.services).map(Number),
8485
regions: Array.isArray(req.body.regions)
85-
? req.body.regions
86-
: JSON.parse(req.body.regions),
86+
? req.body.regions.map(Number)
87+
: JSON.parse(req.body.regions).map(Number),
8788
isOAuth: req.body.isOAuth === "true",
8889
};
89-
await authService.signUpCustomer(SignUpCustomer);
90+
91+
await authService.signUpCustomer(signUpCustomer);
9092
res.status(204).send();
9193
} catch (error) {
9294
next(error);
@@ -99,7 +101,18 @@ router.post(
99101
upload.single("imageUrl"),
100102
asyncHandle(async (req, res, next) => {
101103
try {
102-
const SignUpMover: SignUpMover = req.body;
104+
const SignUpMover: SignUpMover = {
105+
...req.body,
106+
imageUrl: req.file!,
107+
services: Array.isArray(req.body.services)
108+
? req.body.services.map(Number)
109+
: JSON.parse(req.body.services).map(Number),
110+
regions: Array.isArray(req.body.regions)
111+
? req.body.regions.map(Number)
112+
: JSON.parse(req.body.regions).map(Number),
113+
isOAuth: req.body.isOAuth === "true",
114+
career: Number(req.body.career),
115+
};
103116
await authService.signUpMover(SignUpMover);
104117
res.status(204).send();
105118
} catch (error) {
@@ -108,4 +121,32 @@ router.post(
108121
})
109122
);
110123

124+
router.post(
125+
"/validate",
126+
asyncHandle(async (req, res, next) => {
127+
try {
128+
const { email, phoneNumber } = req.body;
129+
await authService.validate(email, phoneNumber);
130+
res.status(204).send();
131+
} catch (error) {
132+
next(error);
133+
}
134+
})
135+
);
136+
137+
router.post(
138+
"/refresh",
139+
passport.authenticate("refresh-token", { session: false }),
140+
asyncHandle(async (req, res, next) => {
141+
try {
142+
const user = req.user as Payload;
143+
const accessToken = createToken(user, "access");
144+
res.cookie("accessToken", accessToken, cookieConfig.accessTokenOption);
145+
res.status(204).send();
146+
} catch (error) {
147+
next(error);
148+
}
149+
})
150+
);
151+
111152
export default router;

src/controllers/customerController.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ router.post(
1717
const profile = {
1818
userId: userId,
1919
imageUrl: req.file!,
20-
services: Array.isArray(req.body.services)
21-
? req.body.services
22-
: JSON.parse(req.body.services),
23-
regions: Array.isArray(req.body.regions)
24-
? req.body.regions
25-
: JSON.parse(req.body.regions), //postman으로 테스트하였는데 문자 배열로 인식하는 것 같아 임시 코드 작성 수정예정
20+
regions: req.body.regions
21+
? Array.isArray(req.body.regions)
22+
? req.body.regions.map(Number)
23+
: JSON.parse(req.body.regions).map(Number)
24+
: [],
25+
services: req.body.services
26+
? Array.isArray(req.body.services)
27+
? req.body.services.map(Number)
28+
: JSON.parse(req.body.services).map(Number)
29+
: [],
2630
};
2731
await customerService.createCustomerProfile(profile);
2832
res.status(204).send();
@@ -39,7 +43,16 @@ router.patch(
3943
asyncHandle(async (req, res, next) => {
4044
try {
4145
const userId = (req.user as Payload).id;
42-
const profile = { ...req.body, imageUrl: req.file };
46+
const profile = {
47+
...req.body,
48+
imageUrl: req.file,
49+
regions: req.body.regions
50+
? JSON.parse(req.body.regions).map(Number)
51+
: [],
52+
services: req.body.services
53+
? JSON.parse(req.body.services).map(Number)
54+
: [],
55+
};
4356
await customerService.updateCustomerProfile(userId, profile);
4457
res.status(204).send();
4558
} catch (error) {

src/services/authService.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ const signIn = async ({ email, password }: SignInData) => {
4444
throw error;
4545
}
4646

47-
// const isPasswordValid = await bcrypt.compare(password, user.password!);
48-
49-
const isPasswordValid = password === user.password;
47+
const isPasswordValid = await bcrypt.compare(password, user.password!); //패스워드 검증
5048

5149
if (!isPasswordValid) {
5250
const error: CustomError = new Error("Unauthorized");
@@ -87,9 +85,12 @@ const signUpCustomer = async (customer: SignUpCustomer) => {
8785
}
8886
}
8987

88+
const hashedPassword = await bcrypt.hash(customer.password, 10);
89+
9090
const customerData = {
9191
...customer,
9292
imageUrl,
93+
password: hashedPassword,
9394
};
9495

9596
const result = await userRepository.createCustomer(customerData);
@@ -125,9 +126,12 @@ const signUpMover = async (mover: SignUpMover) => {
125126
}
126127
}
127128

129+
const hashedPassword = await bcrypt.hash(mover.password, 10);
130+
128131
const moverData = {
129132
...mover,
130133
imageUrl,
134+
password: hashedPassword,
131135
};
132136
const result = await userRepository.createMover(moverData);
133137

@@ -137,4 +141,28 @@ const signUpMover = async (mover: SignUpMover) => {
137141
}
138142
};
139143

140-
export default { signIn, signUpCustomer, signUpMover };
144+
const validate = async (email: string, phoneNumber: string) => {
145+
const user = await userRepository.existingUser(email, phoneNumber);
146+
147+
if (user) {
148+
const error: CustomError = new Error("Conflict");
149+
if (user.email === email) {
150+
error.status = 409;
151+
error.data = {
152+
message: "이미 존재하는 이메일입니다.",
153+
};
154+
throw error;
155+
}
156+
if (user.phoneNumber === phoneNumber) {
157+
error.status = 409;
158+
error.data = {
159+
message: "이미 존재하는 전화번호입니다.",
160+
};
161+
throw error;
162+
}
163+
}
164+
165+
return user;
166+
};
167+
168+
export default { signIn, signUpCustomer, signUpMover, validate };

src/services/userService.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import userRepository from "../repositorys/userRepository";
22
import CustomError from "../utils/interfaces/customError";
3+
import bcrypt from "bcrypt";
34

45
interface UpdateUser {
56
name?: string;
@@ -32,8 +33,13 @@ interface MoverResponse {
3233

3334
const updateUser = async (userId: number, updateData: UpdateUser) => {
3435
const user = await userRepository.findById(userId);
36+
const isPasswordCorrect = await bcrypt.compare(
37+
updateData.currentPassword!,
38+
user!.password!
39+
);
40+
3541
if (updateData.newPassword && updateData.currentPassword) {
36-
if (user!.password !== updateData.currentPassword) {
42+
if (!isPasswordCorrect) {
3743
const error: CustomError = new Error("Unauthorized");
3844
error.status = 401;
3945
error.data = {
@@ -52,10 +58,12 @@ const updateUser = async (userId: number, updateData: UpdateUser) => {
5258
}
5359
}
5460

61+
const hashedNewPassword = await bcrypt.hash(updateData.newPassword!, 10);
62+
5563
const updateUserData = {
5664
name: updateData.name,
5765
phoneNumber: updateData.phoneNumber,
58-
password: updateData.newPassword,
66+
password: hashedNewPassword,
5967
};
6068

6169
return await userRepository.updateUser(userId, updateUserData);

0 commit comments

Comments
 (0)