Skip to content

Commit 75adf0d

Browse files
committed
fixes
1 parent 71766cd commit 75adf0d

File tree

4 files changed

+40
-57
lines changed

4 files changed

+40
-57
lines changed

src/lib/backends/google-tasks-backend.js

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TaskBackend from './task-backend.js'
2+
import { isChrome } from '../browser-detect.js'
23

34
/**
45
* Google Tasks API client for Chrome Extensions
@@ -12,32 +13,19 @@ class GoogleTasksBackendExtension extends TaskBackend {
1213

1314
this.dataKey = 'google_tasks_data'
1415
this.tasklistIdKey = 'google_tasks_default_list'
15-
this.signedInKey = 'google_tasks_signed_in'
1616
this.data = JSON.parse(localStorage.getItem(this.dataKey) ?? '{}')
1717
this.defaultTasklistId =
1818
localStorage.getItem(this.tasklistIdKey) ?? '@default'
19-
this.isSignedIn = localStorage.getItem(this.signedInKey) === 'true'
2019
this.accessToken = null
2120
this.tokenPromise = null // Prevent multiple simultaneous token requests
2221
}
2322

24-
/**
25-
* Check if Chrome Identity API is available
26-
*/
27-
isChromeIdentityAvailable() {
28-
return (
29-
typeof chrome !== 'undefined' &&
30-
chrome.identity &&
31-
chrome.identity.getAuthToken
32-
)
33-
}
34-
3523
/**
3624
* Get a valid access token using Chrome's identity API
3725
* This automatically handles token caching and refresh
3826
*/
3927
async getAuthToken(interactive = false) {
40-
if (!this.isChromeIdentityAvailable()) {
28+
if (!isChrome()) {
4129
throw new Error(
4230
'Chrome identity API not available. Google Tasks only works in Chrome.'
4331
)
@@ -55,8 +43,6 @@ class GoogleTasksBackendExtension extends TaskBackend {
5543
scopes: this.scopes,
5644
},
5745
(token) => {
58-
this.tokenPromise = null
59-
6046
if (chrome.runtime.lastError) {
6147
reject(new Error(chrome.runtime.lastError.message))
6248
return
@@ -73,37 +59,33 @@ class GoogleTasksBackendExtension extends TaskBackend {
7359
)
7460
})
7561

76-
return this.tokenPromise
62+
try {
63+
return await this.tokenPromise
64+
} finally {
65+
this.tokenPromise = null
66+
}
7767
}
7868

7969
/**
8070
* Sign in using Chrome's identity API
8171
* This handles OAuth flow automatically and keeps users signed in
8272
*/
8373
async signIn() {
84-
if (!this.isChromeIdentityAvailable()) {
74+
if (!isChrome()) {
8575
throw new Error(
8676
'Chrome identity API not available. Google Tasks only works in Chrome.'
8777
)
8878
}
8979

90-
try {
91-
await this.getAuthToken(true)
92-
this.isSignedIn = true
93-
localStorage.setItem(this.signedInKey, 'true')
94-
return this.accessToken
95-
} catch (error) {
96-
this.isSignedIn = false
97-
localStorage.setItem(this.signedInKey, 'false')
98-
throw error
99-
}
80+
await this.getAuthToken(true)
81+
return this.accessToken
10082
}
10183

10284
/**
10385
* Sign out and clear cached tokens
10486
*/
10587
async signOut() {
106-
if (!this.isChromeIdentityAvailable()) {
88+
if (!isChrome()) {
10789
throw new Error(
10890
'Chrome identity API not available. Google Tasks only works in Chrome.'
10991
)
@@ -122,27 +104,15 @@ class GoogleTasksBackendExtension extends TaskBackend {
122104
}
123105

124106
this.accessToken = null
125-
this.isSignedIn = false
126-
localStorage.setItem(this.signedInKey, 'false')
127107
this.clearLocalData()
128108
}
129109

130-
/**
131-
* Check if signed in
132-
*/
133-
getIsSignedIn() {
134-
return this.isSignedIn
135-
}
136110

137111
/**
138112
* Make an authenticated API request
139113
* Chrome identity API automatically handles token refresh
140114
*/
141115
async apiRequest(endpoint, options = {}) {
142-
if (!this.isSignedIn) {
143-
throw new Error('Not signed in')
144-
}
145-
146116
// Get a fresh token (Chrome caches it and auto-refreshes as needed)
147117
let token = await this.getAuthToken(false)
148118

@@ -192,8 +162,7 @@ class GoogleTasksBackendExtension extends TaskBackend {
192162
return retryResponse.json()
193163
} catch (error) {
194164
// Token refresh failed, user needs to sign in again
195-
this.isSignedIn = false
196-
localStorage.setItem(this.signedInKey, 'false')
165+
this.clearLocalData()
197166
throw new Error(
198167
'Authentication expired. Please sign in again.'
199168
)
@@ -212,10 +181,6 @@ class GoogleTasksBackendExtension extends TaskBackend {
212181
* Sync tasks from Google Tasks API
213182
*/
214183
async sync(resourceTypes = ['tasklists', 'tasks']) {
215-
if (!this.isSignedIn) {
216-
throw new Error('Not signed in to Google account')
217-
}
218-
219184
try {
220185
// Get task lists
221186
if (resourceTypes.includes('tasklists')) {

src/lib/backends/localstorage-backend.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ class LocalStorageBackend extends TaskBackend {
163163
* Delete a task
164164
*/
165165
async deleteTask(taskId) {
166-
throw new Error('test error')
167166
const idx = this.data.items.findIndex((item) => item.id === taskId)
168167
if (idx !== -1) {
169168
this.data.items.splice(idx, 1)
@@ -176,7 +175,6 @@ class LocalStorageBackend extends TaskBackend {
176175
* Add a new task
177176
*/
178177
async addTask(content, due) {
179-
throw new Error('test error')
180178
const newTask = {
181179
id: crypto.randomUUID(),
182180
content: content,

src/lib/components/Tasks.svelte

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@
7979
try {
8080
if (backend === 'google-tasks') {
8181
api = createTaskBackend(backend)
82-
83-
if (!api.getIsSignedIn()) {
84-
settings.googleTasksSignedIn = false
85-
error = 'google sign in expired'
86-
syncing = false
87-
return
88-
}
8982
} else {
9083
api = createTaskBackend(backend, { token })
9184
}
@@ -109,7 +102,16 @@
109102
await api.sync()
110103
tasks = api.getTasks()
111104
} catch (err) {
112-
error = `failed to sync tasks`
105+
// Check if this is an auth error for Google Tasks
106+
if (
107+
settings.taskBackend === 'google-tasks' &&
108+
err.message?.includes('Authentication expired')
109+
) {
110+
settings.googleTasksSignedIn = false
111+
error = 'google sign in expired'
112+
} else {
113+
error = `failed to sync tasks`
114+
}
113115
console.error(err)
114116
} finally {
115117
if (showSyncing) syncing = false

src/vite-env.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
/// <reference types="svelte" />
22
/// <reference types="vite/client" />
3+
4+
// Chrome extension API types
5+
declare namespace chrome {
6+
namespace identity {
7+
function getAuthToken(
8+
details: { interactive: boolean; scopes?: string[] },
9+
callback: (token?: string) => void
10+
): void
11+
function removeCachedAuthToken(
12+
details: { token: string },
13+
callback: () => void
14+
): void
15+
}
16+
namespace runtime {
17+
const lastError: { message: string } | undefined
18+
const id: string | undefined
19+
}
20+
}

0 commit comments

Comments
 (0)