-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
61 lines (54 loc) · 1.8 KB
/
Copy pathpopup.js
File metadata and controls
61 lines (54 loc) · 1.8 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
document.addEventListener('DOMContentLoaded', async () => {
showLoginContainer();
try {
const token = await getAuthToken(false);
if (token) {
showLogoutContainer();
}
} catch (error) {
console.error('Authentication error:', error);
}
document.getElementById('loginButton').addEventListener('click', handleLogin);
document.getElementById('logoutButton').addEventListener('click', handleLogout);
});
async function handleLogin() {
try {
const token = await getAuthToken(true);
if (token) {
showLogoutContainer();
}
} catch (error) {
console.error('Login error:', error);
alert('Unable to authenticate with Google. Please try again.');
}
}
function handleLogout() {
chrome.runtime.sendMessage({action: 'removeAuthToken'}, (response) => {
if (response.success) {
showLoginContainer();
alert('Logged out successfully.');
} else {
alert('Failed to logout. Please try again.');
}
});
}
function getAuthToken(interactive = true) {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({interactive: interactive}, (token) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
reject(chrome.runtime.lastError);
} else {
resolve(token);
}
});
});
}
function showLoginContainer() {
document.getElementById('loginContainer').style.display = 'block';
document.getElementById('logoutContainer').style.display = 'none';
}
function showLogoutContainer() {
document.getElementById('loginContainer').style.display = 'none';
document.getElementById('logoutContainer').style.display = 'block';
}