Skip to content

Commit 29a045a

Browse files
authored
feat(auth): support signout (zbirenbaum#54)
1 parent 814e233 commit 29a045a

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

lua/copilot/api.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ function mod.set_editor_info(client, params)
3535
return mod.notify(client, "setEditorInfo", params)
3636
end
3737

38+
---@alias copilot_check_status_params { options?: { localChecksOnly?: boolean } }
3839
---@alias copilot_check_status_data { user?: string }
3940

41+
---@param params? copilot_check_status_params
4042
---@return any|nil err
4143
---@return copilot_check_status_data data
4244
---@return table ctx
43-
function mod.check_status(client, callback)
44-
return mod.request(client, "checkStatus", {}, callback)
45+
function mod.check_status(client, params, callback)
46+
if type(params) == "function" then
47+
callback = params
48+
params = {}
49+
end
50+
return mod.request(client, "checkStatus", params or {}, callback)
4551
end
4652

4753
---@alias copilot_sign_in_initiate_data { verificationUri?: string, userCode?: string }
@@ -64,6 +70,10 @@ function mod.sign_in_confirm(client, params, callback)
6470
return mod.request(client, "signInConfirm", params, callback)
6571
end
6672

73+
function mod.sign_out(client, callback)
74+
return mod.request(client, "signOut", {}, callback)
75+
end
76+
6777
---@alias copilot_get_version_data { version: string }
6878

6979
---@return any|nil err

lua/copilot/auth.lua

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
local api = require("copilot.api")
2+
local u = require("copilot.util")
23

34
local M = {}
45

5-
function M.setup(client)
6-
local function echo(message)
7-
vim.cmd('echom "[Copilot] ' .. tostring(message):gsub('"', '\\"') .. '"')
8-
end
6+
local function echo(message)
7+
vim.cmd('echom "[Copilot] ' .. tostring(message):gsub('"', '\\"') .. '"')
8+
end
99

10+
function M.setup(client)
1011
local function copy_to_clipboard(str)
1112
vim.cmd(string.format(
1213
[[
@@ -100,6 +101,33 @@ function M.setup(client)
100101
initiate_setup()
101102
end
102103

104+
function M.signout()
105+
local client = u.get_copilot_client()
106+
if not client then
107+
return
108+
end
109+
110+
api.check_status(
111+
client,
112+
{ options = { localChecksOnly = true } },
113+
---@param status copilot_check_status_data
114+
function(err, status)
115+
if err then
116+
echo(err)
117+
return
118+
end
119+
120+
if status.user then
121+
echo("Signed out as GitHub user " .. status.user)
122+
else
123+
echo("Not signed in")
124+
end
125+
126+
api.sign_out(client, function() end)
127+
end
128+
)
129+
end
130+
103131
local function find_config_path()
104132
local config = vim.fn.expand("$XDG_CONFIG_HOME")
105133
if config and vim.fn.isdirectory(config) > 0 then

0 commit comments

Comments
 (0)