Skip to content

Commit 0512e10

Browse files
committed
refactor: replace chrome.cookies with chrome.storage for library token
1 parent 473637c commit 0512e10

2 files changed

Lines changed: 29 additions & 79 deletions

File tree

src/apis/external/library.ts

Lines changed: 21 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import {
1212

1313
const LIBRARY_BASE_URL = 'https://library.konkuk.ac.kr';
1414
const LIBRARY_API_URL = `${LIBRARY_BASE_URL}/pyxis-api`;
15-
16-
// 도서관 쿠키 관련 상수
17-
// 도서관 시스템(PYXIS)에서 요구하는 고정값으로, 실제 로그인 응답에는 포함되지 않음
18-
const COOKIE_IS_PASSWORD_EXPIRED = false; // 비밀번호 만료 여부 (항상 false)
19-
const COOKIE_CHECKSUM = 58; // 쿠키 무결성 검증용 체크섬 (고정값)
15+
const LIBRARY_TOKEN_STORAGE_KEY = 'libraryToken';
2016

2117
/**
2218
* 도서관 열람실 예약 페이지 URL 생성
@@ -150,111 +146,65 @@ export async function getLibrarySeatRoomsAPI(
150146
}
151147

152148
/**
153-
* 도서관 인증 정보를 쿠키에 저장
154-
* 웹사이트에서도 로그인 상태로 인식되도록 함
149+
* 도서관 인증 토큰을 chrome.storage에 저장
155150
* @param loginData 로그인 응답 데이터
156151
*/
157-
export async function setLibraryCookie(
152+
export async function setLibraryToken(
158153
loginData: LibraryLoginData
159154
): Promise<boolean> {
160155
try {
161-
if (typeof chrome === 'undefined' || !chrome.cookies) {
156+
if (typeof chrome === 'undefined' || !chrome.storage) {
162157
return false;
163158
}
164159

165160
// 만료 시간 설정 (현재 시간 + 1시간)
166161
const expireDate = new Date();
167162
expireDate.setHours(expireDate.getHours() + 1);
168163

169-
// 쿠키에 저장할 데이터 구성
170-
const cookieData = {
171-
id: loginData.id,
172-
accessToken: loginData.accessToken,
173-
name: loginData.name,
174-
availableHomepages: loginData.availableHomepages,
175-
disableServices: loginData.disableServices,
176-
alternativeId: loginData.alternativeId,
177-
branch: loginData.branch,
178-
memberNo: loginData.memberNo,
179-
isPasswordExpired: COOKIE_IS_PASSWORD_EXPIRED,
180-
patronType: loginData.patronType,
181-
patronState: loginData.patronState,
182-
dept: loginData.dept,
183-
checkSum: COOKIE_CHECKSUM,
184-
printMemberNo: loginData.printMemberNo,
185-
isPortalLogin: loginData.isPortalLogin,
186-
isFamilyLogin: loginData.isFamilyLogin,
187-
isPrivacyPolicyAgree: loginData.isPrivacyPolicyAgree,
188-
expireDate: expireDate.toISOString(),
189-
};
190-
191-
// URL 인코딩하여 쿠키 값 생성
192-
const cookieValue = encodeURIComponent(JSON.stringify(cookieData));
193-
194-
// 기존 쿠키 삭제 (도메인 충돌 방지)
195-
await chrome.cookies.remove({
196-
url: LIBRARY_BASE_URL,
197-
name: 'KONKUK_PYXIS3',
198-
});
199-
200-
const result = await chrome.cookies.set({
201-
url: LIBRARY_BASE_URL,
202-
name: 'KONKUK_PYXIS3',
203-
value: cookieValue,
204-
path: '/',
205-
secure: true,
206-
sameSite: 'lax',
207-
expirationDate: Math.floor(expireDate.getTime() / 1000),
164+
await chrome.storage.local.set({
165+
[LIBRARY_TOKEN_STORAGE_KEY]: {
166+
accessToken: loginData.accessToken,
167+
expireDate: expireDate.toISOString(),
168+
},
208169
});
209170

210-
if (!result) {
211-
console.error('[Library] Cookie set returned null - setting failed');
212-
return false;
213-
}
214-
215171
return true;
216172
} catch (error) {
217-
console.error('[Library] Failed to set cookie:', error);
173+
console.error('[Library] Failed to set token:', error);
218174
return false;
219175
}
220176
}
221177

222178
/**
223-
* 브라우저 쿠키에서 도서관 인증 토큰 가져오기
224-
* 사용자가 도서관 사이트에 로그인한 상태면 쿠키에 토큰이 저장되어 있음
179+
* chrome.storage에서 도서관 인증 토큰 가져오기
225180
* @returns accessToken 또는 null
226181
*/
227-
export async function getLibraryTokenFromCookie(): Promise<string | null> {
182+
export async function getLibraryTokenFromStorage(): Promise<string | null> {
228183
try {
229-
// 크롬 익스텐션 환경에서만 사용 가능
230-
if (typeof chrome === 'undefined' || !chrome.cookies) {
184+
if (typeof chrome === 'undefined' || !chrome.storage) {
231185
return null;
232186
}
233187

234-
const cookie = await chrome.cookies.get({
235-
url: LIBRARY_BASE_URL,
236-
name: 'KONKUK_PYXIS3',
237-
});
188+
const result = await chrome.storage.local.get(LIBRARY_TOKEN_STORAGE_KEY);
189+
const data = result[LIBRARY_TOKEN_STORAGE_KEY];
238190

239-
if (!cookie?.value) {
191+
if (!data?.accessToken) {
240192
return null;
241193
}
242194

243-
// URL 디코딩 후 JSON 파싱
244-
const decoded = decodeURIComponent(cookie.value);
245-
const data = JSON.parse(decoded);
246-
247195
// 만료 체크
248196
if (data.expireDate) {
249197
const expireDate = new Date(data.expireDate);
250198
if (expireDate < new Date()) {
251-
return null; // 토큰 만료됨
199+
// 만료된 토큰 삭제
200+
await chrome.storage.local.remove(LIBRARY_TOKEN_STORAGE_KEY);
201+
return null;
252202
}
253203
}
254204

255-
return data.accessToken || null;
205+
return data.accessToken;
256206
} catch (error) {
257-
console.error('[Library] Failed to get token from cookie:', error);
207+
console.error('[Library] Failed to get token from storage:', error);
258208
return null;
259209
}
260210
}

src/components/Labs/LibrarySeatSection.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Button } from '@/components/ui/button';
33
import { RefreshCw, ExternalLink, Info } from 'lucide-react';
44
import {
55
getLibrarySeatRoomsAPI,
6-
getLibraryTokenFromCookie,
6+
getLibraryTokenFromStorage,
77
libraryLoginAPI,
8-
setLibraryCookie,
8+
setLibraryToken,
99
openLibraryReservationPage,
1010
} from '@/apis';
1111
import { LibrarySeatRoom } from '@/types/api';
@@ -26,10 +26,10 @@ const LibrarySeatSection = () => {
2626
// eCampus credentials 로드
2727
const loadedCredentials = await loadECampusCredentials();
2828

29-
// 1. 먼저 쿠키에서 토큰 가져오기 시도
30-
let token = await getLibraryTokenFromCookie();
29+
// 1. 먼저 storage에서 토큰 가져오기 시도
30+
let token = await getLibraryTokenFromStorage();
3131

32-
// 2. 쿠키에 토큰이 없으면 eCampus credentials로 로그인 시도
32+
// 2. 토큰이 없으면 eCampus credentials로 로그인 시도
3333
if (!token && loadedCredentials) {
3434
const loginResponse = await libraryLoginAPI(
3535
loadedCredentials.id,
@@ -38,8 +38,8 @@ const LibrarySeatSection = () => {
3838

3939
if (loginResponse.success && loginResponse.data) {
4040
token = loginResponse.data.accessToken;
41-
// 웹사이트에서도 로그인 상태 유지되도록 쿠키 설정
42-
await setLibraryCookie(loginResponse.data);
41+
// 토큰 저장
42+
await setLibraryToken(loginResponse.data);
4343
}
4444
}
4545

@@ -102,7 +102,7 @@ const LibrarySeatSection = () => {
102102
<div className="flex items-start gap-2 rounded-lg bg-muted/50 p-3">
103103
<Info className="h-4 w-4 mt-0.5 text-muted-foreground flex-shrink-0" />
104104
<p className="text-xs text-muted-foreground leading-relaxed">
105-
좌석 현황을 보려면 할 일 탭에서 eCampus 로그인 정보를 등록해주세요.
105+
좌석 현황을 보려면 설정 탭에서 eCampus 계정 정보를 등록해주세요.
106106
</p>
107107
</div>
108108
<Button

0 commit comments

Comments
 (0)