-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
91 lines (62 loc) · 2.03 KB
/
Copy pathutils.js
File metadata and controls
91 lines (62 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function signIn(clientId,redirectUri,scopes,response_type="token") {
let oauth2Endpoint = "https://accounts.google.com/o/oauth2/v2/auth";
let params = {
client_id: clientId,
redirect_uri: "https://bioanywhere.github.io/oauth-callback.html",
response_type: response_type,
scope: scopes,
include_granted_scopes: "true",
state: "pass-through-value",
};
for (var p in params) {
let input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", p);
input.setAttribute("value", params[p]);
// form.appendChild(input);
}
// Construct the URL for Google login
let loginUrl = oauth2Endpoint + "?" + new URLSearchParams(params).toString();
// Open the Google login page in a new popup window
const width = 600;
const height = 600;
const left = window.screen.width / 2 - width / 2;
const top = window.screen.height / 2 - height / 2;
const options = `width=${width},height=${height},left=${left},top=${top},resizable,scrollbars=yes,status=1`;
window.open(loginUrl, "_blank", options);
}
function logout(access_token, redirect_url) {
fetch("https://oauth2.googleapis.com/revoke?token=" + access_token, {
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded",
},
}).then(() => {
// Clear access token from localStorage
localStorage.removeItem("access_token");
// Redirect to the specified URL after logout
location.href = redirect_url;
});
}
function getParamsFromURL(url) {
let params = {};
let regex = /([^&=]+)=([^&]*)/g,
m;
while ((m = regex.exec(url))) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return params;
}
function saveOAuth2Info(data, path, name) {
if (Object.keys(data).length > 0) {
localStorage.setItem(name, JSON.stringify(data))
}
// hide the access token
// window.history.pushState({}, document.title, "/" + path)
}
export default{
signIn,
logout,
saveOAuth2Info,
getParamsFromURL,
}