fix(sync): trigger auth then resume operation when not authenticated (#69)

* fix(sync): trigger auth then resume operation when not authenticated

Problem: `get_access_token()` called `auth()` then immediately tried to
load tokens, but `auth()` is async (TCP server + browser redirect), so
tokens were never present at that point. All sync operations silently
aborted when unauthenticated.

Solution: Remove the inline auth attempt from `get_access_token()` and
add an `on_complete` callback to `auth()` / `_exchange_code()`. Add a
`with_token(callback)` helper in `gtasks.lua` and `gcal.lua` that
triggers auth with the sync operation as the continuation, so
`push`/`pull`/`sync` resume automatically after the OAuth flow
completes.

* ci: format
This commit is contained in:
Barrett Ruth 2026-03-05 13:24:43 -05:00 committed by GitHub
parent 7fb3289b21
commit 0163941a2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 62 additions and 35 deletions

View file

@ -136,17 +136,31 @@ local function delete_event(access_token, calendar_id, event_id)
return err
end
---@param callback fun(access_token: string): nil
local function with_token(callback)
oauth.async(function()
local token = client:get_access_token()
if not token then
client:auth(function()
oauth.async(function()
local fresh = client:get_access_token()
if fresh then
callback(fresh)
end
end)
end)
return
end
callback(token)
end)
end
function M.auth()
client:auth()
end
function M.push()
oauth.async(function()
local access_token = client:get_access_token()
if not access_token then
return
end
with_token(function(access_token)
local calendars, cal_err = get_all_calendars(access_token)
if cal_err or not calendars then
log.error(cal_err or 'failed to fetch calendars')