Skip to content

Commit 54159cf

Browse files
authored
Merge pull request #127 from UMCIGNAL/main
Main
2 parents e05a294 + fce0f58 commit 54159cf

5 files changed

Lines changed: 178 additions & 6 deletions

File tree

dist/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ app.use(express.json());
2525
app.use(express.urlencoded({ extended: true }));
2626
app.use(morgan('dev'));
2727

28-
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
28+
app.use('/swagger-api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
2929

3030
router.get('/', (req, res) => {
3131
res.json('Wellcome UMCignal');

dist/user/user.controller/user.controller.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response, NextFunction } from 'express';
22
import { emailValidation, gmailValidation } from '../../security/validation/validation';
3-
import { changeUserInfoService, getMyInstService, mailVerifyService, sendMailCodeService, userLogOutService, userSignOutService, userSignupService } from '../user.service/user.service';
3+
import { changeUserInfoService, getMyInstService, mailVerifyService, mailVerifyStudentIdService, sendMailCodeService, userLogOutService, userSignOutService, userSignupService } from '../user.service/user.service';
44
import { userChangeInfoDTO, UserDto } from '../user.dto/user.dto';
55
import { decodeTokenUserId } from '../../security/JWT/auth.jwt';
66
import { check_token, come_back_user } from '../../middlware/softDelete';
@@ -268,7 +268,9 @@ export const getMyInstController = async (
268268
):Promise<any> => {
269269
try {
270270
const token = req.headers.authorization?.split(' ')[1];
271-
271+
272+
const user_id = decodeTokenUserId(token) as number;
273+
272274
if(!token || token === undefined) {
273275
return res.status(404).json({ message: '토큰이 없습니다.' });
274276
}
@@ -292,4 +294,43 @@ export const getMyInstController = async (
292294
console.error(error);
293295
return res.status(500).json({ message: '서버 오류가 발생했습니다.' });
294296
}
297+
};
298+
299+
export const mailVerifyStudentIdController = async (
300+
req : Request,
301+
res : Response,
302+
next : NextFunction
303+
):Promise<any> => {
304+
try {
305+
const mailVerification = req.body.mailVerification as string;
306+
307+
if (!mailVerification) {
308+
return res.status(401).json({ message: '인증 코드가 제공되지 않았습니다.' });
309+
}
310+
311+
const student_id = req.body.student_id as string;
312+
313+
if(!student_id) {
314+
return res.status(401).json({ message: '학번이(가) 제공되지 않았습니다.' });
315+
}
316+
317+
// 이메일 인증 코드 검증
318+
if(mailVerification.length !== 6) {
319+
return res.status(408).json({ message: '인증 코드는 6자리입니다.' });
320+
}
321+
322+
const token = await mailVerifyStudentIdService(student_id, mailVerification);
323+
324+
if(!token) {
325+
return res.status(403).json({ message: '인증 코드가 올바르지 않습니다. 다시 인증해주세요.' });
326+
}
327+
328+
return res.status(200).json({
329+
message: '메일 인증 완료',
330+
token: token
331+
});
332+
} catch (error : any) {
333+
console.error(error);
334+
return res.status(500).json({ message: '서버 오류가 발생했습니다.' });
335+
}
295336
};

dist/user/user.model/user.model.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,41 @@ export const getMyInstModel = async (
203203
const result = quert_result[0].instagram_id;
204204

205205
return result;
206+
};
207+
208+
209+
export const mailVerifyStudentIdModel = async (
210+
student_id : string,
211+
mailVerification: string
212+
): Promise<string | null> => {
213+
const pool = await getPool();
214+
215+
try {
216+
const query = `SELECT * FROM user WHERE student_id = ? AND valid_key = ?`;
217+
218+
const [result]: any = await pool.query(query, [student_id, mailVerification]);
219+
220+
if (result.length === 0) {
221+
return null; // 유효한 인증 코드가 없는 경우
222+
}
223+
224+
const token = await generateToken(result[0]); // JWT 토큰 생성
225+
226+
if (!token) {
227+
console.error('Token generation failed');
228+
return null;
229+
}
230+
231+
console.log('Token generated:', token);
232+
233+
const updateQuery = `UPDATE user SET Token = ? WHERE user_id = ?`;
234+
235+
// 토큰 값과 user_id가 정확하게 들어왔는지 확인
236+
await pool.query(updateQuery, [token, result[0].user_id]);
237+
238+
return token;
239+
} catch (error) {
240+
return null;
241+
}
242+
206243
};

dist/user/user.router.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from 'express';
2-
import { changeUserInfoController, getMyInstController, mailVerifyController, sendMailCodeController, userLogOutController, userSignOutController, userSignupController, userWhoCameBackController } from './user.controller/user.controller';
2+
import { changeUserInfoController, getMyInstController, mailVerifyController, mailVerifyStudentIdController, sendMailCodeController, userLogOutController, userSignOutController, userSignupController, userWhoCameBackController } from './user.controller/user.controller';
33
import { authenticateToken } from '../security/JWT/auth.jwt';
44

55
const router = express.Router();
@@ -10,6 +10,7 @@ router.get('/', (req, res) => {
1010

1111
router.post('/mailCode', sendMailCodeController); // 처음 로그인 시 사용 또는 다시 로그인 시도 시 사용
1212
router.post('/verify', mailVerifyController); // 인증 코드 검증 및 토큰 발급
13+
router.post('/verifyStudentId', mailVerifyStudentIdController); // 인증 코드 검증 및 토큰 발급
1314
router.patch('/signup', authenticateToken, userSignupController); // 이미 가입한 회원 초기 정보 입력
1415

1516
router.patch('/logOut', authenticateToken, userLogOutController); // 로그아웃 처리
@@ -521,4 +522,88 @@ router.get('/getMyIns', authenticateToken, getMyInstController);
521522
* message:
522523
* type: string
523524
*/
525+
526+
527+
/**
528+
* @swagger
529+
* /user/verifyStudentId:
530+
* post:
531+
* summary: 학생 메일 인증 코드 검증 및 토큰 발급
532+
* description: 클라이언트에서 받은 인증 코드와 학번을 검증하여 성공 시 토큰을 발급합니다.
533+
* tags:
534+
* - user
535+
* requestBody:
536+
* required: true
537+
* content:
538+
* application/json:
539+
* schema:
540+
* type: object
541+
* required:
542+
* - mailVerification
543+
* - student_id
544+
* properties:
545+
* mailVerification:
546+
* type: string
547+
* example: "123456"
548+
* description: 인증 메일로 받은 6자리 코드
549+
* student_id:
550+
* type: string
551+
* example: "202312345"
552+
* description: 사용자의 학번
553+
* responses:
554+
* 200:
555+
* description: 인증 성공 및 토큰 발급
556+
* content:
557+
* application/json:
558+
* schema:
559+
* type: object
560+
* properties:
561+
* message:
562+
* type: string
563+
* example: "메일 인증 완료"
564+
* token:
565+
* type: string
566+
* example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
567+
* 401:
568+
* description: 인증 코드 또는 학번 누락
569+
* content:
570+
* application/json:
571+
* schema:
572+
* type: object
573+
* properties:
574+
* message:
575+
* type: string
576+
* example: "인증 코드가 제공되지 않았습니다."
577+
* 403:
578+
* description: 잘못된 인증 코드
579+
* content:
580+
* application/json:
581+
* schema:
582+
* type: object
583+
* properties:
584+
* message:
585+
* type: string
586+
* example: "인증 코드가 올바르지 않습니다. 다시 인증해주세요."
587+
* 408:
588+
* description: 인증 코드가 6자리가 아님
589+
* content:
590+
* application/json:
591+
* schema:
592+
* type: object
593+
* properties:
594+
* message:
595+
* type: string
596+
* example: "인증 코드는 6자리입니다."
597+
* 500:
598+
* description: 서버 내부 오류
599+
* content:
600+
* application/json:
601+
* schema:
602+
* type: object
603+
* properties:
604+
* message:
605+
* type: string
606+
* example: "서버 오류가 발생했습니다."
607+
*/
608+
524609
export default router;

dist/user/user.service/user.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { generateCode, sendEmail } from "../../security/mail/mail.sender";
2-
import { changeUserInfoModel, getMyInstModel, mailVerifyModel, sendMailModel, userLogOutModel, userSignOutModel, userSignupModel } from "../user.model/user.model";
2+
import { changeUserInfoModel, getMyInstModel, mailVerifyModel, mailVerifyStudentIdModel, sendMailModel, userLogOutModel, userSignOutModel, userSignupModel } from "../user.model/user.model";
33
import { userChangeInfoDTO, UserDto } from "../user.dto/user.dto";
44

55

@@ -60,4 +60,13 @@ export const getMyInstService = async (
6060
):Promise<String> => {
6161
const result = await getMyInstModel (user_id);
6262
return result;
63-
}
63+
};
64+
65+
export const mailVerifyStudentIdService = async (
66+
student_id : string,
67+
mailVerification : string
68+
): Promise<string | null> => {
69+
// 인증 코드 검증
70+
const token = await mailVerifyStudentIdModel(student_id, mailVerification);
71+
return token;
72+
};

0 commit comments

Comments
 (0)