Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 64 additions & 10 deletions controller/board.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,78 @@
import { Request, Response } from 'express';
import { Boards } from '../src/db/models/board';
import { Users } from '../src/db/models/user';

const boardController = {
boardAll: (req: Request, res: Response) => {
boardAll: async (req: Request, res: Response) => {
// 게시판 글 목록 데이터 보내주기
console.log('💜boardAll');
let boardList = await Boards.findAll();
res.status(200).json({
boardList,
});
},
boardOne: (req: Request, res: Response) => {
boardOne: async (req: Request, res: Response) => {
// 게시글 상세 내용 + 댓글 데이터 보내주기
console.log('💜boardOne ', req.params);
const board_id = Number(req.params.board_id);
const boardData = await Boards.findOne({
where: {
id: board_id,
},
});
if (boardData === null) {
res.status(403).json({
message: 'no board data Error!',
});
} else {
// board_id를 key로 가지는 칸반보드 데이터 불러오기
// board_id를 key로 가지는 댓글 데이터 불러오기
res.status(200).json({
...boardData, // 여기 좀더 고민
//content
//comment
});
}
},
boardAdd: (req: Request, res: Response) => {
boardAdd: async (req: Request, res: Response) => {
// 게시글 등록하기
console.log('💜boardAdd ', req.body, req.user_email, req.user_id);
const title = req.body.title;
if (title !== '') {
const writer = req.user_email;
const user_id = req.user_id;
const newBoard = await Boards.create({
id: undefined,
title,
writer,
user_id,
});
const board_id = newBoard.get('id');
// board_id를 key로 가지는 칸반보드 데이터 저장
// board_id를 key로 가지는 댓글 데이터 저장(빈파일? 생성)
res.status(200).json({
board_id,
});
} else {
res.status(400).json({
message: 'no input title Error!',
});
}
},
boardDelete: (req: Request, res: Response) => {
boardDelete: async (req: Request, res: Response) => {
// 게시글 삭제하기
},
commentAdd: (req: Request, res: Response) => {
// 댓글 추가하기
},
commentDelete: (req: Request, res: Response) => {
// 댓글 삭제하기
console.log('💜boardDelete ', req.params);
const board_id = Number(req.params.board_id);
await Boards.destroy({
where: {
id: board_id,
},
});
// board_id를 key로 가지는 칸반보드 데이터 삭제
// board_id를 key로 가지는 댓글 데이터 삭제
res.status(200).json({
message: `delete ${board_id} complete`,
});
},
};

Expand Down
13 changes: 13 additions & 0 deletions controller/board_comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Request, Response } from 'express';
import { Boards } from '../src/db/models/board';

const commentController = {
commentAdd: (req: Request, res: Response) => {
// 댓글 추가하기
},
commentDelete: (req: Request, res: Response) => {
// 댓글 삭제하기
},
};

export { commentController };
32 changes: 30 additions & 2 deletions middleware/authChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import jwt, { VerifyErrors } from 'jsonwebtoken';
import axios from 'axios';
import { Request, Response, NextFunction } from 'express';
import { accessTokenGenerator } from '../Auth/GenerateAccessToken';
import { Users } from '../src/db/models/user';
import * as dotenv from 'dotenv';
dotenv.config();

Expand Down Expand Up @@ -40,6 +41,7 @@ export const authChecker = async (req: Request, res: Response, next: NextFunctio
const email = decoded.email;
const newAccessToken = await accessTokenGenerator(id, email);
req.newAccessToken = newAccessToken;
req.user_id = id;
req.user_email = email;
}
},
Expand Down Expand Up @@ -92,7 +94,19 @@ export const authChecker = async (req: Request, res: Response, next: NextFunctio
console.log(err.message);
res.redirect(`${process.env.CLIENT_URL}/login`);
});
req.user_email = resInfo;
const userInfo = await Users.findOne({
where: {
email: resInfo,
},
});
if (userInfo !== null) {
console.log('find userInfo', userInfo);
req.user_email = resInfo;
req.user_id = userInfo.get('id') as number;
} else {
// 유저 정보를 찾을 수 없음 -> 인증 불가 -> 다시 로그인해야함
res.redirect(`${process.env.CLIENT_URL}/login`);
}
} else if (LoginType === 'github') {
// 로그인 방식 - github
// refresh token이 없음, 로그아웃 하기 전까지 access token 계속 사용 가능
Expand All @@ -114,11 +128,25 @@ export const authChecker = async (req: Request, res: Response, next: NextFunctio
console.log(err.message);
res.redirect(`${process.env.CLIENT_URL}/login`);
});
req.user_email = `${resInfo}@github.com`;
const email = `${resInfo}@github.com`;
const userInfo = await Users.findOne({
where: {
email,
},
});
if (userInfo !== null) {
console.log('find userInfo', userInfo);
req.user_email = email;
req.user_id = userInfo.get('id') as number;
} else {
// 유저 정보를 찾을 수 없음 -> 인증 불가 -> 다시 로그인해야함
res.redirect(`${process.env.CLIENT_URL}/login`);
}
}
// 실제 요청으로 넘어감
// req.user_email: 유저 이메일 정보 저장, 실제 요청에서 사용 가능
// 나중에 응답 보낼때 accessToken에 req.newAccessToken을 넣어주면 됨
console.log('💖authChecker ', LoginType, req.user_id, req.user_email, req.newAccessToken);
next();
} else {
// access token이 없을 때 -> 로그인 페이지로 돌아감
Expand Down
19 changes: 12 additions & 7 deletions routes/board.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import app from '../app';
import { authChecker } from './../middleware/authChecker';
import { boardController } from '../controller/board';
import { commentController } from '../controller/board_comment';
import express from 'express';
const boardRouter = express.Router();
// 실제 요청 처리하기 전 access token 확인
boardRouter.use('/board', authChecker);

// authChecker - 실제 요청 처리하기 전 access token 확인
//boardRouter.use('/board', authChecker);

// 게시판 글 목록 데이터 보내주기
boardRouter.get('/board', boardController.boardAll);
boardRouter.get('/board', authChecker, boardController.boardAll);

// 게시글 등록하기
boardRouter.post('/board', authChecker, boardController.boardAdd);

// 게시글 상세 내용 + 댓글 데이터 보내주기
boardRouter.get('/board/:board_id', boardController.boardOne);
boardRouter.get('/board/:board_id', authChecker, boardController.boardOne);

// 게시글 삭제하기
boardRouter.delete('/board/:board_id', boardController.boardDelete);
boardRouter.delete('/board/:board_id', authChecker, boardController.boardDelete);

// 댓글 추가하기
boardRouter.post('/board/:board_id/comment', boardController.commentAdd);
boardRouter.post('/board/:board_id/comment', authChecker, commentController.commentAdd);

// 댓글 삭제하기
boardRouter.delete('/board/:board_id/:comment_id', boardController.commentDelete);
boardRouter.delete('/board/:board_id/:comment_id', authChecker, commentController.commentDelete);

export default boardRouter;
2 changes: 2 additions & 0 deletions routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { emailAuthController } from '../controller/emailauth';
import { oauthController } from '../controller/loginOAuth';
import { userController } from '../controller/user';
const userRouter = express.Router();

// 로그인
userRouter.post('/login', userController.login);
// 로그인 - nodemailer
userRouter.post('/mail', emailController);
userRouter.post('/emailauth', emailAuthController.authorizationCode);
// 로그인 - OAuth 방식: google, github
Expand Down
1 change: 1 addition & 0 deletions routes/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { authChecker } from './../middleware/authChecker';
import { workspaceController } from '../controller/workspace';
import express from 'express';
const workspaceRouter = express.Router();

// 실제 요청 처리하기 전 access token 확인
workspaceRouter.use('/workspace', authChecker);

Expand Down
3 changes: 2 additions & 1 deletion src/customType/express.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export declare global {
namespace Express {
interface Request {
newAccessToken?: string;
user_email?: string;
user_id?: number | undefined;
user_email?: string | undefined;
}
}
}
14 changes: 13 additions & 1 deletion src/db/models/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ import {
import { sequelize } from './index';
import { Users } from './user';
interface BoardAttributes {
id: number | undefined;
writer: string | undefined;
title: string;
user_id: number | undefined;
}

export class Boards extends Model<BoardAttributes> {
public readonly id!: number;
public writer!: string;
public title!: string;
static associations: {
boardBelongsToUser: Association<Boards, Users>;
Expand All @@ -26,7 +31,14 @@ export class Boards extends Model<BoardAttributes> {
}
Boards.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
writer: DataTypes.STRING,
title: DataTypes.STRING,
user_id: DataTypes.INTEGER,
},
{
sequelize,
Expand All @@ -35,7 +47,7 @@ Boards.init(
);

Boards.belongsTo(Users, {
foreignKey: 'writer',
foreignKey: 'user_id',
targetKey: 'id',
});

Expand Down