11import 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' ) ) {
0 commit comments