-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground-auth.js
More file actions
138 lines (120 loc) · 3.35 KB
/
Copy pathbackground-auth.js
File metadata and controls
138 lines (120 loc) · 3.35 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Background script authentication handler for ContextZero
* Listens for authentication events and updates extension state
*/
// Listen for authentication messages
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.type) {
case 'CONTEXTZERO_AUTH_SUCCESS':
handleAuthSuccess(message.data);
break;
case 'CONTEXTZERO_AUTH_CLEARED':
handleAuthCleared();
break;
case 'CONTEXTZERO_AUTH_TIMEOUT':
handleAuthTimeout(message.error);
break;
}
});
/**
* Handle successful authentication
*/
function handleAuthSuccess(authData) {
console.log('ContextZero: Authentication successful', authData.email);
// Update extension badge to show authenticated state
chrome.action.setBadgeText({ text: '' });
chrome.action.setBadgeBackgroundColor({ color: '#00AA00' });
// Update extension icon if needed
chrome.action.setTitle({
title: `ContextZero - Signed in as ${authData.email}`
});
// Notify all tabs about authentication
chrome.tabs.query({}, (tabs) => {
tabs.forEach(tab => {
if (tab.id) {
chrome.tabs.sendMessage(tab.id, {
type: 'CONTEXTZERO_AUTH_UPDATE',
isAuthenticated: true,
authData
}).catch(() => {
// Tab might not have content script
});
}
});
});
}
/**
* Handle authentication cleared/sign out
*/
function handleAuthCleared() {
console.log('ContextZero: Authentication cleared');
// Update extension badge
chrome.action.setBadgeText({ text: '!' });
chrome.action.setBadgeBackgroundColor({ color: '#FF0000' });
// Update extension icon title
chrome.action.setTitle({
title: 'ContextZero - Click to sign in'
});
// Notify all tabs
chrome.tabs.query({}, (tabs) => {
tabs.forEach(tab => {
if (tab.id) {
chrome.tabs.sendMessage(tab.id, {
type: 'CONTEXTZERO_AUTH_UPDATE',
isAuthenticated: false
}).catch(() => {
// Tab might not have content script
});
}
});
});
}
/**
* Handle authentication timeout
*/
function handleAuthTimeout(error) {
console.error('ContextZero: Authentication timeout', error);
// Show notification if available
if (chrome.notifications) {
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon128.png',
title: 'ContextZero Authentication',
message: error || 'Authentication timed out. Please try again.'
});
}
}
/**
* Check auth status on startup
*/
async function checkAuthOnStartup() {
try {
const result = await chrome.storage.local.get(['clerk_auth_data']);
const authData = result.clerk_auth_data;
if (authData && authData.token) {
// Check if expired
if (authData.expiresAt) {
const expiresAt = new Date(authData.expiresAt);
if (expiresAt > new Date()) {
handleAuthSuccess(authData);
return;
}
}
}
// Not authenticated
handleAuthCleared();
} catch (error) {
console.error('ContextZero: Error checking auth on startup:', error);
handleAuthCleared();
}
}
// Check auth status when extension starts
checkAuthOnStartup();
// Export for testing
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
handleAuthSuccess,
handleAuthCleared,
handleAuthTimeout
};
}