Skip to content

Commit 203dfde

Browse files
committed
Update deprecated msal-browser version to the latest
1 parent febc9ab commit 203dfde

File tree

3 files changed

+92
-53
lines changed

3 files changed

+92
-53
lines changed

package-lock.json

Lines changed: 22 additions & 0 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
@@ -51,6 +51,7 @@
5151
},
5252
"type": "module",
5353
"dependencies": {
54+
"@azure/msal-browser": "^4.5.0",
5455
"@codemirror/lang-javascript": "^6.2.2",
5556
"@codemirror/lang-python": "^6.1.6",
5657
"@codemirror/language-data": "^6.5.1",

src/lib/utils/onedrive-file-picker.ts

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
let CLIENT_ID = '';
1+
import { PublicClientApplication } from '@azure/msal-browser';
2+
import type { PopupRequest } from '@azure/msal-browser';
3+
4+
let CLIENT_ID = '521ada3e-6154-4a35-b9d3-51faac8ac944';
25

36
async function getCredentials() {
47
if (CLIENT_ID) return;
8+
59
const response = await fetch('/api/config');
610
if (!response.ok) {
711
throw new Error('Failed to fetch OneDrive credentials');
@@ -13,63 +17,69 @@ async function getCredentials() {
1317
}
1418
}
1519

16-
function loadMsalScript(): Promise<void> {
17-
return new Promise((resolve, reject) => {
18-
const win = window;
19-
if (win.msal) {
20-
resolve();
21-
return;
22-
}
23-
const script = document.createElement('script');
24-
script.src = 'https://alcdn.msauth.net/browser/2.19.0/js/msal-browser.min.js';
25-
script.async = true;
26-
script.onload = () => resolve();
27-
script.onerror = () => reject(new Error('Failed to load MSAL script'));
28-
document.head.appendChild(script);
29-
});
30-
}
3120

32-
let msalInstance: any;
21+
let msalInstance: PublicClientApplication | null = null;
3322

3423
// Initialize MSAL authentication
3524
async function initializeMsal() {
36-
if (!CLIENT_ID) {
37-
await getCredentials();
38-
}
39-
const msalParams = {
40-
auth: {
41-
authority: 'https://login.microsoftonline.com/consumers',
42-
clientId: CLIENT_ID
43-
}
44-
};
4525
try {
46-
await loadMsalScript();
47-
const win = window;
48-
msalInstance = new win.msal.PublicClientApplication(msalParams);
49-
if (msalInstance.initialize) {
50-
await msalInstance.initialize();
26+
if (!CLIENT_ID) {
27+
await getCredentials();
28+
}
29+
30+
const msalParams = {
31+
auth: {
32+
authority: 'https://login.microsoftonline.com/consumers',
33+
clientId: CLIENT_ID
34+
}
35+
};
36+
37+
if (!msalInstance) {
38+
msalInstance = new PublicClientApplication(msalParams);
39+
if (msalInstance.initialize) {
40+
await msalInstance.initialize();
41+
}
5142
}
43+
44+
return msalInstance;
5245
} catch (error) {
53-
console.error('MSAL initialization error:', error);
46+
throw new Error('MSAL initialization failed: ' + (error instanceof Error ? error.message : String(error)));
5447
}
5548
}
5649

5750
// Retrieve OneDrive access token
5851
async function getToken(): Promise<string> {
59-
const authParams = { scopes: ['OneDrive.ReadWrite'] };
52+
const authParams: PopupRequest = { scopes: ['OneDrive.ReadWrite'] };
6053
let accessToken = '';
6154
try {
62-
await initializeMsal();
55+
msalInstance = await initializeMsal();
56+
if (!msalInstance) {
57+
throw new Error('MSAL not initialized');
58+
}
59+
6360
const resp = await msalInstance.acquireTokenSilent(authParams);
6461
accessToken = resp.accessToken;
6562
} catch (err) {
66-
const resp = await msalInstance.loginPopup(authParams);
67-
msalInstance.setActiveAccount(resp.account);
68-
if (resp.idToken) {
69-
const resp2 = await msalInstance.acquireTokenSilent(authParams);
70-
accessToken = resp2.accessToken;
63+
if (!msalInstance) {
64+
throw new Error('MSAL not initialized');
65+
}
66+
67+
try {
68+
const resp = await msalInstance.loginPopup(authParams);
69+
msalInstance.setActiveAccount(resp.account);
70+
if (resp.idToken) {
71+
const resp2 = await msalInstance.acquireTokenSilent(authParams);
72+
accessToken = resp2.accessToken;
73+
}
74+
} catch (popupError) {
75+
throw new Error('Failed to login: ' + (popupError instanceof Error ? popupError.message : String(popupError)));
7176
}
7277
}
78+
79+
if (!accessToken) {
80+
throw new Error('Failed to acquire access token');
81+
}
82+
7383
return accessToken;
7484
}
7585

@@ -95,6 +105,7 @@ const params = {
95105
}
96106
};
97107

108+
98109
// Download file from OneDrive
99110
async function downloadOneDriveFile(fileInfo: any): Promise<Blob> {
100111
const accessToken = await getToken();
@@ -119,6 +130,8 @@ async function downloadOneDriveFile(fileInfo: any): Promise<Blob> {
119130
return await downloadResponse.blob();
120131
}
121132

133+
// OneDrive 피커 결과 인터페이스 정의
134+
122135
// Open OneDrive file picker and return selected file metadata
123136
export async function openOneDrivePicker(): Promise<any | null> {
124137
if (typeof window === 'undefined') {
@@ -162,7 +175,6 @@ export async function openOneDrivePicker(): Promise<any | null> {
162175
throw new Error('Could not retrieve auth token');
163176
}
164177
} catch (err) {
165-
console.error(err);
166178
channelPort?.postMessage({
167179
result: 'error',
168180
error: { code: 'tokenError', message: 'Failed to get token' },
@@ -187,7 +199,6 @@ export async function openOneDrivePicker(): Promise<any | null> {
187199
break;
188200
}
189201
default: {
190-
console.warn('Unsupported command:', command);
191202
channelPort?.postMessage({
192203
result: 'error',
193204
error: { code: 'unsupportedCommand', message: command.command },
@@ -218,14 +229,17 @@ export async function openOneDrivePicker(): Promise<any | null> {
218229
if (!authToken) {
219230
return reject(new Error('Failed to acquire access token'));
220231
}
232+
221233
pickerWindow = window.open('', 'OneDrivePicker', 'width=800,height=600');
222234
if (!pickerWindow) {
223235
return reject(new Error('Failed to open OneDrive picker window'));
224236
}
237+
225238
const queryString = new URLSearchParams({
226239
filePicker: JSON.stringify(params)
227240
});
228241
const url = `${baseUrl}?${queryString.toString()}`;
242+
229243
const form = pickerWindow.document.createElement('form');
230244
form.setAttribute('action', url);
231245
form.setAttribute('method', 'POST');
@@ -234,11 +248,15 @@ export async function openOneDrivePicker(): Promise<any | null> {
234248
input.setAttribute('name', 'access_token');
235249
input.setAttribute('value', authToken);
236250
form.appendChild(input);
251+
237252
pickerWindow.document.body.appendChild(form);
238253
form.submit();
254+
239255
window.addEventListener('message', handleWindowMessage);
240256
} catch (err) {
241-
if (pickerWindow) pickerWindow.close();
257+
if (pickerWindow) {
258+
pickerWindow.close();
259+
}
242260
reject(err);
243261
}
244262
};
@@ -249,18 +267,16 @@ export async function openOneDrivePicker(): Promise<any | null> {
249267

250268
// Pick and download file from OneDrive
251269
export async function pickAndDownloadFile(): Promise<{ blob: Blob; name: string } | null> {
252-
try {
253-
const pickerResult = await openOneDrivePicker();
254-
if (!pickerResult || !pickerResult.items || pickerResult.items.length === 0) {
255-
return null;
256-
}
257-
const selectedFile = pickerResult.items[0];
258-
const blob = await downloadOneDriveFile(selectedFile);
259-
return { blob, name: selectedFile.name };
260-
} catch (error) {
261-
console.error('Error occurred during OneDrive file pick/download:', error);
262-
throw error;
270+
const pickerResult = await openOneDrivePicker();
271+
272+
if (!pickerResult || !pickerResult.items || pickerResult.items.length === 0) {
273+
return null;
263274
}
275+
276+
const selectedFile = pickerResult.items[0];
277+
const blob = await downloadOneDriveFile(selectedFile);
278+
279+
return { blob, name: selectedFile.name };
264280
}
265281

266282
export { downloadOneDriveFile };

0 commit comments

Comments
 (0)