Skip to content

Commit fc53d20

Browse files
committed
refactor: dashboardClient service(타입단언 수정(get으로 일반객체로 불러오기, ) ), controller(email, domain을 session에 저장), 미들웨어 session 생성
1 parent ccd0ef5 commit fc53d20

File tree

8 files changed

+106
-530
lines changed

8 files changed

+106
-530
lines changed

package-lock.json

Lines changed: 73 additions & 515 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@types/cookie-parser": "^1.4.8",
1818
"@types/cors": "^2.8.17",
1919
"@types/express": "^5.0.0",
20+
"@types/express-session": "^1.18.1",
2021
"@types/node": "^22.13.5",
2122
"eslint": "^9.21.0",
2223
"eslint-config-prettier": "^10.0.1",

src/controllers/dashboardController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,9 @@ export const dashboardController = {
189189
loginClient: async (req: Request, res: Response, next: NextFunction) => {
190190
try {
191191
const { email, password } = req.body;
192-
const clientDomain = await dashboardClientService.loginClient(email, password);
193-
res.status(200).json({ message: '로그인 성공', clientDomain });
192+
const domain = await dashboardClientService.loginClient(email, password);
193+
req.session.client = { email, domain };
194+
res.status(200).json({ message: '로그인 성공' });
194195
} catch (err) {
195196
next(err);
196197
}

src/middleware/session.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import session from 'express-session';
2+
3+
export function createSession() {
4+
return session({
5+
secret: process.env.SESSION_SECRET as string,
6+
resave: false,
7+
saveUninitialized: false,
8+
cookie: { secure: false, httpOnly: true },
9+
});
10+
}

src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import express from 'express';
44
import { UUIDV4 } from 'sequelize';
55
import { authenticateAPIKey } from './middleware/authenticateAPIKey';
66
import { errorHandle } from './middleware/errorHandle';
7+
import { createSession } from './middleware/session';
78
import { dashboardRouter } from './routes/dashboardRoutes';
89
import { trackerSdkRouter } from './routes/trackerSdkRoutes';
9-
1010
const app = express();
1111
const port = 3000;
1212

1313
app.use(authenticateAPIKey);
14+
app.use(createSession());
1415
app.use(
1516
cors({
1617
origin: 'http://client-tracker-sdk',
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import bcrypt from 'bcryptjs';
22
import * as crypto from 'crypto';
33
import { DashboardClientModel } from '../models/dashboardClientModel';
4-
import { ClientType } from '../types/dashboardClientType';
54
export const dashboardClientService = {
65
enrollClient: async (email: string, password: string, domain: string) => {
76
const apiKey = crypto.randomBytes(32).toString('hex');
@@ -17,18 +16,18 @@ export const dashboardClientService = {
1716
},
1817

1918
loginClient: async (email: string, password: string) => {
20-
const client = (await DashboardClientModel.findOne({
19+
const client = await DashboardClientModel.findOne({
2120
where: { email },
2221
attributes: ['hashedPassword', 'domain'],
23-
raw: true,
24-
})) as ClientType | null;
22+
});
2523
if (!client) {
26-
throw new Error('등록된 유저가 아닙니다.');
24+
throw new Error('로그인 에러');
2725
}
28-
const isValidPassword = await bcrypt.compare(password, client.hashedPassword);
26+
const clientData: { hashedPassword: string; domain: string } = client.get({ plain: true });
27+
const isValidPassword = await bcrypt.compare(password, clientData.hashedPassword);
2928
if (!isValidPassword) {
30-
throw new Error('비밀번호가 올바르지 않습니다.');
29+
throw new Error('로그인 에러');
3130
}
32-
return client.domain;
31+
return clientData.domain;
3332
},
3433
};

src/types/dashboardClientType.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/types/global.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'express-session';
2+
3+
declare module 'express-session' {
4+
interface SessionData {
5+
client?: {
6+
email: string;
7+
domain: string;
8+
};
9+
}
10+
}

0 commit comments

Comments
 (0)