Skip to content

Commit b238983

Browse files
committed
Refactor code
1 parent 1757a47 commit b238983

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

frontend/src/utils/collabSocket.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { io } from "socket.io-client";
33
import { updateCursor, Cursor } from "./collabCursor";
44
import { Doc, Text, applyUpdateV2 } from "yjs";
55
import { Awareness } from "y-protocols/awareness";
6+
import { getToken } from "./token";
67

78
export enum CollabEvents {
89
// Send
@@ -38,7 +39,7 @@ export const collabSocket = io(COLLAB_SOCKET_URL, {
3839
reconnectionAttempts: 3,
3940
autoConnect: false,
4041
auth: {
41-
token: `Bearer ${localStorage.getItem("token")}`,
42+
token: getToken(),
4243
},
4344
});
4445

frontend/src/utils/communicationSocket.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { io } from "socket.io-client";
2+
import { getToken } from "./token";
23

34
export enum CommunicationEvents {
45
// send
@@ -22,6 +23,6 @@ export const communicationSocket = io(COMMUNICATION_SOCKET_URL, {
2223
autoConnect: false,
2324
withCredentials: true,
2425
auth: {
25-
token: `Bearer ${localStorage.getItem("token")}`,
26+
token: getToken(),
2627
},
2728
});

frontend/src/utils/matchSocket.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { io } from "socket.io-client";
2+
import { getToken } from "./token";
23

34
const MATCH_SOCKET_URL = "http://localhost:3002";
45

56
export const matchSocket = io(MATCH_SOCKET_URL, {
67
reconnectionAttempts: 3,
78
autoConnect: false,
89
auth: {
9-
token: `Bearer ${localStorage.getItem("token")}`,
10+
token: getToken(),
1011
},
1112
});

frontend/src/utils/token.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const setToken = (token: string) => {
2+
const bearerToken = `Bearer ${token}`;
3+
localStorage.setItem("accessToken", bearerToken);
4+
};
5+
6+
export const getToken = () => {
7+
const token = localStorage.getItem("accessToken");
8+
return token;
9+
};
10+
11+
export const removeToken = () => {
12+
localStorage.removeItem("accessToken");
13+
};

0 commit comments

Comments
 (0)