Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: socket auth 로직 개선 #4

Merged
merged 1 commit into from
Nov 23, 2023
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
7 changes: 7 additions & 0 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ export class AppController {
getHello(): string {
return 'Hello World!';
}

@Get('/test')
test() {
// console.log(req.headers.cookie);
console.log('test');
return 'test';
}
}
8 changes: 2 additions & 6 deletions src/auth/provider/common.auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ export class AuthService {
private readonly jwtOption: JwtOption,
) {}

cookieVerify(cookie: string): ErAuth.AccessTokenSignPayload | EmsAuth.AccessTokenSignPayload {
const access_token = cookie
.split(';')
.filter((v) => v.includes('access_token'))[0]
.split('=')[1];
return this.accessTokenVerify({ access_token });
tokenVerify(token: string): ErAuth.AccessTokenSignPayload | EmsAuth.AccessTokenSignPayload {
return this.accessTokenVerify({ access_token: token });
}
accessTokenVerify({
access_token,
Expand Down
4 changes: 3 additions & 1 deletion src/consumers/req.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export class ReqConsumer {
if (diffMinutes > 5) {
return;
}
console.log(payload);
// console.log(payload);
this.reqGateway.notifyEmsToErNewRequestToEr(payload);
this.reqGateway.notifyEmsToErNewRequestToErForEms(payload);
}

/**
Expand All @@ -36,6 +37,7 @@ export class ReqConsumer {
*/
@MessagePattern(EMS_REQUEST_ER_RESPONSE)
async handleEmsToErResponse(@Payload() payload: EmsToErRequestMessage.EmsToErRes) {
console.log(payload);
this.reqGateway.notifyEmsToErReqResponseToEms(payload);
}

Expand Down
22 changes: 14 additions & 8 deletions src/gateways/req.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,27 @@ export class ReqGateway implements OnGatewayConnection, OnGatewayDisconnect, OnG
}

async handleConnection(client: Socket) {
const cookies = client.handshake.headers.cookie;
if (!cookies) {
const token: string = client.handshake.auth.token;
if (!token) {
client.disconnect();
return;
}
const isValidCookie = this.authService.cookieVerify(cookies);
if (!isValidCookie) {

const isValidToken = this.authService.tokenVerify(token);
if (!isValidToken) {
client.disconnect();
return;
}
const { type } = isValidCookie;
console.log(isValidToken);
const { type } = isValidToken;
if (type === 'ems') {
const { ambulance_company_id, employee_id } = isValidCookie;
const { ambulance_company_id, employee_id } = isValidToken;
await client.join(`request-ems-${ambulance_company_id}-${employee_id}`);
this.logger.debug(`ems request-${client.id} is connected! ${ambulance_company_id}:${employee_id}`);
}

if (type === 'er') {
const { emergency_center_id, employee_id } = isValidCookie;
const { emergency_center_id, employee_id } = isValidToken;
await client.join(`request-er-${emergency_center_id}`);
this.logger.debug(`er request-${client.id} is connected! ${emergency_center_id}:${employee_id}`);
}
Expand All @@ -62,7 +64,11 @@ export class ReqGateway implements OnGatewayConnection, OnGatewayDisconnect, OnG
notifyEmsToErNewRequestToEr(payload: EmsToErRequestMessage.EmsToErReq) {
this.server.to(`request-er-${payload.emergency_center_id}`).emit(EMS_REQUEST_ER, payload);
}

notifyEmsToErNewRequestToErForEms(payload: EmsToErRequestMessage.EmsToErReq) {
const { patient } = payload;
const { ambulance_company_id, ems_employee_id } = patient;
this.server.to(`request-ems-${ambulance_company_id}-${ems_employee_id}`).emit(EMS_REQUEST_ER, payload);
}
notifyEmsToErReqResponseToEms(payload: EmsToErRequestMessage.EmsToErRes) {
this.server
.to(`request-ems-${payload.ambulance_company_id}-${payload.ems_employee_id}`)
Expand Down
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ async function bootstrap() {
app.setGlobalPrefix('api');

app.enableCors({
origin: '*',
origin: [
'https://er.ho-it.me',
'https://ems.ho-it.me',
'http://er.development.com',
'http://ems.development.com',
'http://localhost:4000',
'http://localhost:3000',
],
credentials: true,
});

await app.startAllMicroservices();

await app.listen(port);
Expand Down