Skip to content

Commit b7af737

Browse files
authored
refactor: update Dropbox authentication handling with BroadcastChannel for improved token management (#1367)
1 parent b6fbc66 commit b7af737

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

package-lock.json

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

public/dropbox.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en" dir="ltr">
33
<head>
44
<meta charset="utf-8" />
@@ -17,7 +17,7 @@
1717
const error = params.get("error");
1818

1919
if (code) getToken();
20-
else if (error) window.opener.Cloud.providers.dropbox.returnError(params.get("error_description"));
20+
else if (error) returnError(params.get("error_description"));
2121
else startAuth();
2222

2323
function startAuth() {
@@ -31,13 +31,23 @@
3131
.catch(error => console.error(error));
3232
}
3333

34+
function returnError(description) {
35+
const channel = new BroadcastChannel("dropbox-auth");
36+
channel.postMessage({type: "error", description});
37+
channel.close();
38+
window.close();
39+
}
40+
3441
function getToken() {
3542
auth.setCodeVerifier(window.sessionStorage.getItem("codeVerifier"));
3643
auth
3744
.getAccessTokenFromCode(REDIRECT_URI, code)
3845
.then(resp => {
3946
const token = resp.result.access_token;
40-
window.opener.Cloud.providers.dropbox.setDropBoxToken(token);
47+
const channel = new BroadcastChannel("dropbox-auth");
48+
channel.postMessage({type: "token", token});
49+
channel.close();
50+
window.close();
4151
})
4252
.catch(error => {
4353
console.error(error);

public/modules/io/cloud.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,30 @@ window.Cloud = (function () {
9595
reject(new Error("Timeout. No auth for Dropbox"));
9696
}, 120 * 1000);
9797

98-
window.addEventListener("dropboxauth", e => {
98+
const channel = new BroadcastChannel("dropbox-auth");
99+
channel.onmessage = async ({data}) => {
100+
channel.close();
99101
clearTimeout(watchDog);
100-
resolve();
101-
});
102+
if (data.type === "token") {
103+
await this.setDropBoxToken(data.token);
104+
resolve();
105+
} else {
106+
this.returnError(data.description);
107+
reject(new Error(data.description));
108+
}
109+
};
102110
});
103111
},
104112

105-
// Callback function for auth window
106113
async setDropBoxToken(token) {
107114
DEBUG.cloud && console.info("Access token:", token);
108115
setToken(this.name, token);
109116
await this.connect(token);
110-
this.authWindow.close();
111-
window.dispatchEvent(new Event("dropboxauth"));
112117
},
113118

114119
returnError(errorDescription) {
115120
console.error(errorDescription);
116121
tip(errorDescription.replaceAll("+", " "), true, "error", 4000);
117-
this.authWindow.close();
118122
},
119123

120124
async getLink(path) {

0 commit comments

Comments
 (0)