feat(credentials): re-prompt on bad credentials and improve cancel UX
Problem: After a failed login attempt, the user had to re-run `:CP <platform> login` manually. Cancel messages lacked context, and credential prompts in `submit.lua` used raw platform IDs instead of display names. Solution: `prompt_and_login` now recurses after failure so the user is re-prompted until they cancel or succeed. `submit.lua` extracts the scraper call into `do_submit` and re-prompts via `prompt_credentials` on `bad_credentials`. Username prompts show `<Esc> to cancel`, and cancel messages include the platform display name.
This commit is contained in:
parent
0c06b4a55a
commit
e3c81e895a
2 changed files with 21 additions and 11 deletions
|
|
@ -14,16 +14,16 @@ local STATUS_MESSAGES = {
|
||||||
---@param platform string
|
---@param platform string
|
||||||
---@param display string
|
---@param display string
|
||||||
local function prompt_and_login(platform, display)
|
local function prompt_and_login(platform, display)
|
||||||
vim.ui.input({ prompt = '[cp.nvim]: ' .. display .. ' username: ' }, function(username)
|
vim.ui.input({ prompt = '[cp.nvim]: ' .. display .. ' username (<Esc> to cancel): ' }, function(username)
|
||||||
if not username or username == '' then
|
if not username or username == '' then
|
||||||
logger.log('Cancelled', { level = vim.log.levels.WARN })
|
logger.log(display .. ' login cancelled', { level = vim.log.levels.WARN })
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
vim.fn.inputsave()
|
vim.fn.inputsave()
|
||||||
local password = vim.fn.inputsecret('[cp.nvim]: ' .. display .. ' password: ')
|
local password = vim.fn.inputsecret('[cp.nvim]: ' .. display .. ' password: ')
|
||||||
vim.fn.inputrestore()
|
vim.fn.inputrestore()
|
||||||
if not password or password == '' then
|
if not password or password == '' then
|
||||||
logger.log('Cancelled', { level = vim.log.levels.WARN })
|
logger.log(display .. ' login cancelled', { level = vim.log.levels.WARN })
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -50,6 +50,7 @@ local function prompt_and_login(platform, display)
|
||||||
display .. ' login failed: ' .. (constants.LOGIN_ERRORS[err] or err),
|
display .. ' login failed: ' .. (constants.LOGIN_ERRORS[err] or err),
|
||||||
{ level = vim.log.levels.ERROR }
|
{ level = vim.log.levels.ERROR }
|
||||||
)
|
)
|
||||||
|
prompt_and_login(platform, display)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -19,13 +19,14 @@ local function prompt_credentials(platform, callback)
|
||||||
callback(saved)
|
callback(saved)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
vim.ui.input({ prompt = platform .. ' username: ' }, function(username)
|
local display = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
|
||||||
|
vim.ui.input({ prompt = '[cp.nvim]: ' .. display .. ' username (<Esc> to cancel): ' }, function(username)
|
||||||
if not username or username == '' then
|
if not username or username == '' then
|
||||||
logger.log('Submit cancelled', { level = vim.log.levels.WARN })
|
logger.log('Submit cancelled', { level = vim.log.levels.WARN })
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
vim.fn.inputsave()
|
vim.fn.inputsave()
|
||||||
local password = vim.fn.inputsecret(platform .. ' password: ')
|
local password = vim.fn.inputsecret('[cp.nvim]: ' .. display .. ' password: ')
|
||||||
vim.fn.inputrestore()
|
vim.fn.inputrestore()
|
||||||
vim.cmd.redraw()
|
vim.cmd.redraw()
|
||||||
if not password or password == '' then
|
if not password or password == '' then
|
||||||
|
|
@ -86,7 +87,7 @@ function M.submit(opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
prompt_credentials(platform, function(creds)
|
local function do_submit(creds)
|
||||||
vim.cmd.update()
|
vim.cmd.update()
|
||||||
|
|
||||||
require('cp.scraper').submit(
|
require('cp.scraper').submit(
|
||||||
|
|
@ -112,16 +113,24 @@ function M.submit(opts)
|
||||||
local err = result and result.error or 'unknown error'
|
local err = result and result.error or 'unknown error'
|
||||||
if err == 'bad_credentials' or err:match('^Login failed') then
|
if err == 'bad_credentials' or err:match('^Login failed') then
|
||||||
cache.clear_credentials(platform)
|
cache.clear_credentials(platform)
|
||||||
|
logger.log(
|
||||||
|
'Submit failed: ' .. (constants.LOGIN_ERRORS[err] or err),
|
||||||
|
{ level = vim.log.levels.ERROR }
|
||||||
|
)
|
||||||
|
prompt_credentials(platform, do_submit)
|
||||||
|
else
|
||||||
|
logger.log(
|
||||||
|
'Submit failed: ' .. (constants.LOGIN_ERRORS[err] or err),
|
||||||
|
{ level = vim.log.levels.ERROR }
|
||||||
|
)
|
||||||
end
|
end
|
||||||
logger.log(
|
|
||||||
'Submit failed: ' .. (constants.LOGIN_ERRORS[err] or err),
|
|
||||||
{ level = vim.log.levels.ERROR }
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
end)
|
end
|
||||||
|
|
||||||
|
prompt_credentials(platform, do_submit)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue