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

@ -236,11 +236,7 @@ function OAuthClient:get_access_token()
local creds = self:resolve_credentials()
local tokens = self:load_tokens()
if not tokens or not tokens.refresh_token then
self:auth()
tokens = self:load_tokens()
if not tokens then
return nil
end
return nil
end
local now = os.time()
local obtained = tokens.obtained_at or 0
@ -255,8 +251,9 @@ function OAuthClient:get_access_token()
return tokens.access_token
end
---@param on_complete? fun(): nil
---@return nil
function OAuthClient:auth()
function OAuthClient:auth(on_complete)
local creds = self:resolve_credentials()
local port = self.port
@ -329,7 +326,7 @@ function OAuthClient:auth()
close_server()
if code then
vim.schedule(function()
self:_exchange_code(creds, code, code_verifier, port)
self:_exchange_code(creds, code, code_verifier, port, on_complete)
end)
end
end)
@ -347,8 +344,9 @@ end
---@param code string
---@param code_verifier string
---@param port integer
---@param on_complete? fun(): nil
---@return nil
function OAuthClient:_exchange_code(creds, code, code_verifier, port)
function OAuthClient:_exchange_code(creds, code, code_verifier, port, on_complete)
local body = 'client_id='
.. M.url_encode(creds.client_id)
.. '&client_secret='
@ -387,6 +385,9 @@ function OAuthClient:_exchange_code(creds, code, code_verifier, port)
decoded.obtained_at = os.time()
self:save_tokens(decoded)
log.info(self.name .. ' authorized successfully.')
if on_complete then
on_complete()
end
end
---@param opts { name: string, scope: string, port: integer, config_key: string }