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:
parent
7fb3289b21
commit
0163941a2b
3 changed files with 62 additions and 35 deletions
|
|
@ -337,23 +337,38 @@ local function pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
return created, updated
|
||||
end
|
||||
|
||||
---@return string? access_token
|
||||
---@param access_token string
|
||||
---@return table<string, string>? tasklists
|
||||
---@return pending.Store? store
|
||||
---@return pending.Store? s
|
||||
---@return string? now_ts
|
||||
local function sync_setup()
|
||||
local access_token = client:get_access_token()
|
||||
if not access_token then
|
||||
return nil
|
||||
end
|
||||
local function sync_setup(access_token)
|
||||
local tasklists, tl_err = get_all_tasklists(access_token)
|
||||
if tl_err or not tasklists then
|
||||
log.error(tl_err or 'failed to fetch task lists')
|
||||
return nil
|
||||
return nil, nil, nil
|
||||
end
|
||||
local s = require('pending').store()
|
||||
local now_ts = os.date('!%Y-%m-%dT%H:%M:%SZ') --[[@as string]]
|
||||
return access_token, tasklists, s, now_ts
|
||||
return tasklists, s, now_ts
|
||||
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()
|
||||
|
|
@ -361,12 +376,11 @@ function M.auth()
|
|||
end
|
||||
|
||||
function M.push()
|
||||
oauth.async(function()
|
||||
local access_token, tasklists, s, now_ts = sync_setup()
|
||||
if not access_token then
|
||||
with_token(function(access_token)
|
||||
local tasklists, s, now_ts = sync_setup(access_token)
|
||||
if not tasklists then
|
||||
return
|
||||
end
|
||||
---@cast tasklists table<string, string>
|
||||
---@cast s pending.Store
|
||||
---@cast now_ts string
|
||||
local by_gtasks_id = build_id_index(s)
|
||||
|
|
@ -382,12 +396,11 @@ function M.push()
|
|||
end
|
||||
|
||||
function M.pull()
|
||||
oauth.async(function()
|
||||
local access_token, tasklists, s, now_ts = sync_setup()
|
||||
if not access_token then
|
||||
with_token(function(access_token)
|
||||
local tasklists, s, now_ts = sync_setup(access_token)
|
||||
if not tasklists then
|
||||
return
|
||||
end
|
||||
---@cast tasklists table<string, string>
|
||||
---@cast s pending.Store
|
||||
---@cast now_ts string
|
||||
local by_gtasks_id = build_id_index(s)
|
||||
|
|
@ -403,12 +416,11 @@ function M.pull()
|
|||
end
|
||||
|
||||
function M.sync()
|
||||
oauth.async(function()
|
||||
local access_token, tasklists, s, now_ts = sync_setup()
|
||||
if not access_token then
|
||||
with_token(function(access_token)
|
||||
local tasklists, s, now_ts = sync_setup(access_token)
|
||||
if not tasklists then
|
||||
return
|
||||
end
|
||||
---@cast tasklists table<string, string>
|
||||
---@cast s pending.Store
|
||||
---@cast now_ts string
|
||||
local by_gtasks_id = build_id_index(s)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue